UNPKG

7.91 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"));
11
12var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
13
14var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
15
16var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray"));
17
18var _GraphQLError = require("../error/GraphQLError");
19
20var _kinds = require("../language/kinds");
21
22var _printer = require("../language/printer");
23
24var _definition = require("../type/definition");
25
26var _typeFromAST = require("../utilities/typeFromAST");
27
28var _valueFromAST = require("../utilities/valueFromAST");
29
30var _coerceInputValue = require("../utilities/coerceInputValue");
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 */
43function 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
70function 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 // Must use input types for variables. This should be caught during
80 // validation, however is checked again here for safety.
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 * 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
137
138function 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 // Note: ValuesOfCorrectType validation should catch this before
187 // execution. This is a runtime check to ensure execution does not
188 // continue with an invalid argument value.
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 * Prepares an object map of argument values given a directive definition
199 * and a AST node which may contain directives. Optionally also accepts a map
200 * of variable values.
201 *
202 * If the directive does not exist on the node, returns undefined.
203 *
204 * Note: The returned value is a plain Object with a prototype, since it is
205 * exposed to user code. Care should be taken to not pull values from the
206 * Object prototype.
207 */
208
209
210function 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
220function hasOwnProperty(obj, prop) {
221 return Object.prototype.hasOwnProperty.call(obj, prop);
222}