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
10 changes: 8 additions & 2 deletions src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ private Object getValueByName(Object value) {
}

private Object getNameByValue(Object value) {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
if (value == null) {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (valueDefinition.getValue() == null) return valueDefinition.getName();
}
} else {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
Expand Down Expand Up @@ -290,6 +291,14 @@ GraphQLSchema createSchema() {

.build();


GraphQLEnumType enumDayType = GraphQLEnumType.newEnum()
.name("Day")
.value("MONDAY")
.value("TUESDAY")
.description("Day of the week")
.build();

GraphQLObjectType queryType = GraphQLObjectType.newObject()
.name("StringQuery")
.field(GraphQLFieldDefinition.newFieldDefinition()
Expand All @@ -304,6 +313,15 @@ GraphQLSchema createSchema() {
public Object get(DataFetchingEnvironment env) {return env.getArgument("value");}
})
.build())
.name("EnumQuery")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("nullEnum")
.type(enumDayType)
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment env) {return null;}
})
.build())
.build();
return GraphQLSchema.newSchema()
.query(queryType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ class GraphqlExecutionSpec extends Specification {

}

def "Legal null value for enum"() {

given:
String query =
"{ nullEnum }";

Map<String, Object> expected = mapOf(
"nullEnum", null);

expect:
runTest(query, expected);

}

def "Illegal null value for primitives"() {

given:
Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/graphql/schema/GraphQLEnumTypeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ class GraphQLEnumTypeTest extends Specification {
enumType.getCoercing().serialize(12) == null
}

def "serialize returns NULL for null and null being an known value"() {
setup:
def enumType = newEnum().name("TestEnum")
.value("NAME", 42)
.value("NULL", null)
.build();
expect:
enumType.getCoercing().serialize(null) == "NULL"
}

def "serialize returns null for null value "() {
expect:
enumType.getCoercing().serialize(null) == null
}

def "duplicate value definition fails"() {
when:
newEnum().name("AnotherTestEnum")
Expand Down