Skip to content

Commit 3bca3b9

Browse files
committed
Add middleware options
1 parent a86177c commit 3bca3b9

File tree

7 files changed

+301
-167
lines changed

7 files changed

+301
-167
lines changed

src/GraphQL.Upload.AspNetCore/GraphQL.Upload.AspNetCore.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Version>0.1.0</Version>
6+
<Authors>Jannik Lassahn</Authors>
7+
<Company />
8+
<RepositoryUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</RepositoryUrl>
9+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
10+
<PackageProjectUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</PackageProjectUrl>
11+
<Description>Middleware and an Upload scalar to add support for GraphQL multipart requests for ASP.NET Core</Description>
512
</PropertyGroup>
613

714
<ItemGroup>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System.Collections.Generic;
4+
5+
namespace GraphQL.Upload.AspNetCore
6+
{
7+
internal class GraphQLRequest
8+
{
9+
public const string QueryKey = "query";
10+
public const string VariablesKey = "variables";
11+
public const string OperationNameKey = "operationName";
12+
public const string MapKey = "map";
13+
14+
[JsonProperty(QueryKey)]
15+
public string Query { get; set; }
16+
17+
[JsonProperty(VariablesKey)]
18+
public JObject Variables { get; set; }
19+
20+
[JsonProperty(OperationNameKey)]
21+
public string OperationName { get; set; }
22+
23+
[JsonIgnore]
24+
public List<Meta> TokensToReplace { get; set; }
25+
26+
public Inputs GetInputs()
27+
{
28+
var variables = Variables?.ToInputs();
29+
30+
// the following implementation seems brittle because of a lot of casting
31+
// and it depends on the types that ToInputs() creates.
32+
33+
foreach (var info in TokensToReplace)
34+
{
35+
int i = 0;
36+
object o = variables;
37+
38+
foreach (var p in info.Parts)
39+
{
40+
var isLast = i++ == info.Parts.Count - 1;
41+
42+
if (p is string s)
43+
{
44+
if (isLast)
45+
((Inputs)o)[s] = info.File;
46+
else
47+
o = ((Inputs)o)[s];
48+
}
49+
else if (p is int index)
50+
{
51+
if (isLast)
52+
((List<object>)o)[index] = info.File;
53+
else
54+
o = ((List<object>)o)[index];
55+
}
56+
}
57+
}
58+
59+
return variables;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)