Skip to content

Commit 973205f

Browse files
committed
Merge branch 'master' into remove-undeclared-directives
2 parents e9cfa8d + 8a5fe91 commit 973205f

File tree

8 files changed

+59
-56
lines changed

8 files changed

+59
-56
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionCon
607607
protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLEnumType enumType, Object result) {
608608
Object serialized;
609609
try {
610-
serialized = enumType.getCoercing().serialize(result);
610+
serialized = enumType.serialize(result);
611611
} catch (CoercingSerializeException e) {
612612
serialized = handleCoercionProblem(executionContext, parameters, e);
613613
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object
208208
}
209209

210210
private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value) {
211-
return graphQLEnumType.getCoercing().parseValue(value);
211+
return graphQLEnumType.parseValue(value);
212212
}
213213

214214
private List coerceValueForList(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, String inputName, GraphQLList graphQLList, Object value) {
@@ -240,7 +240,7 @@ private Object coerceValueAst(GraphqlFieldVisibility fieldVisibility, GraphQLTyp
240240
return coerceValueAstForInputObject(fieldVisibility, (GraphQLInputObjectType) type, (ObjectValue) inputValue, variables);
241241
}
242242
if (type instanceof GraphQLEnumType) {
243-
return parseLiteral(inputValue, ((GraphQLEnumType) type).getCoercing(), variables);
243+
return ((GraphQLEnumType) type).parseLiteral(inputValue);
244244
}
245245
if (isList(type)) {
246246
return coerceValueAstForList(fieldVisibility, (GraphQLList) type, inputValue, variables);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ private Object coerce(GraphQLType type, Object value) {
468468
return null;
469469
}
470470
if (type instanceof GraphQLEnumType) {
471-
return ((GraphQLEnumType) type).getCoercing().serialize(value);
471+
return ((GraphQLEnumType) type).serialize(value);
472472
} else {
473473
return ((GraphQLScalarType) type).getCoercing().serialize(value);
474474
}

src/main/java/graphql/execution/nextgen/FetchedValueAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private FetchedValueAnalysis analyzeEnumValue(FetchedValue fetchedValue, Object
189189
}
190190
Object serialized;
191191
try {
192-
serialized = enumType.getCoercing().serialize(toAnalyze);
192+
serialized = enumType.serialize(toAnalyze);
193193
} catch (CoercingSerializeException e) {
194194
SerializationError error = new SerializationError(executionInfo.getPath(), e);
195195
return newFetchedValueAnalysis(SCALAR)

src/main/java/graphql/language/AstValueHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private static Object serialize(GraphQLType type, Object value) {
198198
if (type instanceof GraphQLScalarType) {
199199
return ((GraphQLScalarType) type).getCoercing().serialize(value);
200200
} else {
201-
return ((GraphQLEnumType) type).getCoercing().serialize(value);
201+
return ((GraphQLEnumType) type).serialize(value);
202202
}
203203
}
204204

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,6 @@ public class GraphQLEnumType implements GraphQLNamedInputType, GraphQLNamedOutpu
4444
public static final String CHILD_VALUES = "values";
4545
public static final String CHILD_DIRECTIVES = "directives";
4646

47-
private final Coercing coercing = new Coercing() {
48-
@Override
49-
public Object serialize(Object input) {
50-
return getNameByValue(input);
51-
}
52-
53-
@Override
54-
public Object parseValue(Object input) {
55-
return getValueByName(input);
56-
}
57-
58-
private String typeName(Object input) {
59-
if (input == null) {
60-
return "null";
61-
}
62-
return input.getClass().getSimpleName();
63-
}
64-
65-
@Override
66-
public Object parseLiteral(Object input) {
67-
if (!(input instanceof EnumValue)) {
68-
throw new CoercingParseLiteralException(
69-
"Expected AST type 'EnumValue' but was '" + typeName(input) + "'."
70-
);
71-
}
72-
EnumValue enumValue = (EnumValue) input;
73-
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(enumValue.getName());
74-
if (enumValueDefinition == null) {
75-
throw new CoercingParseLiteralException(
76-
"Expected enum literal value not in allowable values - '" + String.valueOf(input) + "'."
77-
);
78-
}
79-
return enumValueDefinition.getValue();
80-
}
81-
};
8247

8348

8449
/**
@@ -121,6 +86,40 @@ private GraphQLEnumType(String name, String description, List<GraphQLEnumValueDe
12186
buildMap(values);
12287
}
12388

89+
@Internal
90+
public Object serialize(Object input) {
91+
return getNameByValue(input);
92+
}
93+
94+
@Internal
95+
public Object parseValue(Object input) {
96+
return getValueByName(input);
97+
}
98+
99+
private String typeName(Object input) {
100+
if (input == null) {
101+
return "null";
102+
}
103+
return input.getClass().getSimpleName();
104+
}
105+
106+
@Internal
107+
public Object parseLiteral(Object input) {
108+
if (!(input instanceof EnumValue)) {
109+
throw new CoercingParseLiteralException(
110+
"Expected AST type 'EnumValue' but was '" + typeName(input) + "'."
111+
);
112+
}
113+
EnumValue enumValue = (EnumValue) input;
114+
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(enumValue.getName());
115+
if (enumValueDefinition == null) {
116+
throw new CoercingParseLiteralException(
117+
"Expected enum literal value not in allowable values - '" + String.valueOf(input) + "'."
118+
);
119+
}
120+
return enumValueDefinition.getValue();
121+
}
122+
124123
public List<GraphQLEnumValueDefinition> getValues() {
125124
return new ArrayList<>(valueDefinitionMap.values());
126125
}
@@ -182,10 +181,6 @@ public String getDescription() {
182181
return description;
183182
}
184183

185-
public Coercing getCoercing() {
186-
return coercing;
187-
}
188-
189184
public EnumTypeDefinition getDefinition() {
190185
return definition;
191186
}

src/main/java/graphql/validation/ValidationUtil.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean isValidLiteralValue(Value<?> value, GraphQLType type, GraphQLSche
9393
return !invalid.isPresent();
9494
}
9595
if (type instanceof GraphQLEnumType) {
96-
Optional<GraphQLError> invalid = parseLiteral(value, ((GraphQLEnumType) type).getCoercing());
96+
Optional<GraphQLError> invalid = parseLiteralEnum(value,(GraphQLEnumType) type);
9797
invalid.ifPresent(graphQLError -> handleEnumError(value, (GraphQLEnumType) type, graphQLError));
9898
return !invalid.isPresent();
9999
}
@@ -104,6 +104,14 @@ public boolean isValidLiteralValue(Value<?> value, GraphQLType type, GraphQLSche
104104
return type instanceof GraphQLInputObjectType && isValidLiteralValue(value, (GraphQLInputObjectType) type, schema);
105105

106106
}
107+
private Optional<GraphQLError> parseLiteralEnum(Value<?> value, GraphQLEnumType graphQLEnumType) {
108+
try {
109+
graphQLEnumType.parseLiteral(value);
110+
return Optional.empty();
111+
} catch (CoercingParseLiteralException e) {
112+
return Optional.of(e);
113+
}
114+
}
107115

108116
private Optional<GraphQLError> parseLiteral(Value<?> value, Coercing<?,?> coercing) {
109117
try {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GraphQLEnumTypeTest extends Specification {
1919

2020
def "parse throws exception for unknown value"() {
2121
when:
22-
enumType.getCoercing().parseValue("UNKNOWN")
22+
enumType.parseValue("UNKNOWN")
2323

2424
then:
2525
thrown(CoercingParseValueException)
@@ -28,39 +28,39 @@ class GraphQLEnumTypeTest extends Specification {
2828

2929
def "parse value return value for the name"() {
3030
expect:
31-
enumType.getCoercing().parseValue("NAME") == 42
31+
enumType.parseValue("NAME") == 42
3232
}
3333

3434
def "serialize returns name for value"() {
3535
expect:
36-
enumType.getCoercing().serialize(42) == "NAME"
36+
enumType.serialize(42) == "NAME"
3737
}
3838

3939
def "serialize throws exception for unknown value"() {
4040
when:
41-
enumType.getCoercing().serialize(12)
41+
enumType.serialize(12)
4242
then:
4343
thrown(CoercingSerializeException)
4444
}
4545

4646

4747
def "parseLiteral return null for invalid input"() {
4848
when:
49-
enumType.getCoercing().parseLiteral(StringValue.newStringValue("foo").build())
49+
enumType.parseLiteral(StringValue.newStringValue("foo").build())
5050
then:
5151
thrown(CoercingParseLiteralException)
5252
}
5353

5454
def "parseLiteral return null for invalid enum name"() {
5555
when:
56-
enumType.getCoercing().parseLiteral(EnumValue.newEnumValue("NOT_NAME").build())
56+
enumType.parseLiteral(EnumValue.newEnumValue("NOT_NAME").build())
5757
then:
5858
thrown(CoercingParseLiteralException)
5959
}
6060

6161
def "parseLiteral returns value for 'NAME'"() {
6262
expect:
63-
enumType.getCoercing().parseLiteral(EnumValue.newEnumValue("NAME").build()) == 42
63+
enumType.parseLiteral(EnumValue.newEnumValue("NAME").build()) == 42
6464
}
6565

6666

@@ -96,7 +96,7 @@ class GraphQLEnumTypeTest extends Specification {
9696
.build()
9797

9898
when:
99-
def serialized = enumType.coercing.serialize(Episode.EMPIRE)
99+
def serialized = enumType.serialize(Episode.EMPIRE)
100100

101101
then:
102102
serialized == "EMPIRE"
@@ -111,7 +111,7 @@ class GraphQLEnumTypeTest extends Specification {
111111
.build()
112112

113113
when:
114-
def serialized = enumType.coercing.serialize(Episode.NEWHOPE)
114+
def serialized = enumType.serialize(Episode.NEWHOPE)
115115

116116
then:
117117
serialized == "NEWHOPE"
@@ -128,7 +128,7 @@ class GraphQLEnumTypeTest extends Specification {
128128
String stringInput = Episode.NEWHOPE.toString()
129129

130130
when:
131-
def serialized = enumType.coercing.serialize(stringInput)
131+
def serialized = enumType.serialize(stringInput)
132132

133133
then:
134134
serialized == "NEWHOPE"

0 commit comments

Comments
 (0)