1 | import objectValues from '../polyfills/objectValues';
|
2 | import keyMap from '../jsutils/keyMap';
|
3 | import inspect from '../jsutils/inspect';
|
4 | import invariant from '../jsutils/invariant';
|
5 | import isInvalid from '../jsutils/isInvalid';
|
6 | import { Kind } from '../language/kinds';
|
7 | import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export function valueFromAST(valueNode, type, variables) {
|
30 | if (!valueNode) {
|
31 |
|
32 |
|
33 | return;
|
34 | }
|
35 |
|
36 | if (isNonNullType(type)) {
|
37 | if (valueNode.kind === Kind.NULL) {
|
38 | return;
|
39 | }
|
40 |
|
41 | return valueFromAST(valueNode, type.ofType, variables);
|
42 | }
|
43 |
|
44 | if (valueNode.kind === Kind.NULL) {
|
45 |
|
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 |
|
54 | return;
|
55 | }
|
56 |
|
57 | var variableValue = variables[variableName];
|
58 |
|
59 | if (variableValue === null && isNonNullType(type)) {
|
60 | return;
|
61 | }
|
62 |
|
63 |
|
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 |
|
80 |
|
81 | if (isNonNullType(itemType)) {
|
82 | return;
|
83 | }
|
84 |
|
85 | coercedValues.push(null);
|
86 | } else {
|
87 | var itemValue = valueFromAST(itemNode, itemType, variables);
|
88 |
|
89 | if (isInvalid(itemValue)) {
|
90 | return;
|
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;
|
104 | }
|
105 |
|
106 | return [coercedValue];
|
107 | }
|
108 |
|
109 | if (isInputObjectType(type)) {
|
110 | if (valueNode.kind !== Kind.OBJECT) {
|
111 | return;
|
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;
|
128 | }
|
129 |
|
130 | continue;
|
131 | }
|
132 |
|
133 | var fieldValue = valueFromAST(fieldNode.value, field.type, variables);
|
134 |
|
135 | if (isInvalid(fieldValue)) {
|
136 | return;
|
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;
|
148 | }
|
149 |
|
150 | var enumValue = type.getValue(valueNode.value);
|
151 |
|
152 | if (!enumValue) {
|
153 | return;
|
154 | }
|
155 |
|
156 | return enumValue.value;
|
157 | }
|
158 |
|
159 |
|
160 | if (isScalarType(type)) {
|
161 |
|
162 |
|
163 |
|
164 | var result;
|
165 |
|
166 | try {
|
167 | result = type.parseLiteral(valueNode, variables);
|
168 | } catch (_error) {
|
169 | return;
|
170 | }
|
171 |
|
172 | if (isInvalid(result)) {
|
173 | return;
|
174 | }
|
175 |
|
176 | return result;
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 | invariant(false, 'Unexpected input type: ' + inspect(type));
|
182 | }
|
183 |
|
184 |
|
185 | function isMissingVariable(valueNode, variables) {
|
186 | return valueNode.kind === Kind.VARIABLE && (!variables || isInvalid(variables[valueNode.name.value]));
|
187 | }
|