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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public NonNullableValueCoercedAsNullException(VariableDefinition variableDefinit
this.path = path;
}

public NonNullableValueCoercedAsNullException(GraphQLType graphQLType) {
super(format("Coerced Null value for NonNull type '%s'", GraphQLTypeUtil.simplePrint(graphQLType)));
}

public NonNullableValueCoercedAsNullException(VariableDefinition variableDefinition, String causeMessage) {
super(format("Variable '%s' has an invalid value: %s", variableDefinition.getName(), causeMessage));
this.sourceLocations = Collections.singletonList(variableDefinition.getSourceLocation());
}

public NonNullableValueCoercedAsNullException(String fieldName, List<Object> path, GraphQLType graphQLType) {
super(format("Field '%s' has coerced Null value for NonNull type '%s'",
fieldName, GraphQLTypeUtil.simplePrint(graphQLType)));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ private Map<String, Object> externalValueToInternalValueForVariables(GraphQLSche
.cause(e.getCause())
.sourceLocation(variableDefinition.getSourceLocation())
.build();
} catch (NonNullableValueCoercedAsNullException e) {
throw new NonNullableValueCoercedAsNullException(variableDefinition, e.getMessage());
}
}

Expand Down Expand Up @@ -495,7 +497,7 @@ private Object externalValueToInternalValue(GraphqlFieldVisibility fieldVisibili
Object returnValue =
externalValueToInternalValue(fieldVisibility, unwrapOne(graphQLType), value);
if (returnValue == null) {
throw new NonNullableValueCoercedAsNullException("", emptyList(), graphQLType);
throw new NonNullableValueCoercedAsNullException(graphQLType);
}
return returnValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/graphql/Issue743.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ class Issue743 extends Specification {
executionResult.data == null
executionResult.errors.size() == 1
executionResult.errors[0].errorType == ErrorType.ValidationError
executionResult.errors[0].message == "Variable 'isTrue' has coerced Null value for NonNull type 'Boolean!'"
executionResult.errors[0].message == "Variable 'isTrue' has an invalid value: Variable 'isTrue' has coerced Null value for NonNull type 'Boolean!'"
}
}
6 changes: 3 additions & 3 deletions src/test/groovy/graphql/NullVariableCoercionTest.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package graphql


import graphql.language.SourceLocation
import graphql.schema.DataFetcher
import graphql.schema.GraphQLObjectType
import graphql.schema.idl.RuntimeWiring
Expand Down Expand Up @@ -68,8 +68,8 @@ class NullVariableCoercionTest extends Specification {
varResult.data == null
varResult.errors.size() == 1
varResult.errors[0].errorType == ErrorType.ValidationError
varResult.errors[0].message == "Field 'baz' has coerced Null value for NonNull type 'String!'"
// varResult.errors[0].locations == [new SourceLocation(1, 11)]
varResult.errors[0].message == "Variable 'input' has an invalid value: Field 'baz' has coerced Null value for NonNull type 'String!'"
varResult.errors[0].locations == [new SourceLocation(1, 11)]
}

def "can handle defaulting on complex input objects"() {
Expand Down
20 changes: 19 additions & 1 deletion src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,25 @@ class ValuesResolverTest extends Specification {

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

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

def defaultValueForFoo = new ArrayValue([new StringValue("defaultValueForFoo")])
def type = new ListType(new NonNullType(new TypeName("String")))
VariableDefinition fooVarDef = new VariableDefinition("foo", type, defaultValueForFoo)

def variableValuesMap = ["foo": [null]]

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

then:
def error = thrown(NonNullableValueCoercedAsNullException)
error.message == "Variable 'foo' has an invalid value: Coerced Null value for NonNull type 'String!'"
}

// Note: use NullValue defined in Field when it exists,
Expand Down