1 | import find from "../polyfills/find.mjs";
|
2 | import keyMap from "../jsutils/keyMap.mjs";
|
3 | import inspect from "../jsutils/inspect.mjs";
|
4 | import printPathArray from "../jsutils/printPathArray.mjs";
|
5 | import { GraphQLError } from "../error/GraphQLError.mjs";
|
6 | import { Kind } from "../language/kinds.mjs";
|
7 | import { print } from "../language/printer.mjs";
|
8 | import { isInputType, isNonNullType } from "../type/definition.mjs";
|
9 | import { typeFromAST } from "../utilities/typeFromAST.mjs";
|
10 | import { valueFromAST } from "../utilities/valueFromAST.mjs";
|
11 | import { coerceInputValue } from "../utilities/coerceInputValue.mjs";
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export 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 |
|
51 | function 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 |
|
61 |
|
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 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | export function getArgumentValues(def, node, variableValues) {
|
120 | var _node$arguments;
|
121 |
|
122 | var coercedValues = {};
|
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 |
|
172 |
|
173 |
|
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 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | export 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 |
|
204 | function hasOwnProperty(obj, prop) {
|
205 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
206 | }
|