2
2
3
3
4
4
import graphql .GraphQLException ;
5
- import graphql .language .*;
6
- import graphql .schema .*;
5
+ import graphql .language .Argument ;
6
+ import graphql .language .ArrayValue ;
7
+ import graphql .language .NullValue ;
8
+ import graphql .language .ObjectField ;
9
+ import graphql .language .ObjectValue ;
10
+ import graphql .language .Value ;
11
+ import graphql .language .VariableDefinition ;
12
+ import graphql .language .VariableReference ;
13
+ import graphql .schema .GraphQLArgument ;
14
+ import graphql .schema .GraphQLEnumType ;
15
+ import graphql .schema .GraphQLInputObjectField ;
16
+ import graphql .schema .GraphQLInputObjectType ;
17
+ import graphql .schema .GraphQLList ;
18
+ import graphql .schema .GraphQLNonNull ;
19
+ import graphql .schema .GraphQLScalarType ;
20
+ import graphql .schema .GraphQLSchema ;
21
+ import graphql .schema .GraphQLType ;
7
22
8
- import java .util .*;
23
+ import java .util .ArrayList ;
24
+ import java .util .Collections ;
25
+ import java .util .LinkedHashMap ;
26
+ import java .util .List ;
27
+ import java .util .Map ;
9
28
10
29
public class ValuesResolver {
11
30
12
31
13
- public Map <String , Object > getVariableValues (GraphQLSchema schema , List <VariableDefinition > variableDefinitions , Map <String , Object > inputs ) {
32
+ public Map <String , Object > getVariableValues (GraphQLSchema schema , List <VariableDefinition > variableDefinitions , Map <String , Object > args ) {
14
33
Map <String , Object > result = new LinkedHashMap <>();
15
34
for (VariableDefinition variableDefinition : variableDefinitions ) {
16
- result .put (variableDefinition .getName (), getVariableValue (schema , variableDefinition , inputs .get (variableDefinition .getName ())));
35
+ String varName = variableDefinition .getName ();
36
+ // we transfer the variable as field arguments if its present as value
37
+ if (args .containsKey (varName )) {
38
+ Object arg = args .get (varName );
39
+ Object variableValue = getVariableValue (schema , variableDefinition , arg );
40
+ result .put (varName , variableValue );
41
+ }
17
42
}
18
43
return result ;
19
44
}
@@ -23,14 +48,19 @@ public Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes
23
48
Map <String , Object > result = new LinkedHashMap <>();
24
49
Map <String , Argument > argumentMap = argumentMap (arguments );
25
50
for (GraphQLArgument fieldArgument : argumentTypes ) {
26
- Argument argument = argumentMap .get (fieldArgument .getName ());
51
+ String argName = fieldArgument .getName ();
52
+ Argument argument = argumentMap .get (argName );
27
53
Object value ;
28
54
if (argument != null ) {
29
55
value = coerceValueAst (fieldArgument .getType (), argument .getValue (), variables );
30
56
} else {
31
57
value = fieldArgument .getDefaultValue ();
32
58
}
33
- result .put (fieldArgument .getName (), value );
59
+ // only put an arg into the result IF they specified a variable at all or
60
+ // the default value ended up being something non null
61
+ if (argumentMap .containsKey (argName ) || value != null ) {
62
+ result .put (argName , value );
63
+ }
34
64
}
35
65
return result ;
36
66
}
@@ -48,6 +78,7 @@ private Map<String, Argument> argumentMap(List<Argument> arguments) {
48
78
private Object getVariableValue (GraphQLSchema schema , VariableDefinition variableDefinition , Object inputValue ) {
49
79
GraphQLType type = TypeFromAST .getTypeFromAST (schema , variableDefinition .getType ());
50
80
81
+ //noinspection ConstantConditions
51
82
if (!isValid (type , inputValue )) {
52
83
throw new GraphQLException ("Invalid value for type" );
53
84
}
@@ -81,6 +112,7 @@ private Object coerceValue(GraphQLType graphQLType, Object value) {
81
112
} else if (graphQLType instanceof GraphQLList ) {
82
113
return coerceValueForList ((GraphQLList ) graphQLType , value );
83
114
} else if (graphQLType instanceof GraphQLInputObjectType && value instanceof Map ) {
115
+ //noinspection unchecked
84
116
return coerceValueForInputObjectType ((GraphQLInputObjectType ) graphQLType , (Map <String , Object >) value );
85
117
} else if (graphQLType instanceof GraphQLInputObjectType ) {
86
118
return value ;
@@ -170,22 +202,49 @@ private Object coerceValueAstForInputObject(GraphQLInputObjectType type, ObjectV
170
202
171
203
for (GraphQLInputObjectField inputTypeField : type .getFields ()) {
172
204
if (inputValueFieldsByName .containsKey (inputTypeField .getName ())) {
205
+ boolean putObjectInMap = true ;
206
+
173
207
ObjectField field = inputValueFieldsByName .get (inputTypeField .getName ());
174
- Object fieldValue = coerceValueAst (inputTypeField .getType (), field .getValue (), variables );
175
- if (fieldValue == null ) {
176
- fieldValue = inputTypeField .getDefaultValue ();
208
+ Value fieldInputValue = field .getValue ();
209
+
210
+ Object fieldObject = null ;
211
+ if (fieldInputValue instanceof VariableReference ) {
212
+ String varName = ((VariableReference ) fieldInputValue ).getName ();
213
+ if (!variables .containsKey (varName )) {
214
+ putObjectInMap = false ;
215
+ } else {
216
+ fieldObject = variables .get (varName );
217
+ }
218
+ } else {
219
+ fieldObject = coerceValueAst (inputTypeField .getType (), fieldInputValue , variables );
220
+ }
221
+
222
+ if (fieldObject == null ) {
223
+ if (!field .getValue ().isEqualTo (NullValue .Null )) {
224
+ fieldObject = inputTypeField .getDefaultValue ();
225
+ }
226
+ }
227
+ if (putObjectInMap ) {
228
+ result .put (field .getName (), fieldObject );
229
+ } else {
230
+ assertNonNullInputField (inputTypeField );
177
231
}
178
- result .put (field .getName (), fieldValue );
179
232
} else if (inputTypeField .getDefaultValue () != null ) {
180
233
result .put (inputTypeField .getName (), inputTypeField .getDefaultValue ());
181
- } else if (inputTypeField .getType () instanceof GraphQLNonNull ) {
182
- // Possibly overkill; an object literal with a missing non null field shouldn't pass validation
183
- throw new GraphQLException ("Null value for NonNull type " + inputTypeField .getType ());
234
+ } else {
235
+ assertNonNullInputField (inputTypeField );
184
236
}
185
237
}
186
238
return result ;
187
239
}
188
240
241
+ private void assertNonNullInputField (GraphQLInputObjectField inputTypeField ) {
242
+ if (inputTypeField .getType () instanceof GraphQLNonNull ) {
243
+ // Possibly overkill; an object literal with a missing non null field shouldn't pass validation
244
+ throw new GraphQLException ("Null value for NonNull type " + inputTypeField .getType ());
245
+ }
246
+ }
247
+
189
248
private Map <String , ObjectField > mapObjectValueFieldsByName (ObjectValue inputValue ) {
190
249
Map <String , ObjectField > inputValueFieldsByName = new LinkedHashMap <>();
191
250
for (ObjectField objectField : inputValue .getObjectFields ()) {
0 commit comments