UNPKG

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