Skip to content

Commit 82ae1fa

Browse files
committed
Makes getArgumentValues/getVariableValues algorithm linear
1 parent 18f1f79 commit 82ae1fa

File tree

1 file changed

+87
-87
lines changed

1 file changed

+87
-87
lines changed

src/execution/values.js

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -44,60 +44,65 @@ export function getVariableValues(
4444
): CoercedVariableValues {
4545
const errors = [];
4646
const coercedValues = {};
47-
for (let i = 0; i < varDefNodes.length; i++) {
48-
const varDefNode = varDefNodes[i];
47+
for (const varDefNode of varDefNodes) {
4948
const varName = varDefNode.variable.name.value;
5049
const varType = typeFromAST(schema, varDefNode.type);
5150
if (!isInputType(varType)) {
5251
// Must use input types for variables. This should be caught during
5352
// validation, however is checked again here for safety.
53+
const varTypeStr = print(varDefNode.type);
5454
errors.push(
5555
new GraphQLError(
56-
`Variable "$${varName}" expected value of type ` +
57-
`"${print(
58-
varDefNode.type,
59-
)}" which cannot be used as an input type.`,
56+
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
6057
varDefNode.type,
6158
),
6259
);
63-
} else {
64-
const hasValue = hasOwnProperty(inputs, varName);
65-
const value = hasValue ? inputs[varName] : undefined;
66-
if (!hasValue && varDefNode.defaultValue) {
67-
// If no value was provided to a variable with a default value,
68-
// use the default value.
60+
continue;
61+
}
62+
63+
if (!hasOwnProperty(inputs, varName)) {
64+
if (varDefNode.defaultValue) {
6965
coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
70-
} else if ((!hasValue || value === null) && isNonNullType(varType)) {
71-
// If no value or a nullish value was provided to a variable with a
72-
// non-null type (required), produce an error.
66+
}
67+
68+
if (isNonNullType(varType)) {
69+
const varTypeStr = inspect(varType);
7370
errors.push(
7471
new GraphQLError(
75-
hasValue
76-
? `Variable "$${varName}" of non-null type ` +
77-
`"${inspect(varType)}" must not be null.`
78-
: `Variable "$${varName}" of required type ` +
79-
`"${inspect(varType)}" was not provided.`,
72+
`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`,
8073
varDefNode,
8174
),
8275
);
83-
} else if (hasValue) {
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;
93-
}
94-
errors.push(...coercionErrors);
95-
} else {
96-
coercedValues[varName] = coerced.value;
97-
}
9876
}
77+
continue;
78+
}
79+
80+
const value = inputs[varName];
81+
if (value === null && isNonNullType(varType)) {
82+
const varTypeStr = inspect(varType);
83+
errors.push(
84+
new GraphQLError(
85+
`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`,
86+
varDefNode,
87+
),
88+
);
89+
continue;
90+
}
91+
92+
const coerced = coerceValue(value, varType, varDefNode);
93+
if (coerced.errors) {
94+
for (const error of coerced.errors) {
95+
error.message =
96+
`Variable "$${varName}" got invalid value ${inspect(value)}; ` +
97+
error.message;
98+
}
99+
errors.push(...coerced.errors);
100+
continue;
99101
}
102+
103+
coercedValues[varName] = coerced.value;
100104
}
105+
101106
return errors.length === 0
102107
? { errors: undefined, coerced: coercedValues }
103108
: { errors, coerced: undefined };
@@ -117,70 +122,65 @@ export function getArgumentValues(
117122
variableValues?: ?ObjMap<mixed>,
118123
): { [argument: string]: mixed, ... } {
119124
const coercedValues = {};
120-
const argNodes = node.arguments;
121-
if (!argNodes) {
122-
return coercedValues;
123-
}
124-
const argDefs = def.args;
125-
const argNodeMap = keyMap(argNodes, arg => arg.name.value);
126-
for (let i = 0; i < argDefs.length; i++) {
127-
const argDef = argDefs[i];
125+
const argNodeMap = keyMap(node.arguments || [], arg => arg.name.value);
126+
127+
for (const argDef of def.args) {
128128
const name = argDef.name;
129129
const argType = argDef.type;
130130
const argumentNode = argNodeMap[name];
131-
let hasValue;
132-
let isNull;
133-
if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) {
134-
const variableName = argumentNode.value.name.value;
135-
hasValue =
136-
variableValues != null && hasOwnProperty(variableValues, variableName);
137-
isNull = variableValues != null && variableValues[variableName] === null;
138-
} else {
139-
hasValue = argumentNode != null;
140-
isNull = argumentNode != null && argumentNode.value.kind === Kind.NULL;
141-
}
142131

143-
if (!hasValue && argDef.defaultValue !== undefined) {
144-
// If no argument was provided where the definition has a default value,
145-
// use the default value.
146-
coercedValues[name] = argDef.defaultValue;
147-
} else if ((!hasValue || isNull) && isNonNullType(argType)) {
148-
// If no argument or a null value was provided to an argument with a
149-
// non-null type (required), produce a field error.
150-
if (isNull) {
151-
throw new GraphQLError(
152-
`Argument "${name}" of non-null type "${inspect(argType)}" ` +
153-
'must not be null.',
154-
argumentNode.value,
155-
);
156-
} else if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) {
157-
const variableName = argumentNode.value.name.value;
158-
throw new GraphQLError(
159-
`Argument "${name}" of required type "${inspect(argType)}" ` +
160-
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
161-
argumentNode.value,
162-
);
163-
} else {
132+
if (!argumentNode) {
133+
if (argDef.defaultValue !== undefined) {
134+
coercedValues[name] = argDef.defaultValue;
135+
} else if (isNonNullType(argType)) {
164136
throw new GraphQLError(
165137
`Argument "${name}" of required type "${inspect(argType)}" ` +
166138
'was not provided.',
167139
node,
168140
);
169141
}
170-
} else if (hasValue) {
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-
);
142+
continue;
143+
}
144+
145+
const valueNode = argumentNode.value;
146+
let isNull = valueNode.kind === Kind.NULL;
147+
148+
if (valueNode.kind === Kind.VARIABLE) {
149+
const variableName = valueNode.name.value;
150+
if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
151+
if (argDef.defaultValue !== undefined) {
152+
coercedValues[name] = argDef.defaultValue;
153+
} else if (isNonNullType(argType)) {
154+
throw new GraphQLError(
155+
`Argument "${name}" of required type "${inspect(argType)}" ` +
156+
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
157+
valueNode,
158+
);
159+
}
160+
continue;
181161
}
182-
coercedValues[name] = coercedValue;
162+
isNull = variableValues[variableName] == null;
163+
}
164+
165+
if (isNull && isNonNullType(argType)) {
166+
throw new GraphQLError(
167+
`Argument "${name}" of non-null type "${inspect(argType)}" ` +
168+
'must not be null.',
169+
valueNode,
170+
);
171+
}
172+
173+
const coercedValue = valueFromAST(valueNode, argType, variableValues);
174+
if (coercedValue === undefined) {
175+
// Note: ValuesOfCorrectType validation should catch this before
176+
// execution. This is a runtime check to ensure execution does not
177+
// continue with an invalid argument value.
178+
throw new GraphQLError(
179+
`Argument "${name}" has invalid value ${print(valueNode)}.`,
180+
valueNode,
181+
);
183182
}
183+
coercedValues[name] = coercedValue;
184184
}
185185
return coercedValues;
186186
}

0 commit comments

Comments
 (0)