UNPKG

7.36 kBJavaScriptView Raw
1import find from "../polyfills/find.mjs";
2import keyMap from "../jsutils/keyMap.mjs";
3import inspect from "../jsutils/inspect.mjs";
4import printPathArray from "../jsutils/printPathArray.mjs";
5import { GraphQLError } from "../error/GraphQLError.mjs";
6import { Kind } from "../language/kinds.mjs";
7import { print } from "../language/printer.mjs";
8import { isInputType, isNonNullType } from "../type/definition.mjs";
9import { typeFromAST } from "../utilities/typeFromAST.mjs";
10import { valueFromAST } from "../utilities/valueFromAST.mjs";
11import { coerceInputValue } from "../utilities/coerceInputValue.mjs";
12
13/**
14 * Prepares an object map of variableValues of the correct type based on the
15 * provided variable definitions and arbitrary input. If the input cannot be
16 * parsed to match the variable definitions, a GraphQLError will be thrown.
17 *
18 * Note: The returned value is a plain Object with a prototype, since it is
19 * exposed to user code. Care should be taken to not pull values from the
20 * Object prototype.
21 *
22 * @internal
23 */
24export function getVariableValues(schema, varDefNodes, inputs, options) {
25 var errors = [];
26 var maxErrors = options === null || options === void 0 ? void 0 : options.maxErrors;
27
28 try {
29 var coerced = coerceVariableValues(schema, varDefNodes, inputs, function (error) {
30 if (maxErrors != null && errors.length >= maxErrors) {
31 throw new GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
32 }
33
34 errors.push(error);
35 });
36
37 if (errors.length === 0) {
38 return {
39 coerced: coerced
40 };
41 }
42 } catch (error) {
43 errors.push(error);
44 }
45
46 return {
47 errors: errors
48 };
49}
50
51function coerceVariableValues(schema, varDefNodes, inputs, onError) {
52 var coercedValues = {};
53
54 var _loop = function _loop(_i2) {
55 var varDefNode = varDefNodes[_i2];
56 var varName = varDefNode.variable.name.value;
57 var varType = typeFromAST(schema, varDefNode.type);
58
59 if (!isInputType(varType)) {
60 // Must use input types for variables. This should be caught during
61 // validation, however is checked again here for safety.
62 var varTypeStr = print(varDefNode.type);
63 onError(new GraphQLError("Variable \"$".concat(varName, "\" expected value of type \"").concat(varTypeStr, "\" which cannot be used as an input type."), varDefNode.type));
64 return "continue";
65 }
66
67 if (!hasOwnProperty(inputs, varName)) {
68 if (varDefNode.defaultValue) {
69 coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
70 } else if (isNonNullType(varType)) {
71 var _varTypeStr = inspect(varType);
72
73 onError(new GraphQLError("Variable \"$".concat(varName, "\" of required type \"").concat(_varTypeStr, "\" was not provided."), varDefNode));
74 }
75
76 return "continue";
77 }
78
79 var value = inputs[varName];
80
81 if (value === null && isNonNullType(varType)) {
82 var _varTypeStr2 = inspect(varType);
83
84 onError(new GraphQLError("Variable \"$".concat(varName, "\" of non-null type \"").concat(_varTypeStr2, "\" must not be null."), varDefNode));
85 return "continue";
86 }
87
88 coercedValues[varName] = coerceInputValue(value, varType, function (path, invalidValue, error) {
89 var prefix = "Variable \"$".concat(varName, "\" got invalid value ") + inspect(invalidValue);
90
91 if (path.length > 0) {
92 prefix += " at \"".concat(varName).concat(printPathArray(path), "\"");
93 }
94
95 onError(new GraphQLError(prefix + '; ' + error.message, varDefNode, undefined, undefined, undefined, error.originalError));
96 });
97 };
98
99 for (var _i2 = 0; _i2 < varDefNodes.length; _i2++) {
100 var _ret = _loop(_i2);
101
102 if (_ret === "continue") continue;
103 }
104
105 return coercedValues;
106}
107/**
108 * Prepares an object map of argument values given a list of argument
109 * definitions and list of argument AST nodes.
110 *
111 * Note: The returned value is a plain Object with a prototype, since it is
112 * exposed to user code. Care should be taken to not pull values from the
113 * Object prototype.
114 *
115 * @internal
116 */
117
118
119export function getArgumentValues(def, node, variableValues) {
120 var _node$arguments;
121
122 var coercedValues = {}; // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
123
124 var argumentNodes = (_node$arguments = node.arguments) !== null && _node$arguments !== void 0 ? _node$arguments : [];
125 var argNodeMap = keyMap(argumentNodes, function (arg) {
126 return arg.name.value;
127 });
128
129 for (var _i4 = 0, _def$args2 = def.args; _i4 < _def$args2.length; _i4++) {
130 var argDef = _def$args2[_i4];
131 var name = argDef.name;
132 var argType = argDef.type;
133 var argumentNode = argNodeMap[name];
134
135 if (!argumentNode) {
136 if (argDef.defaultValue !== undefined) {
137 coercedValues[name] = argDef.defaultValue;
138 } else if (isNonNullType(argType)) {
139 throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + 'was not provided.', node);
140 }
141
142 continue;
143 }
144
145 var valueNode = argumentNode.value;
146 var isNull = valueNode.kind === Kind.NULL;
147
148 if (valueNode.kind === Kind.VARIABLE) {
149 var variableName = valueNode.name.value;
150
151 if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
152 if (argDef.defaultValue !== undefined) {
153 coercedValues[name] = argDef.defaultValue;
154 } else if (isNonNullType(argType)) {
155 throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + "was provided the variable \"$".concat(variableName, "\" which was not provided a runtime value."), valueNode);
156 }
157
158 continue;
159 }
160
161 isNull = variableValues[variableName] == null;
162 }
163
164 if (isNull && isNonNullType(argType)) {
165 throw new GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat(inspect(argType), "\" ") + 'must not be null.', valueNode);
166 }
167
168 var coercedValue = valueFromAST(valueNode, argType, variableValues);
169
170 if (coercedValue === undefined) {
171 // Note: ValuesOfCorrectTypeRule validation should catch this before
172 // execution. This is a runtime check to ensure execution does not
173 // continue with an invalid argument value.
174 throw new GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat(print(valueNode), "."), valueNode);
175 }
176
177 coercedValues[name] = coercedValue;
178 }
179
180 return coercedValues;
181}
182/**
183 * Prepares an object map of argument values given a directive definition
184 * and a AST node which may contain directives. Optionally also accepts a map
185 * of variable values.
186 *
187 * If the directive does not exist on the node, returns undefined.
188 *
189 * Note: The returned value is a plain Object with a prototype, since it is
190 * exposed to user code. Care should be taken to not pull values from the
191 * Object prototype.
192 */
193
194export function getDirectiveValues(directiveDef, node, variableValues) {
195 var directiveNode = node.directives && find(node.directives, function (directive) {
196 return directive.name.value === directiveDef.name;
197 });
198
199 if (directiveNode) {
200 return getArgumentValues(directiveDef, directiveNode, variableValues);
201 }
202}
203
204function hasOwnProperty(obj, prop) {
205 return Object.prototype.hasOwnProperty.call(obj, prop);
206}