1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.valueFromAST = valueFromAST;
|
7 |
|
8 | var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues"));
|
9 |
|
10 | var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
|
11 |
|
12 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
13 |
|
14 | var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
|
15 |
|
16 | var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid"));
|
17 |
|
18 | var _kinds = require("../language/kinds");
|
19 |
|
20 | var _definition = require("../type/definition");
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function valueFromAST(valueNode, type, variables) {
|
45 | if (!valueNode) {
|
46 |
|
47 |
|
48 | return;
|
49 | }
|
50 |
|
51 | if ((0, _definition.isNonNullType)(type)) {
|
52 | if (valueNode.kind === _kinds.Kind.NULL) {
|
53 | return;
|
54 | }
|
55 |
|
56 | return valueFromAST(valueNode, type.ofType, variables);
|
57 | }
|
58 |
|
59 | if (valueNode.kind === _kinds.Kind.NULL) {
|
60 |
|
61 | return null;
|
62 | }
|
63 |
|
64 | if (valueNode.kind === _kinds.Kind.VARIABLE) {
|
65 | var variableName = valueNode.name.value;
|
66 |
|
67 | if (!variables || (0, _isInvalid.default)(variables[variableName])) {
|
68 |
|
69 | return;
|
70 | }
|
71 |
|
72 | var variableValue = variables[variableName];
|
73 |
|
74 | if (variableValue === null && (0, _definition.isNonNullType)(type)) {
|
75 | return;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | return variableValue;
|
82 | }
|
83 |
|
84 | if ((0, _definition.isListType)(type)) {
|
85 | var itemType = type.ofType;
|
86 |
|
87 | if (valueNode.kind === _kinds.Kind.LIST) {
|
88 | var coercedValues = [];
|
89 |
|
90 | for (var _i2 = 0, _valueNode$values2 = valueNode.values; _i2 < _valueNode$values2.length; _i2++) {
|
91 | var itemNode = _valueNode$values2[_i2];
|
92 |
|
93 | if (isMissingVariable(itemNode, variables)) {
|
94 |
|
95 |
|
96 | if ((0, _definition.isNonNullType)(itemType)) {
|
97 | return;
|
98 | }
|
99 |
|
100 | coercedValues.push(null);
|
101 | } else {
|
102 | var itemValue = valueFromAST(itemNode, itemType, variables);
|
103 |
|
104 | if ((0, _isInvalid.default)(itemValue)) {
|
105 | return;
|
106 | }
|
107 |
|
108 | coercedValues.push(itemValue);
|
109 | }
|
110 | }
|
111 |
|
112 | return coercedValues;
|
113 | }
|
114 |
|
115 | var coercedValue = valueFromAST(valueNode, itemType, variables);
|
116 |
|
117 | if ((0, _isInvalid.default)(coercedValue)) {
|
118 | return;
|
119 | }
|
120 |
|
121 | return [coercedValue];
|
122 | }
|
123 |
|
124 | if ((0, _definition.isInputObjectType)(type)) {
|
125 | if (valueNode.kind !== _kinds.Kind.OBJECT) {
|
126 | return;
|
127 | }
|
128 |
|
129 | var coercedObj = Object.create(null);
|
130 | var fieldNodes = (0, _keyMap.default)(valueNode.fields, function (field) {
|
131 | return field.name.value;
|
132 | });
|
133 |
|
134 | for (var _i4 = 0, _objectValues2 = (0, _objectValues3.default)(type.getFields()); _i4 < _objectValues2.length; _i4++) {
|
135 | var field = _objectValues2[_i4];
|
136 | var fieldNode = fieldNodes[field.name];
|
137 |
|
138 | if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
|
139 | if (field.defaultValue !== undefined) {
|
140 | coercedObj[field.name] = field.defaultValue;
|
141 | } else if ((0, _definition.isNonNullType)(field.type)) {
|
142 | return;
|
143 | }
|
144 |
|
145 | continue;
|
146 | }
|
147 |
|
148 | var fieldValue = valueFromAST(fieldNode.value, field.type, variables);
|
149 |
|
150 | if ((0, _isInvalid.default)(fieldValue)) {
|
151 | return;
|
152 | }
|
153 |
|
154 | coercedObj[field.name] = fieldValue;
|
155 | }
|
156 |
|
157 | return coercedObj;
|
158 | }
|
159 |
|
160 | if ((0, _definition.isEnumType)(type)) {
|
161 | if (valueNode.kind !== _kinds.Kind.ENUM) {
|
162 | return;
|
163 | }
|
164 |
|
165 | var enumValue = type.getValue(valueNode.value);
|
166 |
|
167 | if (!enumValue) {
|
168 | return;
|
169 | }
|
170 |
|
171 | return enumValue.value;
|
172 | }
|
173 |
|
174 |
|
175 | if ((0, _definition.isScalarType)(type)) {
|
176 |
|
177 |
|
178 |
|
179 | var result;
|
180 |
|
181 | try {
|
182 | result = type.parseLiteral(valueNode, variables);
|
183 | } catch (_error) {
|
184 | return;
|
185 | }
|
186 |
|
187 | if ((0, _isInvalid.default)(result)) {
|
188 | return;
|
189 | }
|
190 |
|
191 | return result;
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 | (0, _invariant.default)(false, 'Unexpected input type: ' + (0, _inspect.default)(type));
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 | function isMissingVariable(valueNode, variables) {
|
202 | return valueNode.kind === _kinds.Kind.VARIABLE && (!variables || (0, _isInvalid.default)(variables[valueNode.name.value]));
|
203 | }
|