Skip to content

Commit 359a8e6

Browse files
author
Antoine Boyer
committed
Fix NPE when coercing null enum value in BatchedExecutionStrategy
1 parent 4a908db commit 359a8e6

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private GraphQLObjectType getGraphQLObjectType(GraphQLType fieldType, Object val
209209
private void handlePrimitives(List<GraphQLExecutionNodeValue> values, String fieldName,
210210
GraphQLType type) {
211211
for (GraphQLExecutionNodeValue value : values) {
212-
Object coercedValue = coerce(type, value.getValue());
212+
Object coercedValue = value.getValue() == null ? null : coerce(type, value.getValue());
213213
value.getResultContainer().putResult(fieldName, coercedValue);
214214
}
215215
}

src/test/groovy/graphql/execution/batched/FunWithStringsSchemaFactory.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import graphql.schema.DataFetcher;
55
import graphql.schema.DataFetchingEnvironment;
66
import graphql.schema.GraphQLArgument;
7+
import graphql.schema.GraphQLEnumType;
78
import graphql.schema.GraphQLFieldDefinition;
89
import graphql.schema.GraphQLList;
910
import graphql.schema.GraphQLNonNull;
@@ -290,6 +291,23 @@ GraphQLSchema createSchema() {
290291

291292
.build();
292293

294+
295+
GraphQLEnumType enumDayType = GraphQLEnumType.newEnum()
296+
.name("Day")
297+
.value("MONDAY")
298+
.value("TUESDAY")
299+
.description("Day of the week")
300+
.build();
301+
302+
GraphQLObjectType enumObjectType = GraphQLObjectType.newObject()
303+
.name("EnumObject")
304+
.field(GraphQLFieldDefinition.newFieldDefinition()
305+
.name("value")
306+
.type(enumDayType)
307+
.dataFetcher(stringObjectValueFetcher)
308+
.build())
309+
.build();
310+
293311
GraphQLObjectType queryType = GraphQLObjectType.newObject()
294312
.name("StringQuery")
295313
.field(GraphQLFieldDefinition.newFieldDefinition()
@@ -304,6 +322,19 @@ GraphQLSchema createSchema() {
304322
public Object get(DataFetchingEnvironment env) {return env.getArgument("value");}
305323
})
306324
.build())
325+
.name("EnumQuery")
326+
.field(GraphQLFieldDefinition.newFieldDefinition()
327+
.name("enum")
328+
.type(enumObjectType)
329+
.argument(GraphQLArgument.newArgument()
330+
.name("value")
331+
.type(Scalars.GraphQLString)
332+
.build())
333+
.dataFetcher(new DataFetcher() {
334+
@Override
335+
public Object get(DataFetchingEnvironment env) {return env.getArgument("value");}
336+
})
337+
.build())
307338
.build();
308339
return GraphQLSchema.newSchema()
309340
.query(queryType)

src/test/groovy/graphql/execution/batched/GraphqlExecutionSpec.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ class GraphqlExecutionSpec extends Specification {
237237

238238
}
239239

240+
def "Legal null value for enum"() {
241+
242+
given:
243+
String query =
244+
"{ enum(value: \"null\") { value } }";
245+
246+
Map<String, Object> expected = mapOf(
247+
"enum", mapOf("value", null));
248+
249+
expect:
250+
runTest(query, expected);
251+
252+
}
253+
240254
def "Illegal null value for primitives"() {
241255

242256
given:

0 commit comments

Comments
 (0)