Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit 1862464

Browse files
authored
Merge pull request #48 from adamivora/VariableTranslation
fix(Type): variable values and schema types translation
2 parents 3b77d6f + f324225 commit 1862464

39 files changed

+871
-213
lines changed

examples/GraphQLCore.GraphiQLExample/Controllers/GraphQLController.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public JsonResult Post([FromBody] GraphiQLInput input)
5353

5454
private static dynamic GetVariables(GraphiQLInput input)
5555
{
56-
if (string.IsNullOrWhiteSpace(input.Variables))
56+
var variables = input.Variables?.ToString();
57+
58+
if (string.IsNullOrEmpty(variables))
5759
return new ExpandoObject();
5860

59-
return JsonConvert.DeserializeObject<ExpandoObject>(input.Variables);
61+
return JsonConvert.DeserializeObject<ExpandoObject>(variables);
6062
}
6163
}
62-
}
64+
}

examples/GraphQLCore.GraphiQLExample/Models/GraphiQLInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class GraphiQLInput
44
{
55
public string OperationName { get; set; }
66
public string Query { get; set; }
7-
public string Variables { get; set; }
7+
public dynamic Variables { get; set; }
88
}
9-
}
9+
}

examples/GraphQLCore.GraphiQLExample/public/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
height: 100vh;
1414
}
1515
</style>
16-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/graphiql/0.7.3/graphiql.css" />
16+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/graphiql/0.9.3/graphiql.css" />
1717
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
18-
<script src="//cdn.jsdelivr.net/react/15.0.1/react.min.js"></script>
19-
<script src="//cdn.jsdelivr.net/react/15.0.1/react-dom.min.js"></script>
20-
<script src="https://cdn.jsdelivr.net/graphiql/0.7.3/graphiql.js"></script>
18+
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
19+
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
20+
<script src="https://cdn.jsdelivr.net/graphiql/0.9.3/graphiql.js"></script>
2121
</head>
2222
<body>
2323
<div id="graphiql">Loading...</div>

src/GraphQLCore/Execution/FieldScope.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using GraphQLCore.Type.Directives;
44
using Language.AST;
5+
using System;
56
using System.Collections;
67
using System.Collections.Generic;
78
using System.Dynamic;
@@ -12,7 +13,6 @@
1213
using Type.Complex;
1314
using Type.Translation;
1415
using Utils;
15-
using System;
1616

1717
public class FieldScope
1818
{
@@ -76,11 +76,6 @@ public object GetArgumentValue(IEnumerable<GraphQLArgument> arguments, string ar
7676
if (argument == null)
7777
return null;
7878

79-
if (argument.Value.Kind == ASTNodeKind.Variable)
80-
{
81-
return this.variableResolver.GetValue((GraphQLVariable)argument.Value);
82-
}
83-
8479
return type.GetFromAst(argument.Value, this.schemaRepository);
8580
}
8681

@@ -118,8 +113,11 @@ private object GetValueForArgument(IList<GraphQLArgument> arguments, ParameterEx
118113
return this.CreateContextObject(e.Type);
119114

120115
return ReflectionUtilities.ChangeValueType(
121-
this.GetArgumentValue(arguments, e.Name,
122-
this.schemaRepository.GetSchemaInputTypeFor(e.Type)), e.Type);
116+
this.GetArgumentValue(
117+
arguments,
118+
e.Name,
119+
this.schemaRepository.GetSchemaInputTypeFor(e.Type)),
120+
e.Type);
123121
}
124122

125123
private bool IsContextType(ParameterExpression e)

src/GraphQLCore/Execution/VariableResolver.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Dynamic;
1010
using System.Linq;
1111
using System.Linq.Expressions;
12+
using System.Reflection;
1213

1314
namespace GraphQLCore.Execution
1415
{
@@ -53,8 +54,8 @@ public object GetValue(string variableName)
5354
object variableValue;
5455
this.variables.TryGetValue(variableName, out variableValue);
5556

56-
if (variableValue != null)
57-
return this.TranslatePerDefinition(variableValue, typeDefinition);
57+
if (variableValue != null)
58+
return this.TranslatePerDefinition(variableValue, typeDefinition);
5859

5960
if (typeDefinition is Type.GraphQLNonNullType)
6061
throw new GraphQLException($"Type \"{typeDefinition.ToString()}\" is non-nullable and cannot be null.");
@@ -73,7 +74,15 @@ public object TranslatePerDefinition(object inputObject, GraphQLBaseType typeDef
7374
return this.CreateObjectFromDynamic((GraphQLInputObjectType)typeDefinition, (ExpandoObject)inputObject);
7475

7576
if (typeDefinition is GraphQLList)
76-
return this.CreateList((IEnumerable)inputObject, (GraphQLList)typeDefinition);
77+
{
78+
if (inputObject == null)
79+
return null;
80+
81+
if (ReflectionUtilities.IsCollection(inputObject.GetType()))
82+
return this.CreateList((IEnumerable)inputObject, (GraphQLList)typeDefinition);
83+
84+
return this.CreateSingleValueList(inputObject, (GraphQLList)typeDefinition);
85+
}
7786

7887
return inputObject;
7988
}
@@ -109,6 +118,16 @@ private IEnumerable<object> CreateList(IEnumerable inputObject, GraphQLList type
109118
yield return this.TranslatePerDefinition(item, typeDefinition.MemberType);
110119
}
111120

121+
private IEnumerable CreateSingleValueList(object inputObject, GraphQLList typeDefinition)
122+
{
123+
var systemType = this.schemaRepository.GetInputSystemTypeFor(typeDefinition.MemberType);
124+
125+
var singleValue = this.TranslatePerDefinition(inputObject, typeDefinition.MemberType);
126+
singleValue = ReflectionUtilities.ChangeValueType(singleValue, systemType);
127+
128+
yield return singleValue;
129+
}
130+
112131
private GraphQLBaseType GetTypeDefinition(GraphQLType typeDefinition)
113132
{
114133
if (typeDefinition is GraphQLNamedType)

src/GraphQLCore/Language/AST/Enums.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public enum ASTNodeKind
1717
FloatValue,
1818
StringValue,
1919
BooleanValue,
20+
NullValue,
2021
EnumValue,
2122
ListValue,
2223
ObjectValue,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GraphQLCore.Language.AST
2+
{
3+
public class GraphQLNullValue : GraphQLValue
4+
{
5+
public override ASTNodeKind Kind
6+
{
7+
get
8+
{
9+
return ASTNodeKind.NullValue;
10+
}
11+
}
12+
}
13+
}

src/GraphQLCore/Language/GraphQLAstVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public virtual GraphQLListValue EndVisitListValue(GraphQLListValue node)
279279
return node;
280280
}
281281

282-
private ASTNode BeginVisitListValue(GraphQLListValue node)
282+
public virtual GraphQLListValue BeginVisitListValue(GraphQLListValue node)
283283
{
284284
foreach (var value in node.Values)
285285
this.BeginVisitNode(value);

src/GraphQLCore/Language/ParserContext.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ private GraphQLValue ParseBooleanValue(Token token)
250250
};
251251
}
252252

253+
private GraphQLValue ParseNullValue(Token token)
254+
{
255+
this.Advance();
256+
return new GraphQLNullValue()
257+
{
258+
Location = this.GetLocation(token.Start)
259+
};
260+
}
261+
253262
private GraphQLValue ParseConstantValue()
254263
{
255264
return this.ParseValueLiteral(true);
@@ -615,11 +624,13 @@ private GraphQLValue ParseNameValue(bool isConstant)
615624

616625
if (token.Value.Equals("true") || token.Value.Equals("false"))
617626
return this.ParseBooleanValue(token);
618-
else if (token.Value != null && !token.Value.Equals("null"))
627+
else if (token.Value.Equals("null"))
628+
return this.ParseNullValue(token);
629+
else if (token != null)
619630
return this.ParseEnumValue(token);
620631

621632
throw new GraphQLSyntaxErrorException(
622-
$"Unexpected {this.currentToken}", this.source, this.currentToken.Start);
633+
$"Unexpected {this.currentToken}", this.source, this.currentToken.Start);
623634
}
624635

625636
private GraphQLValue ParseObject(bool isConstant)

src/GraphQLCore/Type/Complex/GraphQLFieldInfo.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,9 @@ public abstract class GraphQLFieldInfo
1414

1515
public GraphQLBaseType GetGraphQLType(ISchemaRepository schemaRepository)
1616
{
17-
return this.GetGraphQLType(this.SystemType, schemaRepository);
17+
return this.GetSchemaType(this.SystemType, schemaRepository);
1818
}
1919

2020
protected abstract GraphQLBaseType GetSchemaType(Type type, ISchemaRepository schemaRepository);
21-
22-
private GraphQLBaseType GetGraphQLType(Type type, ISchemaRepository schemaRepository)
23-
{
24-
if (ReflectionUtilities.IsCollection(type))
25-
{
26-
return new GraphQLList(this.GetGraphQLType(
27-
ReflectionUtilities.GetCollectionMemberType(type),
28-
schemaRepository));
29-
}
30-
31-
if (ReflectionUtilities.IsNullable(type))
32-
return this.GetSchemaType(Nullable.GetUnderlyingType(type), schemaRepository);
33-
34-
if (ReflectionUtilities.IsValueType(type))
35-
return new GraphQLNonNullType(this.GetSchemaType(type, schemaRepository));
36-
37-
return this.GetSchemaType(type, schemaRepository);
38-
}
3921
}
40-
}
22+
}

0 commit comments

Comments
 (0)