1 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import { forEach, isCollection } from 'iterall';
|
12 | import objectValues from '../polyfills/objectValues';
|
13 | import inspect from '../jsutils/inspect';
|
14 | import isNullish from '../jsutils/isNullish';
|
15 | import isInvalid from '../jsutils/isInvalid';
|
16 | import { Kind } from '../language/kinds';
|
17 | import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition';
|
18 | import { GraphQLID } from '../type/scalars';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export function astFromValue(value, type) {
|
38 | if (isNonNullType(type)) {
|
39 | var astValue = astFromValue(value, type.ofType);
|
40 |
|
41 | if (astValue && astValue.kind === Kind.NULL) {
|
42 | return null;
|
43 | }
|
44 |
|
45 | return astValue;
|
46 | }
|
47 |
|
48 |
|
49 | if (value === null) {
|
50 | return {
|
51 | kind: Kind.NULL
|
52 | };
|
53 | }
|
54 |
|
55 |
|
56 | if (isInvalid(value)) {
|
57 | return null;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | if (isListType(type)) {
|
63 | var itemType = type.ofType;
|
64 |
|
65 | if (isCollection(value)) {
|
66 | var valuesNodes = [];
|
67 | forEach(value, function (item) {
|
68 | var itemNode = astFromValue(item, itemType);
|
69 |
|
70 | if (itemNode) {
|
71 | valuesNodes.push(itemNode);
|
72 | }
|
73 | });
|
74 | return {
|
75 | kind: Kind.LIST,
|
76 | values: valuesNodes
|
77 | };
|
78 | }
|
79 |
|
80 | return astFromValue(value, itemType);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | if (isInputObjectType(type)) {
|
86 | if (value === null || _typeof(value) !== 'object') {
|
87 | return null;
|
88 | }
|
89 |
|
90 | var fields = objectValues(type.getFields());
|
91 | var fieldNodes = [];
|
92 | var _iteratorNormalCompletion = true;
|
93 | var _didIteratorError = false;
|
94 | var _iteratorError = undefined;
|
95 |
|
96 | try {
|
97 | for (var _iterator = fields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
98 | var field = _step.value;
|
99 | var fieldValue = astFromValue(value[field.name], field.type);
|
100 |
|
101 | if (fieldValue) {
|
102 | fieldNodes.push({
|
103 | kind: Kind.OBJECT_FIELD,
|
104 | name: {
|
105 | kind: Kind.NAME,
|
106 | value: field.name
|
107 | },
|
108 | value: fieldValue
|
109 | });
|
110 | }
|
111 | }
|
112 | } catch (err) {
|
113 | _didIteratorError = true;
|
114 | _iteratorError = err;
|
115 | } finally {
|
116 | try {
|
117 | if (!_iteratorNormalCompletion && _iterator.return != null) {
|
118 | _iterator.return();
|
119 | }
|
120 | } finally {
|
121 | if (_didIteratorError) {
|
122 | throw _iteratorError;
|
123 | }
|
124 | }
|
125 | }
|
126 |
|
127 | return {
|
128 | kind: Kind.OBJECT,
|
129 | fields: fieldNodes
|
130 | };
|
131 | }
|
132 |
|
133 | if (isScalarType(type) || isEnumType(type)) {
|
134 |
|
135 |
|
136 | var serialized = type.serialize(value);
|
137 |
|
138 | if (isNullish(serialized)) {
|
139 | return null;
|
140 | }
|
141 |
|
142 |
|
143 | if (typeof serialized === 'boolean') {
|
144 | return {
|
145 | kind: Kind.BOOLEAN,
|
146 | value: serialized
|
147 | };
|
148 | }
|
149 |
|
150 |
|
151 | if (typeof serialized === 'number') {
|
152 | var stringNum = String(serialized);
|
153 | return integerStringRegExp.test(stringNum) ? {
|
154 | kind: Kind.INT,
|
155 | value: stringNum
|
156 | } : {
|
157 | kind: Kind.FLOAT,
|
158 | value: stringNum
|
159 | };
|
160 | }
|
161 |
|
162 | if (typeof serialized === 'string') {
|
163 |
|
164 | if (isEnumType(type)) {
|
165 | return {
|
166 | kind: Kind.ENUM,
|
167 | value: serialized
|
168 | };
|
169 | }
|
170 |
|
171 |
|
172 | if (type === GraphQLID && integerStringRegExp.test(serialized)) {
|
173 | return {
|
174 | kind: Kind.INT,
|
175 | value: serialized
|
176 | };
|
177 | }
|
178 |
|
179 | return {
|
180 | kind: Kind.STRING,
|
181 | value: serialized
|
182 | };
|
183 | }
|
184 |
|
185 | throw new TypeError("Cannot convert value to AST: ".concat(inspect(serialized)));
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 | throw new Error("Unknown type: ".concat(type, "."));
|
191 | }
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | var integerStringRegExp = /^-?(0|[1-9][0-9]*)$/; |
\ | No newline at end of file |