1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true,
|
5 | });
|
6 | exports.astFromValue = astFromValue;
|
7 |
|
8 | var _inspect = require('../jsutils/inspect.js');
|
9 |
|
10 | var _invariant = require('../jsutils/invariant.js');
|
11 |
|
12 | var _isIterableObject = require('../jsutils/isIterableObject.js');
|
13 |
|
14 | var _isObjectLike = require('../jsutils/isObjectLike.js');
|
15 |
|
16 | var _kinds = require('../language/kinds.js');
|
17 |
|
18 | var _definition = require('../type/definition.js');
|
19 |
|
20 | var _scalars = require('../type/scalars.js');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function astFromValue(value, type) {
|
44 | if ((0, _definition.isNonNullType)(type)) {
|
45 | const astValue = astFromValue(value, type.ofType);
|
46 |
|
47 | if (
|
48 | (astValue === null || astValue === void 0 ? void 0 : astValue.kind) ===
|
49 | _kinds.Kind.NULL
|
50 | ) {
|
51 | return null;
|
52 | }
|
53 |
|
54 | return astValue;
|
55 | }
|
56 |
|
57 | if (value === null) {
|
58 | return {
|
59 | kind: _kinds.Kind.NULL,
|
60 | };
|
61 | }
|
62 |
|
63 | if (value === undefined) {
|
64 | return null;
|
65 | }
|
66 |
|
67 |
|
68 | if ((0, _definition.isListType)(type)) {
|
69 | const itemType = type.ofType;
|
70 |
|
71 | if ((0, _isIterableObject.isIterableObject)(value)) {
|
72 | const valuesNodes = [];
|
73 |
|
74 | for (const item of value) {
|
75 | const itemNode = astFromValue(item, itemType);
|
76 |
|
77 | if (itemNode != null) {
|
78 | valuesNodes.push(itemNode);
|
79 | }
|
80 | }
|
81 |
|
82 | return {
|
83 | kind: _kinds.Kind.LIST,
|
84 | values: valuesNodes,
|
85 | };
|
86 | }
|
87 |
|
88 | return astFromValue(value, itemType);
|
89 | }
|
90 |
|
91 |
|
92 | if ((0, _definition.isInputObjectType)(type)) {
|
93 | if (!(0, _isObjectLike.isObjectLike)(value)) {
|
94 | return null;
|
95 | }
|
96 |
|
97 | const fieldNodes = [];
|
98 |
|
99 | for (const field of Object.values(type.getFields())) {
|
100 | const fieldValue = astFromValue(value[field.name], field.type);
|
101 |
|
102 | if (fieldValue) {
|
103 | fieldNodes.push({
|
104 | kind: _kinds.Kind.OBJECT_FIELD,
|
105 | name: {
|
106 | kind: _kinds.Kind.NAME,
|
107 | value: field.name,
|
108 | },
|
109 | value: fieldValue,
|
110 | });
|
111 | }
|
112 | }
|
113 |
|
114 | return {
|
115 | kind: _kinds.Kind.OBJECT,
|
116 | fields: fieldNodes,
|
117 | };
|
118 | }
|
119 |
|
120 | if ((0, _definition.isLeafType)(type)) {
|
121 |
|
122 |
|
123 | const serialized = type.serialize(value);
|
124 |
|
125 | if (serialized == null) {
|
126 | return null;
|
127 | }
|
128 |
|
129 | if (typeof serialized === 'boolean') {
|
130 | return {
|
131 | kind: _kinds.Kind.BOOLEAN,
|
132 | value: serialized,
|
133 | };
|
134 | }
|
135 |
|
136 | if (typeof serialized === 'number' && Number.isFinite(serialized)) {
|
137 | const stringNum = String(serialized);
|
138 | return integerStringRegExp.test(stringNum)
|
139 | ? {
|
140 | kind: _kinds.Kind.INT,
|
141 | value: stringNum,
|
142 | }
|
143 | : {
|
144 | kind: _kinds.Kind.FLOAT,
|
145 | value: stringNum,
|
146 | };
|
147 | }
|
148 |
|
149 | if (typeof serialized === 'string') {
|
150 |
|
151 | if ((0, _definition.isEnumType)(type)) {
|
152 | return {
|
153 | kind: _kinds.Kind.ENUM,
|
154 | value: serialized,
|
155 | };
|
156 | }
|
157 |
|
158 | if (type === _scalars.GraphQLID && integerStringRegExp.test(serialized)) {
|
159 | return {
|
160 | kind: _kinds.Kind.INT,
|
161 | value: serialized,
|
162 | };
|
163 | }
|
164 |
|
165 | return {
|
166 | kind: _kinds.Kind.STRING,
|
167 | value: serialized,
|
168 | };
|
169 | }
|
170 |
|
171 | throw new TypeError(
|
172 | `Cannot convert value to AST: ${(0, _inspect.inspect)(serialized)}.`,
|
173 | );
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 | false ||
|
179 | (0, _invariant.invariant)(
|
180 | false,
|
181 | 'Unexpected input type: ' + (0, _inspect.inspect)(type),
|
182 | );
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
|