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