Skip to content

Commit 468fc03

Browse files
Drew Olsonbbakerman
authored andcommitted
Use most specific possible field name in coercion errors
1 parent a29aebb commit 468fc03

2 files changed

Lines changed: 85 additions & 58 deletions

File tree

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

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,23 @@ public Map<String, Object> coerceArgumentValues(GraphQLSchema schema, List<Varia
6767
String variableName = variableDefinition.getName();
6868
GraphQLType variableType = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
6969

70-
try {
71-
// 3.e
72-
if (!variableValues.containsKey(variableName)) {
73-
Value defaultValue = variableDefinition.getDefaultValue();
74-
if (defaultValue != null) {
75-
// 3.e.i
76-
Object coercedValue = coerceValueAst(variableType, variableDefinition.getDefaultValue(), null);
77-
coercedValues.put(variableName, coercedValue);
78-
} else if (isNonNullType(variableType)) {
79-
// 3.e.ii
80-
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
81-
}
82-
} else {
83-
Object value = variableValues.get(variableName);
84-
// 3.f
85-
Object coercedValue = getVariableValue(variableDefinition, variableType, value);
86-
// 3.g
70+
// 3.e
71+
if (!variableValues.containsKey(variableName)) {
72+
Value defaultValue = variableDefinition.getDefaultValue();
73+
if (defaultValue != null) {
74+
// 3.e.i
75+
Object coercedValue = coerceValueAst(variableType, variableDefinition.getDefaultValue(), null);
8776
coercedValues.put(variableName, coercedValue);
77+
} else if (isNonNullType(variableType)) {
78+
// 3.e.ii
79+
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
8880
}
89-
} catch (CoercingParseValueException e) {
90-
throw new CoercingParseValueException(
91-
"Variable '" + variableName + "' has an invalid value. " + e.getMessage(),
92-
e.getCause(),
93-
variableDefinition.getSourceLocation()
94-
);
81+
} else {
82+
Object value = variableValues.get(variableName);
83+
// 3.f
84+
Object coercedValue = getVariableValue(variableDefinition, variableType, value);
85+
// 3.g
86+
coercedValues.put(variableName, coercedValue);
9587
}
9688
}
9789

@@ -104,7 +96,7 @@ private Object getVariableValue(VariableDefinition variableDefinition, GraphQLTy
10496
return coerceValueAst(variableType, variableDefinition.getDefaultValue(), null);
10597
}
10698

107-
return coerceValue(variableDefinition, variableType, value);
99+
return coerceValue(variableDefinition, variableDefinition.getName(), variableType, value);
108100
}
109101

110102
private boolean isNonNullType(GraphQLType variableType) {
@@ -147,36 +139,49 @@ private Map<String, Argument> argumentMap(List<Argument> arguments) {
147139

148140

149141
@SuppressWarnings("unchecked")
150-
private Object coerceValue(VariableDefinition variableDefinition, GraphQLType graphQLType, Object value) {
151-
if (graphQLType instanceof GraphQLNonNull) {
152-
Object returnValue = coerceValue(variableDefinition, ((GraphQLNonNull) graphQLType).getWrappedType(), value);
153-
if (returnValue == null) {
154-
throw new NonNullableValueCoercedAsNullException(variableDefinition, graphQLType);
142+
private Object coerceValue(VariableDefinition variableDefinition, String inputName, GraphQLType graphQLType, Object value) {
143+
try {
144+
if (graphQLType instanceof GraphQLNonNull) {
145+
Object returnValue =
146+
coerceValue(variableDefinition, inputName, ((GraphQLNonNull) graphQLType).getWrappedType(), value);
147+
if (returnValue == null) {
148+
throw new NonNullableValueCoercedAsNullException(variableDefinition, graphQLType);
149+
}
150+
return returnValue;
155151
}
156-
return returnValue;
157-
}
158152

159-
if (value == null) {
160-
return null;
161-
}
153+
if (value == null) {
154+
return null;
155+
}
162156

163-
if (graphQLType instanceof GraphQLScalarType) {
164-
return coerceValueForScalar((GraphQLScalarType) graphQLType, value);
165-
} else if (graphQLType instanceof GraphQLEnumType) {
166-
return coerceValueForEnum((GraphQLEnumType) graphQLType, value);
167-
} else if (graphQLType instanceof GraphQLList) {
168-
return coerceValueForList(variableDefinition, (GraphQLList) graphQLType, value);
169-
} else if (graphQLType instanceof GraphQLInputObjectType) {
170-
if (value instanceof Map) {
171-
return coerceValueForInputObjectType(variableDefinition, (GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
157+
if (graphQLType instanceof GraphQLScalarType) {
158+
return coerceValueForScalar((GraphQLScalarType) graphQLType, value);
159+
} else if (graphQLType instanceof GraphQLEnumType) {
160+
return coerceValueForEnum((GraphQLEnumType) graphQLType, value);
161+
} else if (graphQLType instanceof GraphQLList) {
162+
return coerceValueForList(variableDefinition, inputName, (GraphQLList) graphQLType, value);
163+
} else if (graphQLType instanceof GraphQLInputObjectType) {
164+
if (value instanceof Map) {
165+
return coerceValueForInputObjectType(variableDefinition, (GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
166+
} else {
167+
throw new CoercingParseValueException(
168+
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
169+
"'. Variables for input objects must be an instance of type 'Map'."
170+
);
171+
}
172172
} else {
173-
throw new CoercingParseValueException(
174-
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
175-
"'. Variables for input objects must be an instance of a 'Map'."
176-
);
173+
return assertShouldNeverHappen("unhandled type %s", graphQLType);
177174
}
178-
} else {
179-
return assertShouldNeverHappen("unhandled type %s", graphQLType);
175+
} catch (CoercingParseValueException e) {
176+
if (e.getLocations() != null) {
177+
throw e;
178+
}
179+
180+
throw new CoercingParseValueException(
181+
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
182+
e.getCause(),
183+
variableDefinition.getSourceLocation()
184+
);
180185
}
181186
}
182187

@@ -192,8 +197,11 @@ private Object coerceValueForInputObjectType(VariableDefinition variableDefiniti
192197

193198
for (GraphQLInputObjectField inputField : fields) {
194199
if (input.containsKey(inputField.getName()) || alwaysHasValue(inputField)) {
195-
Object value = coerceValue(variableDefinition, inputField.getType(), input.get(inputField.getName()));
196-
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
200+
Object value = coerceValue(variableDefinition,
201+
inputField.getName(),
202+
inputField.getType(),
203+
input.get(inputField.getName()));
204+
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
197205
}
198206
}
199207
return result;
@@ -212,15 +220,15 @@ private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value)
212220
return graphQLEnumType.getCoercing().parseValue(value);
213221
}
214222

215-
private List coerceValueForList(VariableDefinition variableDefinition, GraphQLList graphQLList, Object value) {
223+
private List coerceValueForList(VariableDefinition variableDefinition, String inputName, GraphQLList graphQLList, Object value) {
216224
if (value instanceof Iterable) {
217225
List<Object> result = new ArrayList<>();
218226
for (Object val : (Iterable) value) {
219-
result.add(coerceValue(variableDefinition, graphQLList.getWrappedType(), val));
227+
result.add(coerceValue(variableDefinition, inputName, graphQLList.getWrappedType(), val));
220228
}
221229
return result;
222230
} else {
223-
return Collections.singletonList(coerceValue(variableDefinition, graphQLList.getWrappedType(), value));
231+
return Collections.singletonList(coerceValue(variableDefinition, inputName, graphQLList.getWrappedType(), value));
224232
}
225233
}
226234

@@ -318,5 +326,4 @@ private Map<String, ObjectField> mapObjectValueFieldsByName(ObjectValue inputVal
318326
}
319327
return inputValueFieldsByName;
320328
}
321-
322329
}

‎src/test/groovy/graphql/Issue739.groovy‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Issue739 extends Specification {
4646
4747
input BarInput {
4848
baz: String!
49+
boom: Int
4950
}
5051
5152
interface Node {
@@ -77,20 +78,39 @@ class Issue739 extends Specification {
7778
def variables = ["input": 123]
7879

7980
ExecutionInput varInput = ExecutionInput.newExecutionInput()
81+
.query('query Bar($input: BarInput!) {bar(input: $input) {id}}')
82+
.variables(variables)
83+
.build()
84+
85+
ExecutionResult varResult = graphQL
86+
.executeAsync(varInput)
87+
.join()
88+
89+
then:
90+
varResult.data == null
91+
varResult.errors.size() == 1
92+
varResult.errors[0].errorType == ErrorType.ValidationError
93+
varResult.errors[0].message == "Variable 'input' has an invalid value. Expected type 'Map' but was 'Integer'." +
94+
" Variables for input objects must be an instance of type 'Map'.";
95+
varResult.errors[0].locations == [new SourceLocation(1, 11)]
96+
97+
when:
98+
variables = ["input": ["baz": "hi", "boom": "hi"]]
99+
100+
varInput = ExecutionInput.newExecutionInput()
80101
.query('query Bar($input: BarInput!) {bar(input: $input) {id}}')
81102
.variables(variables)
82103
.build()
83104

84-
ExecutionResult varResult = graphQL
105+
varResult = graphQL
85106
.executeAsync(varInput)
86107
.join()
87108

88109
then:
89110
varResult.data == null
90111
varResult.errors.size() == 1
91112
varResult.errors[0].errorType == ErrorType.ValidationError
92-
varResult.errors[0].message == "Variable 'input' has an invalid value. Expected type 'Map' but was 'Integer'." +
93-
" Variables for input objects must be an instance of a 'Map'.";
113+
varResult.errors[0].message == "Variable 'boom' has an invalid value. Expected type 'Int' but was 'String'."
94114
varResult.errors[0].locations == [new SourceLocation(1, 11)]
95115
}
96116
}

0 commit comments

Comments
 (0)