Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,14 @@ public Map<String, Object> coerceVariableValues(GraphQLSchema schema, List<Varia
}
} else {
Object value = variableValues.get(variableName);
Object coercedValue = getVariableValue(fieldVisibility, variableDefinition, variableType, value);
Object coercedValue = coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
coercedValues.put(variableName, coercedValue);
}
}

return coercedValues;
}

private Object getVariableValue(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, GraphQLType variableType, Object value) {

if (value == null && variableDefinition.getDefaultValue() != null) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use null when variable defined in variableValuesMap is null

return coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
}

return coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
}

public Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry().fieldVisibility(DEFAULT_FIELD_VISIBILITY).build();
return getArgumentValuesImpl(codeRegistry, argumentTypes, arguments, variables);
Expand Down
38 changes: 38 additions & 0 deletions src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,44 @@ class ValuesResolverTest extends Specification {
thrown(GraphQLException)
}

def "coerceVariableValues: use null when variable defined in variableValuesMap is null"() {
given:
def schema = TestUtil.schemaWithInputType(nonNull(GraphQLString))

def defaultValueForFoo = new StringValue("defaultValueForFoo")
VariableDefinition fooVarDef = new VariableDefinition("foo", new TypeName("String"), defaultValueForFoo)

def defaultValueForBar = new StringValue("defaultValueForBar")
VariableDefinition barVarDef = new VariableDefinition("bar", new TypeName("String"), defaultValueForBar)

def variableValuesMap = ["foo": null, "bar": "barValue"]

when:
def resolvedVars = resolver.coerceVariableValues(schema, [fooVarDef, barVarDef], variableValuesMap)

then:
resolvedVars['foo'] == null
resolvedVars['bar'] == "barValue"
}

def "coerceVariableValues: if variableType is a Non‐Nullable type and value is null, throw a query error"() {
given:
def schema = TestUtil.schemaWithInputType(nonNull(GraphQLString))

def defaultValueForFoo = new StringValue("defaultValueForFoo")
VariableDefinition fooVarDef = new VariableDefinition("foo", new NonNullType(new TypeName("String")), defaultValueForFoo)


def variableValuesMap = ["foo": null]

when:
resolver.coerceVariableValues(schema, [fooVarDef], variableValuesMap)

then:
def error = thrown(NonNullableValueCoercedAsNullException)
error.message == "Field 'foo' of variable 'foo' has coerced Null value for NonNull type 'String!'"
}

// Note: use NullValue defined in Field when it exists,
// and ignore defaultValue defined in type system
def "getArgumentValues: use null value when argumentValue defined in Field is null"() {
Expand Down