1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.astFromValue = void 0;
|
4 | const cross_inspect_1 = require("cross-inspect");
|
5 | const graphql_1 = require("graphql");
|
6 | const astFromValueUntyped_js_1 = require("./astFromValueUntyped.js");
|
7 | const jsutils_js_1 = require("./jsutils.js");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function astFromValue(value, type) {
|
31 | if ((0, graphql_1.isNonNullType)(type)) {
|
32 | const astValue = astFromValue(value, type.ofType);
|
33 | if (astValue?.kind === graphql_1.Kind.NULL) {
|
34 | return null;
|
35 | }
|
36 | return astValue;
|
37 | }
|
38 |
|
39 | if (value === null) {
|
40 | return { kind: graphql_1.Kind.NULL };
|
41 | }
|
42 |
|
43 | if (value === undefined) {
|
44 | return null;
|
45 | }
|
46 |
|
47 |
|
48 | if ((0, graphql_1.isListType)(type)) {
|
49 | const itemType = type.ofType;
|
50 | if ((0, jsutils_js_1.isIterableObject)(value)) {
|
51 | const valuesNodes = [];
|
52 | for (const item of value) {
|
53 | const itemNode = astFromValue(item, itemType);
|
54 | if (itemNode != null) {
|
55 | valuesNodes.push(itemNode);
|
56 | }
|
57 | }
|
58 | return { kind: graphql_1.Kind.LIST, values: valuesNodes };
|
59 | }
|
60 | return astFromValue(value, itemType);
|
61 | }
|
62 |
|
63 |
|
64 | if ((0, graphql_1.isInputObjectType)(type)) {
|
65 | if (!(0, jsutils_js_1.isObjectLike)(value)) {
|
66 | return null;
|
67 | }
|
68 | const fieldNodes = [];
|
69 | for (const field of Object.values(type.getFields())) {
|
70 | const fieldValue = astFromValue(value[field.name], field.type);
|
71 | if (fieldValue) {
|
72 | fieldNodes.push({
|
73 | kind: graphql_1.Kind.OBJECT_FIELD,
|
74 | name: { kind: graphql_1.Kind.NAME, value: field.name },
|
75 | value: fieldValue,
|
76 | });
|
77 | }
|
78 | }
|
79 | return { kind: graphql_1.Kind.OBJECT, fields: fieldNodes };
|
80 | }
|
81 | if ((0, graphql_1.isLeafType)(type)) {
|
82 |
|
83 |
|
84 | const serialized = type.serialize(value);
|
85 | if (serialized == null) {
|
86 | return null;
|
87 | }
|
88 | if ((0, graphql_1.isEnumType)(type)) {
|
89 | return { kind: graphql_1.Kind.ENUM, value: serialized };
|
90 | }
|
91 |
|
92 | if (type.name === 'ID' &&
|
93 | typeof serialized === 'string' &&
|
94 | integerStringRegExp.test(serialized)) {
|
95 | return { kind: graphql_1.Kind.INT, value: serialized };
|
96 | }
|
97 | return (0, astFromValueUntyped_js_1.astFromValueUntyped)(serialized);
|
98 | }
|
99 |
|
100 |
|
101 | console.assert(false, 'Unexpected input type: ' + (0, cross_inspect_1.inspect)(type));
|
102 | }
|
103 | exports.astFromValue = astFromValue;
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
|