Skip to content

Commit cfe9049

Browse files
bbakermanandimarek
authored andcommitted
graphql-java#427 more tests and a specific exception for coerced null values
1 parent 5934af1 commit cfe9049

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package graphql.execution;
2+
3+
import graphql.GraphQLException;
4+
import graphql.schema.GraphQLType;
5+
import graphql.schema.GraphQLTypeUtil;
6+
7+
/**
8+
* This is thrown if a non nullable value is coerced to a null value
9+
*/
10+
public class NonNullableValueCoercedAsNullException extends GraphQLException {
11+
12+
public NonNullableValueCoercedAsNullException(GraphQLType graphQLType) {
13+
super(String.format("Null value for NonNull type '%s", GraphQLTypeUtil.getUnwrappedTypeName(graphQLType)));
14+
}
15+
16+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private Object coerceValue(GraphQLType graphQLType, Object value) {
9898
if (graphQLType instanceof GraphQLNonNull) {
9999
Object returnValue = coerceValue(((GraphQLNonNull) graphQLType).getWrappedType(), value);
100100
if (returnValue == null) {
101-
throw new GraphQLException("Null value for NonNull type " + graphQLType);
101+
throw new NonNullableValueCoercedAsNullException(graphQLType);
102102
}
103103
return returnValue;
104104
}
@@ -240,8 +240,7 @@ private Object coerceValueAstForInputObject(GraphQLInputObjectType type, ObjectV
240240

241241
private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) {
242242
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());
243+
throw new NonNullableValueCoercedAsNullException(inputTypeField.getType());
245244
}
246245
}
247246

src/test/groovy/graphql/NullValueSupportTest.groovy

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql
22

3+
import graphql.execution.NonNullableValueCoercedAsNullException
34
import graphql.validation.ValidationError
45
import graphql.validation.ValidationErrorType
56
import spock.lang.Specification
@@ -179,13 +180,12 @@ class NullValueSupportTest extends Specification {
179180
GraphQL.newGraphQL(schema).build().execute(queryStr, "mutate", "ctx", variables)
180181

181182
then:
182-
def err = thrown(GraphQLException)
183-
assert err.message.startsWith(expectedExceptionMsg): "Expected GraphQLError msg in ${testCase} : ${err.message}"
183+
thrown(expectedException)
184184

185185

186186
where:
187187

188-
testCase | queryStr | variables || expectedExceptionMsg
188+
testCase | queryStr | variables || expectedException
189189

190190
// ------------------------------
191191
'G' | '''
@@ -194,7 +194,7 @@ class NullValueSupportTest extends Specification {
194194
a
195195
}
196196
}
197-
''' | [:] || "Null value for NonNull type GraphQLNonNull"
197+
''' | [:] || NonNullableValueCoercedAsNullException
198198

199199
// ------------------------------
200200
'H' | '''
@@ -203,8 +203,47 @@ class NullValueSupportTest extends Specification {
203203
a
204204
}
205205
}
206-
''' | [var: null] || "Null value for NonNull type GraphQLNonNull"
206+
''' | [var: null] || NonNullableValueCoercedAsNullException
207207

208208
}
209209

210+
def "nulls in literal places are supported in general"() {
211+
212+
def fetcher = new CapturingDataFetcher()
213+
214+
def schema = TestUtil.schema("""
215+
schema { query : Query }
216+
217+
type Query {
218+
list(arg : [String]) : Int
219+
scalar(arg : String) : Int
220+
complex(arg : ComplexInputObject) : Int
221+
}
222+
223+
input ComplexInputObject {
224+
a: String
225+
b: Int!
226+
}
227+
228+
""",
229+
["Query": [
230+
"list" : fetcher,
231+
"scalar" : fetcher,
232+
"complex": fetcher,
233+
]])
234+
235+
when:
236+
def result = GraphQL.newGraphQL(schema).build().execute(queryStr, null, "ctx", [:])
237+
assert result.errors.isEmpty(): "Unexpected query errors : ${result.errors}"
238+
239+
then:
240+
fetcher.args == expectedArgs
241+
242+
where:
243+
queryStr | expectedArgs
244+
'''{ list(arg : ["abc", null, "xyz"]) }''' | [arg: ["abc", null, "xyz"]]
245+
'''{ scalar(arg : null) }''' | [arg: null]
246+
'''{ complex(arg : null) }''' | [arg: null]
247+
248+
}
210249
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ class ValuesResolverTest extends Specification {
251251

252252
def "getArgumentValues: resolves enum literals"() {
253253
given: "the ast"
254-
EnumValue enumValue1 = new EnumValue("PLUTO");
255-
EnumValue enumValue2 = new EnumValue("MARS");
254+
EnumValue enumValue1 = new EnumValue("PLUTO")
255+
EnumValue enumValue2 = new EnumValue("MARS")
256256
def argument1 = new Argument("arg1", enumValue1)
257257
def argument2 = new Argument("arg2", enumValue2)
258258

@@ -400,9 +400,7 @@ class ValuesResolverTest extends Specification {
400400
where:
401401
InputValue || outputValue
402402
[foo: "added", bar: null] || [foo: "added", bar: null]
403-
404-
// later this will be true once we apply missing value code
405-
//[foo: "added"] || [foo: "added"]
403+
[foo: "added"] || [foo: "added"]
406404
}
407405

408406
}

0 commit comments

Comments
 (0)