1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getVariableValues = getVariableValues;
|
7 | exports.getArgumentValues = getArgumentValues;
|
8 | exports.getDirectiveValues = getDirectiveValues;
|
9 |
|
10 | var _find = _interopRequireDefault(require("../polyfills/find.js"));
|
11 |
|
12 | var _keyMap = _interopRequireDefault(require("../jsutils/keyMap.js"));
|
13 |
|
14 | var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
|
15 |
|
16 | var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray.js"));
|
17 |
|
18 | var _GraphQLError = require("../error/GraphQLError.js");
|
19 |
|
20 | var _kinds = require("../language/kinds.js");
|
21 |
|
22 | var _printer = require("../language/printer.js");
|
23 |
|
24 | var _definition = require("../type/definition.js");
|
25 |
|
26 | var _typeFromAST = require("../utilities/typeFromAST.js");
|
27 |
|
28 | var _valueFromAST = require("../utilities/valueFromAST.js");
|
29 |
|
30 | var _coerceInputValue = require("../utilities/coerceInputValue.js");
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function getVariableValues(schema, varDefNodes, inputs, options) {
|
46 | var errors = [];
|
47 | var maxErrors = options === null || options === void 0 ? void 0 : options.maxErrors;
|
48 |
|
49 | try {
|
50 | var coerced = coerceVariableValues(schema, varDefNodes, inputs, function (error) {
|
51 | if (maxErrors != null && errors.length >= maxErrors) {
|
52 | throw new _GraphQLError.GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
|
53 | }
|
54 |
|
55 | errors.push(error);
|
56 | });
|
57 |
|
58 | if (errors.length === 0) {
|
59 | return {
|
60 | coerced: coerced
|
61 | };
|
62 | }
|
63 | } catch (error) {
|
64 | errors.push(error);
|
65 | }
|
66 |
|
67 | return {
|
68 | errors: errors
|
69 | };
|
70 | }
|
71 |
|
72 | function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
73 | var coercedValues = {};
|
74 |
|
75 | var _loop = function _loop(_i2) {
|
76 | var varDefNode = varDefNodes[_i2];
|
77 | var varName = varDefNode.variable.name.value;
|
78 | var varType = (0, _typeFromAST.typeFromAST)(schema, varDefNode.type);
|
79 |
|
80 | if (!(0, _definition.isInputType)(varType)) {
|
81 |
|
82 |
|
83 | var varTypeStr = (0, _printer.print)(varDefNode.type);
|
84 | onError(new _GraphQLError.GraphQLError("Variable \"$".concat(varName, "\" expected value of type \"").concat(varTypeStr, "\" which cannot be used as an input type."), varDefNode.type));
|
85 | return "continue";
|
86 | }
|
87 |
|
88 | if (!hasOwnProperty(inputs, varName)) {
|
89 | if (varDefNode.defaultValue) {
|
90 | coercedValues[varName] = (0, _valueFromAST.valueFromAST)(varDefNode.defaultValue, varType);
|
91 | } else if ((0, _definition.isNonNullType)(varType)) {
|
92 | var _varTypeStr = (0, _inspect.default)(varType);
|
93 |
|
94 | onError(new _GraphQLError.GraphQLError("Variable \"$".concat(varName, "\" of required type \"").concat(_varTypeStr, "\" was not provided."), varDefNode));
|
95 | }
|
96 |
|
97 | return "continue";
|
98 | }
|
99 |
|
100 | var value = inputs[varName];
|
101 |
|
102 | if (value === null && (0, _definition.isNonNullType)(varType)) {
|
103 | var _varTypeStr2 = (0, _inspect.default)(varType);
|
104 |
|
105 | onError(new _GraphQLError.GraphQLError("Variable \"$".concat(varName, "\" of non-null type \"").concat(_varTypeStr2, "\" must not be null."), varDefNode));
|
106 | return "continue";
|
107 | }
|
108 |
|
109 | coercedValues[varName] = (0, _coerceInputValue.coerceInputValue)(value, varType, function (path, invalidValue, error) {
|
110 | var prefix = "Variable \"$".concat(varName, "\" got invalid value ") + (0, _inspect.default)(invalidValue);
|
111 |
|
112 | if (path.length > 0) {
|
113 | prefix += " at \"".concat(varName).concat((0, _printPathArray.default)(path), "\"");
|
114 | }
|
115 |
|
116 | onError(new _GraphQLError.GraphQLError(prefix + '; ' + error.message, varDefNode, undefined, undefined, undefined, error.originalError));
|
117 | });
|
118 | };
|
119 |
|
120 | for (var _i2 = 0; _i2 < varDefNodes.length; _i2++) {
|
121 | var _ret = _loop(_i2);
|
122 |
|
123 | if (_ret === "continue") continue;
|
124 | }
|
125 |
|
126 | return coercedValues;
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function getArgumentValues(def, node, variableValues) {
|
141 | var _node$arguments;
|
142 |
|
143 | var coercedValues = {};
|
144 |
|
145 | var argumentNodes = (_node$arguments = node.arguments) !== null && _node$arguments !== void 0 ? _node$arguments : [];
|
146 | var argNodeMap = (0, _keyMap.default)(argumentNodes, function (arg) {
|
147 | return arg.name.value;
|
148 | });
|
149 |
|
150 | for (var _i4 = 0, _def$args2 = def.args; _i4 < _def$args2.length; _i4++) {
|
151 | var argDef = _def$args2[_i4];
|
152 | var name = argDef.name;
|
153 | var argType = argDef.type;
|
154 | var argumentNode = argNodeMap[name];
|
155 |
|
156 | if (!argumentNode) {
|
157 | if (argDef.defaultValue !== undefined) {
|
158 | coercedValues[name] = argDef.defaultValue;
|
159 | } else if ((0, _definition.isNonNullType)(argType)) {
|
160 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" of required type \"").concat((0, _inspect.default)(argType), "\" ") + 'was not provided.', node);
|
161 | }
|
162 |
|
163 | continue;
|
164 | }
|
165 |
|
166 | var valueNode = argumentNode.value;
|
167 | var isNull = valueNode.kind === _kinds.Kind.NULL;
|
168 |
|
169 | if (valueNode.kind === _kinds.Kind.VARIABLE) {
|
170 | var variableName = valueNode.name.value;
|
171 |
|
172 | if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
|
173 | if (argDef.defaultValue !== undefined) {
|
174 | coercedValues[name] = argDef.defaultValue;
|
175 | } else if ((0, _definition.isNonNullType)(argType)) {
|
176 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" of required type \"").concat((0, _inspect.default)(argType), "\" ") + "was provided the variable \"$".concat(variableName, "\" which was not provided a runtime value."), valueNode);
|
177 | }
|
178 |
|
179 | continue;
|
180 | }
|
181 |
|
182 | isNull = variableValues[variableName] == null;
|
183 | }
|
184 |
|
185 | if (isNull && (0, _definition.isNonNullType)(argType)) {
|
186 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat((0, _inspect.default)(argType), "\" ") + 'must not be null.', valueNode);
|
187 | }
|
188 |
|
189 | var coercedValue = (0, _valueFromAST.valueFromAST)(valueNode, argType, variableValues);
|
190 |
|
191 | if (coercedValue === undefined) {
|
192 |
|
193 |
|
194 |
|
195 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat((0, _printer.print)(valueNode), "."), valueNode);
|
196 | }
|
197 |
|
198 | coercedValues[name] = coercedValue;
|
199 | }
|
200 |
|
201 | return coercedValues;
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function getDirectiveValues(directiveDef, node, variableValues) {
|
217 | var directiveNode = node.directives && (0, _find.default)(node.directives, function (directive) {
|
218 | return directive.name.value === directiveDef.name;
|
219 | });
|
220 |
|
221 | if (directiveNode) {
|
222 | return getArgumentValues(directiveDef, directiveNode, variableValues);
|
223 | }
|
224 | }
|
225 |
|
226 | function hasOwnProperty(obj, prop) {
|
227 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
228 | }
|