File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -45,8 +45,14 @@ private Object getValueByName(Object value) {
4545 }
4646
4747 private Object getNameByValue (Object value ) {
48- for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap .values ()) {
49- if (value .equals (valueDefinition .getValue ())) return valueDefinition .getName ();
48+ if (value == null ) {
49+ for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap .values ()) {
50+ if (valueDefinition .getValue () == null ) return valueDefinition .getName ();
51+ }
52+ } else {
53+ for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap .values ()) {
54+ if (value .equals (valueDefinition .getValue ())) return valueDefinition .getName ();
55+ }
5056 }
5157 return null ;
5258 }
Original file line number Diff line number Diff line change 44import graphql .schema .DataFetcher ;
55import graphql .schema .DataFetchingEnvironment ;
66import graphql .schema .GraphQLArgument ;
7+ import graphql .schema .GraphQLEnumType ;
78import graphql .schema .GraphQLFieldDefinition ;
89import graphql .schema .GraphQLList ;
910import graphql .schema .GraphQLNonNull ;
@@ -290,6 +291,14 @@ 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+
293302 GraphQLObjectType queryType = GraphQLObjectType .newObject ()
294303 .name ("StringQuery" )
295304 .field (GraphQLFieldDefinition .newFieldDefinition ()
@@ -304,6 +313,15 @@ GraphQLSchema createSchema() {
304313 public Object get (DataFetchingEnvironment env ) {return env .getArgument ("value" );}
305314 })
306315 .build ())
316+ .name ("EnumQuery" )
317+ .field (GraphQLFieldDefinition .newFieldDefinition ()
318+ .name ("nullEnum" )
319+ .type (enumDayType )
320+ .dataFetcher (new DataFetcher () {
321+ @ Override
322+ public Object get (DataFetchingEnvironment env ) {return null ;}
323+ })
324+ .build ())
307325 .build ();
308326 return GraphQLSchema .newSchema ()
309327 .query (queryType )
Original file line number Diff line number Diff 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+ " { nullEnum }" ;
245+
246+ Map<String , Object > expected = mapOf(
247+ " nullEnum" , null );
248+
249+ expect :
250+ runTest(query, expected);
251+
252+ }
253+
240254 def " Illegal null value for primitives" () {
241255
242256 given :
Original file line number Diff line number Diff line change @@ -38,6 +38,21 @@ class GraphQLEnumTypeTest extends Specification {
3838 enumType. getCoercing(). serialize(12 ) == null
3939 }
4040
41+ def " serialize returns NULL for null and null being an known value" () {
42+ setup :
43+ def enumType = newEnum(). name(" TestEnum" )
44+ .value(" NAME" , 42 )
45+ .value(" NULL" , null )
46+ .build();
47+ expect :
48+ enumType. getCoercing(). serialize(null ) == " NULL"
49+ }
50+
51+ def " serialize returns null for null value " () {
52+ expect :
53+ enumType. getCoercing(). serialize(null ) == null
54+ }
55+
4156 def " duplicate value definition fails" () {
4257 when :
4358 newEnum(). name(" AnotherTestEnum" )
You can’t perform that action at this time.
0 commit comments