Skip to content

Commit ee25337

Browse files
committed
quick fix for parsing enum variables inputs
adds coerceValue method, but currently only used for enums
1 parent acc0495 commit ee25337

5 files changed

Lines changed: 70 additions & 4 deletions

File tree

src/main/java/graphql/Scalars.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public Object coerce(Object input) {
2323
}
2424
}
2525

26+
@Override
27+
public Object coerceValue(Object input) {
28+
return null;
29+
}
30+
2631
@Override
2732
public Object coerceLiteral(Object input) {
2833
if (!(input instanceof IntValue)) return null;
@@ -45,6 +50,11 @@ public Object coerce(Object input) {
4550
}
4651
}
4752

53+
@Override
54+
public Object coerceValue(Object input) {
55+
return null;
56+
}
57+
4858
@Override
4959
public Object coerceLiteral(Object input) {
5060
if (!(input instanceof StringValue)) return null;
@@ -64,6 +74,11 @@ public Object coerce(Object input) {
6474
}
6575
}
6676

77+
@Override
78+
public Object coerceValue(Object input) {
79+
return null;
80+
}
81+
6782
@Override
6883
public Object coerceLiteral(Object input) {
6984
return ((FloatValue) input).getValue().floatValue();
@@ -76,6 +91,11 @@ public Object coerce(Object input) {
7691
return input == null ? null : input.toString();
7792
}
7893

94+
@Override
95+
public Object coerceValue(Object input) {
96+
return null;
97+
}
98+
7999
@Override
80100
public Object coerceLiteral(Object input) {
81101
if (!(input instanceof StringValue)) return null;
@@ -98,6 +118,11 @@ public Object coerce(Object input) {
98118
}
99119
}
100120

121+
@Override
122+
public Object coerceValue(Object input) {
123+
return null;
124+
}
125+
101126
@Override
102127
public Object coerceLiteral(Object input) {
103128
if (!(input instanceof BooleanValue)) return null;
@@ -116,6 +141,11 @@ public Object coerce(Object input) {
116141
throw new GraphQLException();
117142
}
118143

144+
@Override
145+
public Object coerceValue(Object input) {
146+
return null;
147+
}
148+
119149
@Override
120150
public Object coerceLiteral(Object input) {
121151
if (!(input instanceof StringValue)) return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object
9393
}
9494

9595
private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value) {
96-
return graphQLEnumType.getCoercing().coerce(value);
96+
return graphQLEnumType.getCoercing().coerceValue(value);
9797
}
9898

9999
private List coerceValueForList(GraphQLList graphQLList, Object value) {

src/main/java/graphql/schema/Coercing.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public interface Coercing {
66

77
Object coerce(Object input);
88

9+
Object coerceValue(Object input);
10+
911
/**
1012
* @param input
1113
* @return return null if not valid

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@ public Object coerce(Object input) {
2323
return getNameByValue(input);
2424
}
2525

26+
@Override
27+
public Object coerceValue(Object input) {
28+
return getValueByValue(input);
29+
}
30+
2631
@Override
2732
public Object coerceLiteral(Object input) {
2833
if (!(input instanceof EnumValue)) return null;
2934
EnumValue enumValue = (EnumValue) input;
3035
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(enumValue.getName());
3136
if (enumValueDefinition.getValue() != null) return enumValueDefinition.getValue();
32-
return enumValueDefinition.getName();
37+
return enumValueDefinition.getValue();
3338
}
3439
};
3540

41+
private Object getValueByValue(Object value) {
42+
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
43+
if (value.equals(valueDefinition.getValue())) return valueDefinition.getValue();
44+
}
45+
throw new GraphQLException("");
46+
}
47+
3648
private Object getNameByValue(Object value) {
3749
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
3850
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();

src/test/groovy/graphql/execution/ValuesResolverTest.groovy

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package graphql.execution
33
import graphql.TestUtil
44
import graphql.language.*
55
import graphql.schema.GraphQLArgument
6-
import graphql.schema.GraphQLEnumType
76
import graphql.schema.GraphQLList
87
import spock.lang.Specification
98
import spock.lang.Unroll
109

1110
import static graphql.Scalars.*
11+
import static graphql.schema.GraphQLEnumType.newEnum
1212
import static graphql.schema.GraphQLInputObjectField.newInputObjectField
1313
import static graphql.schema.GraphQLInputObjectType.newInputObject
1414

@@ -136,7 +136,7 @@ class ValuesResolverTest extends Specification {
136136
def argument2 = new Argument("arg2", enumValue2)
137137

138138
and: "the schema"
139-
def enumType = GraphQLEnumType.newEnum()
139+
def enumType = newEnum()
140140
.name("EnumType")
141141
.value("PLUTO")
142142
.value("MARS", "mars")
@@ -182,4 +182,26 @@ class ValuesResolverTest extends Specification {
182182
values['arg'] == ['world']
183183

184184
}
185+
186+
def "enum as variable input"() {
187+
given:
188+
def enumDef = newEnum()
189+
.name("Test")
190+
.value("A_TEST")
191+
.value("VALUE_TEST", 1)
192+
.build()
193+
194+
def schema = TestUtil.schemaWithInputType(enumDef)
195+
VariableDefinition variableDefinition = new VariableDefinition("variable", new TypeName("Test"))
196+
197+
when:
198+
def resolvedValues = resolver.getVariableValues(schema, [variableDefinition], [variable: inputValue])
199+
then:
200+
resolvedValues['variable'] == outputValue
201+
where:
202+
inputValue || outputValue
203+
"A_TEST" || "A_TEST"
204+
1 || 1
205+
206+
}
185207
}

0 commit comments

Comments
 (0)