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