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"));
|
11 |
|
12 | var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
|
13 |
|
14 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
15 |
|
16 | var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray"));
|
17 |
|
18 | var _GraphQLError = require("../error/GraphQLError");
|
19 |
|
20 | var _kinds = require("../language/kinds");
|
21 |
|
22 | var _printer = require("../language/printer");
|
23 |
|
24 | var _definition = require("../type/definition");
|
25 |
|
26 | var _typeFromAST = require("../utilities/typeFromAST");
|
27 |
|
28 | var _valueFromAST = require("../utilities/valueFromAST");
|
29 |
|
30 | var _coerceInputValue = require("../utilities/coerceInputValue");
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function getVariableValues(schema, varDefNodes, inputs, options) {
|
44 | var maxErrors = options && options.maxErrors;
|
45 | var errors = [];
|
46 |
|
47 | try {
|
48 | var coerced = coerceVariableValues(schema, varDefNodes, inputs, function (error) {
|
49 | if (maxErrors != null && errors.length >= maxErrors) {
|
50 | throw new _GraphQLError.GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
|
51 | }
|
52 |
|
53 | errors.push(error);
|
54 | });
|
55 |
|
56 | if (errors.length === 0) {
|
57 | return {
|
58 | coerced: coerced
|
59 | };
|
60 | }
|
61 | } catch (error) {
|
62 | errors.push(error);
|
63 | }
|
64 |
|
65 | return {
|
66 | errors: errors
|
67 | };
|
68 | }
|
69 |
|
70 | function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
71 | var coercedValues = {};
|
72 |
|
73 | var _loop = function _loop(_i2) {
|
74 | var varDefNode = varDefNodes[_i2];
|
75 | var varName = varDefNode.variable.name.value;
|
76 | var varType = (0, _typeFromAST.typeFromAST)(schema, varDefNode.type);
|
77 |
|
78 | if (!(0, _definition.isInputType)(varType)) {
|
79 |
|
80 |
|
81 | var varTypeStr = (0, _printer.print)(varDefNode.type);
|
82 | onError(new _GraphQLError.GraphQLError("Variable \"$".concat(varName, "\" expected value of type \"").concat(varTypeStr, "\" which cannot be used as an input type."), varDefNode.type));
|
83 | return "continue";
|
84 | }
|
85 |
|
86 | if (!hasOwnProperty(inputs, varName)) {
|
87 | if (varDefNode.defaultValue) {
|
88 | coercedValues[varName] = (0, _valueFromAST.valueFromAST)(varDefNode.defaultValue, varType);
|
89 | }
|
90 |
|
91 | 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 | function getArgumentValues(def, node, variableValues) {
|
139 | var coercedValues = {};
|
140 | var argNodeMap = (0, _keyMap.default)(node.arguments || [], function (arg) {
|
141 | return arg.name.value;
|
142 | });
|
143 |
|
144 | for (var _i4 = 0, _def$args2 = def.args; _i4 < _def$args2.length; _i4++) {
|
145 | var argDef = _def$args2[_i4];
|
146 | var name = argDef.name;
|
147 | var argType = argDef.type;
|
148 | var argumentNode = argNodeMap[name];
|
149 |
|
150 | if (!argumentNode) {
|
151 | if (argDef.defaultValue !== undefined) {
|
152 | coercedValues[name] = argDef.defaultValue;
|
153 | } else if ((0, _definition.isNonNullType)(argType)) {
|
154 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" of required type \"").concat((0, _inspect.default)(argType), "\" ") + 'was not provided.', node);
|
155 | }
|
156 |
|
157 | continue;
|
158 | }
|
159 |
|
160 | var valueNode = argumentNode.value;
|
161 | var isNull = valueNode.kind === _kinds.Kind.NULL;
|
162 |
|
163 | if (valueNode.kind === _kinds.Kind.VARIABLE) {
|
164 | var variableName = valueNode.name.value;
|
165 |
|
166 | if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
|
167 | if (argDef.defaultValue !== undefined) {
|
168 | coercedValues[name] = argDef.defaultValue;
|
169 | } else if ((0, _definition.isNonNullType)(argType)) {
|
170 | 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);
|
171 | }
|
172 |
|
173 | continue;
|
174 | }
|
175 |
|
176 | isNull = variableValues[variableName] == null;
|
177 | }
|
178 |
|
179 | if (isNull && (0, _definition.isNonNullType)(argType)) {
|
180 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat((0, _inspect.default)(argType), "\" ") + 'must not be null.', valueNode);
|
181 | }
|
182 |
|
183 | var coercedValue = (0, _valueFromAST.valueFromAST)(valueNode, argType, variableValues);
|
184 |
|
185 | if (coercedValue === undefined) {
|
186 |
|
187 |
|
188 |
|
189 | throw new _GraphQLError.GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat((0, _printer.print)(valueNode), "."), valueNode);
|
190 | }
|
191 |
|
192 | coercedValues[name] = coercedValue;
|
193 | }
|
194 |
|
195 | return coercedValues;
|
196 | }
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function getDirectiveValues(directiveDef, node, variableValues) {
|
211 | var directiveNode = node.directives && (0, _find.default)(node.directives, function (directive) {
|
212 | return directive.name.value === directiveDef.name;
|
213 | });
|
214 |
|
215 | if (directiveNode) {
|
216 | return getArgumentValues(directiveDef, directiveNode, variableValues);
|
217 | }
|
218 | }
|
219 |
|
220 | function hasOwnProperty(obj, prop) {
|
221 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
222 | }
|