Skip to content

Commit deedb88

Browse files
andimarekbbakerman
authored andcommitted
enum coercing throws correct exception
enum serialization exceptions are handled correctly
1 parent b205cd8 commit deedb88

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
145145
} else if (fieldType instanceof GraphQLScalarType) {
146146
return completeValueForScalar((GraphQLScalarType) fieldType, parameters, result, executionContext);
147147
} else if (fieldType instanceof GraphQLEnumType) {
148-
return completeValueForEnum((GraphQLEnumType) fieldType, parameters, result);
148+
return completeValueForEnum((GraphQLEnumType) fieldType, parameters, result, executionContext);
149149
}
150150

151151

@@ -221,8 +221,14 @@ protected GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params)
221221
return result;
222222
}
223223

224-
protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, ExecutionStrategyParameters parameters, Object result) {
225-
Object serialized = enumType.getCoercing().serialize(result);
224+
protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, ExecutionStrategyParameters parameters, Object result, ExecutionContext context) {
225+
Object serialized;
226+
try {
227+
serialized = enumType.getCoercing().serialize(result);
228+
} catch (CoercingSerializeException e) {
229+
context.addError(new SerializationError(e));
230+
serialized = null;
231+
}
226232
serialized = parameters.nonNullFieldValidator().validate(serialized);
227233
return new ExecutionResultImpl(serialized, null);
228234
}

src/main/java/graphql/schema/GraphQLEnumType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
import graphql.AssertException;
5-
import graphql.GraphQLException;
65
import graphql.Internal;
76
import graphql.PublicApi;
87
import graphql.language.EnumTypeDefinition;
@@ -80,14 +79,14 @@ private void buildMap(List<GraphQLEnumValueDefinition> values) {
8079
private Object getValueByName(Object value) {
8180
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
8281
if (enumValueDefinition != null) return enumValueDefinition.getValue();
83-
throw new GraphQLException("Invalid input for Enum '" + name + "'. No value found for name " + value.toString());
82+
throw new CoercingParseValueException("Invalid input for Enum '" + name + "'. No value found for name '" + value.toString() + "'");
8483
}
8584

8685
private Object getNameByValue(Object value) {
8786
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
8887
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
8988
}
90-
throw new GraphQLException("Invalid input for Enum '" + name + "'. Unknown value " + value);
89+
throw new CoercingSerializeException("Invalid input for Enum '" + name + "'. Unknown value '" + value + "'");
9190
}
9291

9392

src/test/groovy/graphql/execution/ExecutionStrategyTest.groovy

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import graphql.Scalars
55
import graphql.SerializationError
66
import graphql.language.Field
77
import graphql.schema.Coercing
8+
import graphql.schema.GraphQLEnumType
89
import graphql.schema.GraphQLList
910
import graphql.schema.GraphQLScalarType
1011
import spock.lang.Specification
1112

1213
import static ExecutionStrategyParameters.newParameters
14+
import static graphql.schema.GraphQLEnumType.newEnum
1315
import static graphql.schema.GraphQLNonNull.nonNull
1416

1517
@SuppressWarnings("GroovyPointlessBoolean")
@@ -70,7 +72,6 @@ class ExecutionStrategyTest extends Specification {
7072
executionResult.data == ["test"]
7173
}
7274

73-
// this is wrong: we should return null with an error
7475
def "completing value with serializing throwing exception"() {
7576
given:
7677
ExecutionContext executionContext = buildContext()
@@ -96,6 +97,31 @@ class ExecutionStrategyTest extends Specification {
9697

9798
}
9899

100+
def "completing enum with serializing throwing exception"() {
101+
given:
102+
ExecutionContext executionContext = buildContext()
103+
GraphQLEnumType enumType = newEnum().name("Enum").value("value").build()
104+
def typeInfo = TypeInfo.newTypeInfo().type(enumType).build()
105+
NonNullableFieldValidator nullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo)
106+
String result = "not a enum number"
107+
108+
def parameters = newParameters()
109+
.typeInfo(typeInfo)
110+
.source(result)
111+
.nonNullFieldValidator(nullableFieldValidator)
112+
.fields(["dummy": []])
113+
.build()
114+
115+
when:
116+
def executionResult = executionStrategy.completeValue(executionContext, parameters, [new Field()])
117+
118+
then:
119+
executionResult.data == null
120+
executionContext.errors.size() == 1
121+
executionContext.errors[0] instanceof SerializationError
122+
123+
}
124+
99125
def "completing a scalar null value for a non null type throws an exception"() {
100126

101127
GraphQLScalarType NullProducingScalar = new GraphQLScalarType("Custom", "It Can Produce Nulls", new Coercing<Double, Double>() {

src/test/groovy/graphql/schema/GraphQLEnumTypeTest.groovy

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package graphql.schema
22

33
import graphql.AssertException
4-
import graphql.GraphQLException
54
import graphql.language.EnumValue
65
import graphql.language.StringValue
76
import spock.lang.Specification
@@ -23,7 +22,7 @@ class GraphQLEnumTypeTest extends Specification {
2322
enumType.getCoercing().parseValue("UNKNOWN")
2423

2524
then:
26-
thrown(GraphQLException)
25+
thrown(CoercingParseValueException)
2726
}
2827

2928

@@ -41,7 +40,7 @@ class GraphQLEnumTypeTest extends Specification {
4140
when:
4241
enumType.getCoercing().serialize(12)
4342
then:
44-
thrown(GraphQLException)
43+
thrown(CoercingSerializeException)
4544
}
4645

4746

0 commit comments

Comments
 (0)