UNPKG

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