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 @@ -6,6 +6,7 @@
import graphql.PublicApi;
import graphql.language.SourceLocation;
import graphql.language.VariableDefinition;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeUtil;
Expand Down Expand Up @@ -59,6 +60,10 @@ public NonNullableValueCoercedAsNullException(GraphQLInputObjectField inputTypeF
this.path = path;
}

public NonNullableValueCoercedAsNullException(GraphQLArgument graphQLArgument) {
super(format("Argument '%s' has coerced Null value for NonNull type '%s'", graphQLArgument.getName(), graphQLArgument.getType()));
}

@Override
public List<SourceLocation> getLocations() {
return sourceLocations;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private Map<String, Object> getArgumentValuesImpl(GraphQLCodeRegistry codeRegist
argumentType);
coercedValues.put(argumentName, coercedDefaultValue);
} else if (isNonNull(argumentType) && (!hasValue || isNullValue(value))) {
throw new RuntimeException();
throw new NonNullableValueCoercedAsNullException(argumentDefinition);
} else if (hasValue) {
if (isNullValue(value)) {
coercedValues.put(argumentName, value);
Expand Down
18 changes: 18 additions & 0 deletions src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,22 @@ class ValuesResolverTest extends Specification {
then:
values['arg'] == null
}

def "argument of Non-Nullable type and with null coerced value throws error"() {
given:
def inputObjectType = newInputObject()
.name("inputObject")
.build()

def fieldArgument = newArgument().name("arg").type(nonNull(inputObjectType)).build()
def argument = new Argument("arg", new VariableReference("var"))

when:
def variables = ["var": null]
resolver.getArgumentValues([fieldArgument], [argument], variables)

then:
def error = thrown(NonNullableValueCoercedAsNullException)
error.message == "Argument 'arg' has coerced Null value for NonNull type 'inputObject!'"
}
}