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