-
-
Notifications
You must be signed in to change notification settings - Fork 828
Expand file tree
/
Copy pathvalues.ts
More file actions
185 lines (172 loc) · 6.18 KB
/
Copy pathvalues.ts
File metadata and controls
185 lines (172 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
coerceInputValue,
GraphQLError,
GraphQLSchema,
isInputType,
isNonNullType,
NamedTypeNode,
print,
typeFromAST,
valueFromAST,
VariableDefinitionNode,
} from 'graphql';
import {
createGraphQLError,
getOptionalGraphQLJSExport,
hasOwnProperty,
inspect,
printPathArray,
} from '@graphql-tools/utils';
// `validateInputValue` was introduced in graphql-js@17, replacing the `onError`
// callback that `coerceInputValue` used to accept.
const validateInputValue = getOptionalGraphQLJSExport('validateInputValue');
// `coerceInputLiteral` was introduced in graphql-js@17. Unlike `valueFromAST`, it
// correctly rejects AST literals whose kind doesn't match the target type (e.g. an
// enum literal provided for a String variable default), which `valueFromAST`
// stopped doing in graphql-js@17. See the same pattern in getArgumentValues.ts.
const coerceInputLiteral = getOptionalGraphQLJSExport('coerceInputLiteral');
function getAtPath(value: unknown, path: ReadonlyArray<string | number>): unknown {
let current = value;
for (const key of path) {
if (current == null || typeof current !== 'object' || !hasOwnProperty(current, String(key))) {
return undefined;
}
current = (current as Record<string | number, unknown>)[key];
}
return current;
}
type CoercedVariableValues =
| { errors: ReadonlyArray<GraphQLError>; coerced?: never }
| { coerced: { [variable: string]: unknown }; errors?: never };
/**
* Prepares an object map of variableValues of the correct type based on the
* provided variable definitions and arbitrary input. If the input cannot be
* parsed to match the variable definitions, a GraphQLError will be thrown.
*
* Note: The returned value is a plain Object with a prototype, since it is
* exposed to user code. Care should be taken to not pull values from the
* Object prototype.
*/
export function getVariableValues(
schema: GraphQLSchema,
varDefNodes: ReadonlyArray<VariableDefinitionNode>,
inputs: { readonly [variable: string]: unknown },
options?: { maxErrors?: number },
): CoercedVariableValues {
const errors: any[] = [];
const maxErrors = options?.maxErrors;
try {
const coerced = coerceVariableValues(schema, varDefNodes, inputs, error => {
if (maxErrors != null && errors.length >= maxErrors) {
throw createGraphQLError(
'Too many errors processing variables, error limit reached. Execution aborted.',
);
}
errors.push(error);
});
if (errors.length === 0) {
return { coerced };
}
} catch (error) {
errors.push(error);
}
return { errors };
}
function coerceVariableValues(
schema: GraphQLSchema,
varDefNodes: ReadonlyArray<VariableDefinitionNode>,
inputs: { readonly [variable: string]: unknown },
onError: (error: GraphQLError) => void,
): { [variable: string]: unknown } {
const coercedValues: { [variable: string]: unknown } = {};
for (const varDefNode of varDefNodes) {
const varName = varDefNode.variable.name.value;
const varType = typeFromAST(schema, varDefNode.type as NamedTypeNode);
if (!isInputType(varType)) {
// Must use input types for variables. This should be caught during
// validation, however is checked again here for safety.
const varTypeStr = print(varDefNode.type);
onError(
createGraphQLError(
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
{ nodes: varDefNode.type },
),
);
continue;
}
if (!hasOwnProperty(inputs, varName)) {
if (varDefNode.defaultValue) {
const coercedDefaultValue = coerceInputLiteral
? coerceInputLiteral(varDefNode.defaultValue, varType)
: valueFromAST(varDefNode.defaultValue, varType);
if (coercedDefaultValue === undefined) {
// Schema/document validation should catch an invalid default value
// before execution; this mirrors graphql-js's own
// `coerceDefaultValue`/`maybeUseDefaultValue`, which report this
// case as an error rather than silently producing `undefined`.
onError(
createGraphQLError(`Variable "$${varName}" has invalid default value.`, {
nodes: varDefNode.defaultValue,
}),
);
} else {
coercedValues[varName] = coercedDefaultValue;
}
} else if (isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
createGraphQLError(
`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`,
{
nodes: varDefNode,
},
),
);
}
continue;
}
const value = inputs[varName];
if (value === null && isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
createGraphQLError(
`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`,
{
nodes: varDefNode,
},
),
);
continue;
}
const reportInvalidValue = (path: ReadonlyArray<string | number>, error: GraphQLError) => {
let prefix = `Variable "$${varName}" got invalid value ` + inspect(getAtPath(value, path));
if (path.length > 0) {
prefix += ` at "${varName}${printPathArray(path)}"`;
}
onError(
createGraphQLError(prefix + '; ' + error.message, {
nodes: varDefNode,
originalError: error,
}),
);
};
if (validateInputValue) {
// graphql-js >= 17: `coerceInputValue` no longer reports diagnostics via a
// callback, so fall back to `validateInputValue` when coercion fails.
const coercedValue = coerceInputValue(value, varType);
if (coercedValue !== undefined) {
coercedValues[varName] = coercedValue;
} else {
validateInputValue(value, varType, (error, path) => reportInvalidValue(path, error));
}
} else {
coercedValues[varName] = (coerceInputValue as any)(
value,
varType,
(path: ReadonlyArray<string | number>, _invalidValue: unknown, error: GraphQLError) =>
reportInvalidValue(path, error),
);
}
}
return coercedValues;
}