UNPKG

8.23 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getVariableValues = getVariableValues;
7exports.getArgumentValues = getArgumentValues;
8exports.getDirectiveValues = getDirectiveValues;
9
10var _find = _interopRequireDefault(require("../polyfills/find.js"));
11
12var _keyMap = _interopRequireDefault(require("../jsutils/keyMap.js"));
13
14var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
15
16var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray.js"));
17
18var _GraphQLError = require("../error/GraphQLError.js");
19
20var _kinds = require("../language/kinds.js");
21
22var _printer = require("../language/printer.js");
23
24var _definition = require("../type/definition.js");
25
26var _typeFromAST = require("../utilities/typeFromAST.js");
27
28var _valueFromAST = require("../utilities/valueFromAST.js");
29
30var _coerceInputValue = require("../utilities/coerceInputValue.js");
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34/**
35 * Prepares an object map of variableValues of the correct type based on the
36 * provided variable definitions and arbitrary input. If the input cannot be
37 * parsed to match the variable definitions, a GraphQLError will be thrown.
38 *
39 * Note: The returned value is a plain Object with a prototype, since it is
40 * exposed to user code. Care should be taken to not pull values from the
41 * Object prototype.
42 *
43 * @internal
44 */
45function 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
72function 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 // Must use input types for variables. This should be caught during
82 // validation, however is checked again here for safety.
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 * Prepares an object map of argument values given a list of argument
130 * definitions and list of argument AST nodes.
131 *
132 * Note: The returned value is a plain Object with a prototype, since it is
133 * exposed to user code. Care should be taken to not pull values from the
134 * Object prototype.
135 *
136 * @internal
137 */
138
139
140function getArgumentValues(def, node, variableValues) {
141 var _node$arguments;
142
143 var coercedValues = {}; // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
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 // Note: ValuesOfCorrectTypeRule validation should catch this before
193 // execution. This is a runtime check to ensure execution does not
194 // continue with an invalid argument value.
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 * Prepares an object map of argument values given a directive definition
205 * and a AST node which may contain directives. Optionally also accepts a map
206 * of variable values.
207 *
208 * If the directive does not exist on the node, returns undefined.
209 *
210 * Note: The returned value is a plain Object with a prototype, since it is
211 * exposed to user code. Care should be taken to not pull values from the
212 * Object prototype.
213 */
214
215
216function 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
226function hasOwnProperty(obj, prop) {
227 return Object.prototype.hasOwnProperty.call(obj, prop);
228}