UNPKG

6.09 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.valueFromAST = valueFromAST;
7
8var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
9
10var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
11
12var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid"));
13
14var _kinds = require("../language/kinds");
15
16var _definition = require("../type/definition");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * Copyright (c) 2015-present, Facebook, Inc.
22 *
23 * This source code is licensed under the MIT license found in the
24 * LICENSE file in the root directory of this source tree.
25 *
26 *
27 */
28
29/**
30 * Produces a JavaScript value given a GraphQL Value AST.
31 *
32 * A GraphQL type must be provided, which will be used to interpret different
33 * GraphQL Value literals.
34 *
35 * Returns `undefined` when the value could not be validly coerced according to
36 * the provided type.
37 *
38 * | GraphQL Value | JSON Value |
39 * | -------------------- | ------------- |
40 * | Input Object | Object |
41 * | List | Array |
42 * | Boolean | Boolean |
43 * | String | String |
44 * | Int / Float | Number |
45 * | Enum Value | Mixed |
46 * | NullValue | null |
47 *
48 */
49function valueFromAST(valueNode, type, variables) {
50 if (!valueNode) {
51 // When there is no node, then there is also no value.
52 // Importantly, this is different from returning the value null.
53 return;
54 }
55
56 if ((0, _definition.isNonNullType)(type)) {
57 if (valueNode.kind === _kinds.Kind.NULL) {
58 return; // Invalid: intentionally return no value.
59 }
60
61 return valueFromAST(valueNode, type.ofType, variables);
62 }
63
64 if (valueNode.kind === _kinds.Kind.NULL) {
65 // This is explicitly returning the value null.
66 return null;
67 }
68
69 if (valueNode.kind === _kinds.Kind.VARIABLE) {
70 var variableName = valueNode.name.value;
71
72 if (!variables || (0, _isInvalid.default)(variables[variableName])) {
73 // No valid return value.
74 return;
75 }
76
77 var variableValue = variables[variableName];
78
79 if (variableValue === null && (0, _definition.isNonNullType)(type)) {
80 return; // Invalid: intentionally return no value.
81 } // Note: This does no further checking that this variable is correct.
82 // This assumes that this query has been validated and the variable
83 // usage here is of the correct type.
84
85
86 return variableValue;
87 }
88
89 if ((0, _definition.isListType)(type)) {
90 var itemType = type.ofType;
91
92 if (valueNode.kind === _kinds.Kind.LIST) {
93 var coercedValues = [];
94 var itemNodes = valueNode.values;
95
96 for (var i = 0; i < itemNodes.length; i++) {
97 if (isMissingVariable(itemNodes[i], variables)) {
98 // If an array contains a missing variable, it is either coerced to
99 // null or if the item type is non-null, it considered invalid.
100 if ((0, _definition.isNonNullType)(itemType)) {
101 return; // Invalid: intentionally return no value.
102 }
103
104 coercedValues.push(null);
105 } else {
106 var itemValue = valueFromAST(itemNodes[i], itemType, variables);
107
108 if ((0, _isInvalid.default)(itemValue)) {
109 return; // Invalid: intentionally return no value.
110 }
111
112 coercedValues.push(itemValue);
113 }
114 }
115
116 return coercedValues;
117 }
118
119 var coercedValue = valueFromAST(valueNode, itemType, variables);
120
121 if ((0, _isInvalid.default)(coercedValue)) {
122 return; // Invalid: intentionally return no value.
123 }
124
125 return [coercedValue];
126 }
127
128 if ((0, _definition.isInputObjectType)(type)) {
129 if (valueNode.kind !== _kinds.Kind.OBJECT) {
130 return; // Invalid: intentionally return no value.
131 }
132
133 var coercedObj = Object.create(null);
134 var fieldNodes = (0, _keyMap.default)(valueNode.fields, function (field) {
135 return field.name.value;
136 });
137 var fields = (0, _objectValues.default)(type.getFields());
138
139 for (var _i = 0; _i < fields.length; _i++) {
140 var field = fields[_i];
141 var fieldNode = fieldNodes[field.name];
142
143 if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
144 if (field.defaultValue !== undefined) {
145 coercedObj[field.name] = field.defaultValue;
146 } else if ((0, _definition.isNonNullType)(field.type)) {
147 return; // Invalid: intentionally return no value.
148 }
149
150 continue;
151 }
152
153 var fieldValue = valueFromAST(fieldNode.value, field.type, variables);
154
155 if ((0, _isInvalid.default)(fieldValue)) {
156 return; // Invalid: intentionally return no value.
157 }
158
159 coercedObj[field.name] = fieldValue;
160 }
161
162 return coercedObj;
163 }
164
165 if ((0, _definition.isEnumType)(type)) {
166 if (valueNode.kind !== _kinds.Kind.ENUM) {
167 return; // Invalid: intentionally return no value.
168 }
169
170 var enumValue = type.getValue(valueNode.value);
171
172 if (!enumValue) {
173 return; // Invalid: intentionally return no value.
174 }
175
176 return enumValue.value;
177 }
178
179 if ((0, _definition.isScalarType)(type)) {
180 // Scalars fulfill parsing a literal value via parseLiteral().
181 // Invalid values represent a failure to parse correctly, in which case
182 // no value is returned.
183 var result;
184
185 try {
186 result = type.parseLiteral(valueNode, variables);
187 } catch (_error) {
188 return; // Invalid: intentionally return no value.
189 }
190
191 if ((0, _isInvalid.default)(result)) {
192 return; // Invalid: intentionally return no value.
193 }
194
195 return result;
196 }
197 /* istanbul ignore next */
198
199
200 throw new Error("Unknown type: ".concat(type, "."));
201} // Returns true if the provided valueNode is a variable which is not defined
202// in the set of variables.
203
204
205function isMissingVariable(valueNode, variables) {
206 return valueNode.kind === _kinds.Kind.VARIABLE && (!variables || (0, _isInvalid.default)(variables[valueNode.name.value]));
207}
\No newline at end of file