Skip to content

Commit 18f1f79

Browse files
Simplify coercion algorithm in getVariableValues/getArgumentValues (#2050)
1 parent bb07ecc commit 18f1f79

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

src/execution/values.js

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import find from '../polyfills/find';
44
import { GraphQLError } from '../error/GraphQLError';
55
import inspect from '../jsutils/inspect';
6-
import invariant from '../jsutils/invariant';
76
import keyMap from '../jsutils/keyMap';
87
import { coerceValue } from '../utilities/coerceValue';
98
import { typeFromAST } from '../utilities/typeFromAST';
@@ -82,25 +81,19 @@ export function getVariableValues(
8281
),
8382
);
8483
} else if (hasValue) {
85-
if (value === null) {
86-
// If the explicit value `null` was provided, an entry in the coerced
87-
// values must exist as the value `null`.
88-
coercedValues[varName] = null;
89-
} else {
90-
// Otherwise, a non-null value was provided, coerce it to the expected
91-
// type or report an error if coercion fails.
92-
const coerced = coerceValue(value, varType, varDefNode);
93-
const coercionErrors = coerced.errors;
94-
if (coercionErrors) {
95-
for (const error of coercionErrors) {
96-
error.message =
97-
`Variable "$${varName}" got invalid value ${inspect(value)}; ` +
98-
error.message;
99-
}
100-
errors.push(...coercionErrors);
101-
} else {
102-
coercedValues[varName] = coerced.value;
84+
// Otherwise, a non-null value was provided, coerce it to the expected
85+
// type or report an error if coercion fails.
86+
const coerced = coerceValue(value, varType, varDefNode);
87+
const coercionErrors = coerced.errors;
88+
if (coercionErrors) {
89+
for (const error of coercionErrors) {
90+
error.message =
91+
`Variable "$${varName}" got invalid value ${inspect(value)}; ` +
92+
error.message;
10393
}
94+
errors.push(...coercionErrors);
95+
} else {
96+
coercedValues[varName] = coerced.value;
10497
}
10598
}
10699
}
@@ -175,31 +168,18 @@ export function getArgumentValues(
175168
);
176169
}
177170
} else if (hasValue) {
178-
if (argumentNode.value.kind === Kind.NULL) {
179-
// If the explicit value `null` was provided, an entry in the coerced
180-
// values must exist as the value `null`.
181-
coercedValues[name] = null;
182-
} else if (argumentNode.value.kind === Kind.VARIABLE) {
183-
const variableName = argumentNode.value.name.value;
184-
invariant(variableValues, 'Must exist for hasValue to be true.');
185-
// Note: This does no further checking that this variable is correct.
186-
// This assumes that this query has been validated and the variable
187-
// usage here is of the correct type.
188-
coercedValues[name] = variableValues[variableName];
189-
} else {
190-
const valueNode = argumentNode.value;
191-
const coercedValue = valueFromAST(valueNode, argType, variableValues);
192-
if (coercedValue === undefined) {
193-
// Note: ValuesOfCorrectType validation should catch this before
194-
// execution. This is a runtime check to ensure execution does not
195-
// continue with an invalid argument value.
196-
throw new GraphQLError(
197-
`Argument "${name}" has invalid value ${print(valueNode)}.`,
198-
argumentNode.value,
199-
);
200-
}
201-
coercedValues[name] = coercedValue;
171+
const valueNode = argumentNode.value;
172+
const coercedValue = valueFromAST(valueNode, argType, variableValues);
173+
if (coercedValue === undefined) {
174+
// Note: ValuesOfCorrectType validation should catch this before
175+
// execution. This is a runtime check to ensure execution does not
176+
// continue with an invalid argument value.
177+
throw new GraphQLError(
178+
`Argument "${name}" has invalid value ${print(valueNode)}.`,
179+
argumentNode.value,
180+
);
202181
}
182+
coercedValues[name] = coercedValue;
203183
}
204184
}
205185
return coercedValues;

0 commit comments

Comments
 (0)