Skip to content

Commit 39c567e

Browse files
dugenkui03dugenkui
andauthored
fix for coerceVariableValues (#2083)
Co-authored-by: dugenkui <dugenkui@dugenkuideMBP.lan>
1 parent 4213a30 commit 39c567e

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/main/java/graphql/execution/ValuesResolver.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,14 @@ public Map<String, Object> coerceVariableValues(GraphQLSchema schema, List<Varia
6767
}
6868
} else {
6969
Object value = variableValues.get(variableName);
70-
Object coercedValue = getVariableValue(fieldVisibility, variableDefinition, variableType, value);
70+
Object coercedValue = coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
7171
coercedValues.put(variableName, coercedValue);
7272
}
7373
}
7474

7575
return coercedValues;
7676
}
7777

78-
private Object getVariableValue(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, GraphQLType variableType, Object value) {
79-
80-
if (value == null && variableDefinition.getDefaultValue() != null) {
81-
return coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
82-
}
83-
84-
return coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
85-
}
86-
8778
public Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
8879
GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry().fieldVisibility(DEFAULT_FIELD_VISIBILITY).build();
8980
return getArgumentValuesImpl(codeRegistry, argumentTypes, arguments, variables);

src/test/groovy/graphql/execution/ValuesResolverTest.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,44 @@ class ValuesResolverTest extends Specification {
451451
thrown(GraphQLException)
452452
}
453453

454+
def "coerceVariableValues: use null when variable defined in variableValuesMap is null"() {
455+
given:
456+
def schema = TestUtil.schemaWithInputType(nonNull(GraphQLString))
457+
458+
def defaultValueForFoo = new StringValue("defaultValueForFoo")
459+
VariableDefinition fooVarDef = new VariableDefinition("foo", new TypeName("String"), defaultValueForFoo)
460+
461+
def defaultValueForBar = new StringValue("defaultValueForBar")
462+
VariableDefinition barVarDef = new VariableDefinition("bar", new TypeName("String"), defaultValueForBar)
463+
464+
def variableValuesMap = ["foo": null, "bar": "barValue"]
465+
466+
when:
467+
def resolvedVars = resolver.coerceVariableValues(schema, [fooVarDef, barVarDef], variableValuesMap)
468+
469+
then:
470+
resolvedVars['foo'] == null
471+
resolvedVars['bar'] == "barValue"
472+
}
473+
474+
def "coerceVariableValues: if variableType is a Non‐Nullable type and value is null, throw a query error"() {
475+
given:
476+
def schema = TestUtil.schemaWithInputType(nonNull(GraphQLString))
477+
478+
def defaultValueForFoo = new StringValue("defaultValueForFoo")
479+
VariableDefinition fooVarDef = new VariableDefinition("foo", new NonNullType(new TypeName("String")), defaultValueForFoo)
480+
481+
482+
def variableValuesMap = ["foo": null]
483+
484+
when:
485+
resolver.coerceVariableValues(schema, [fooVarDef], variableValuesMap)
486+
487+
then:
488+
def error = thrown(NonNullableValueCoercedAsNullException)
489+
error.message == "Field 'foo' of variable 'foo' has coerced Null value for NonNull type 'String!'"
490+
}
491+
454492
// Note: use NullValue defined in Field when it exists,
455493
// and ignore defaultValue defined in type system
456494
def "getArgumentValues: use null value when argumentValue defined in Field is null"() {

0 commit comments

Comments
 (0)