1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseInputValueLiteral = exports.parseInputValue = exports.serializeInputValue = exports.transformInputValue = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const helpers_js_1 = require("./helpers.js");
|
6 | function transformInputValue(type, value, inputLeafValueTransformer = null, inputObjectValueTransformer = null) {
|
7 | if (value == null) {
|
8 | return value;
|
9 | }
|
10 | const nullableType = (0, graphql_1.getNullableType)(type);
|
11 | if ((0, graphql_1.isLeafType)(nullableType)) {
|
12 | return inputLeafValueTransformer != null
|
13 | ? inputLeafValueTransformer(nullableType, value)
|
14 | : value;
|
15 | }
|
16 | else if ((0, graphql_1.isListType)(nullableType)) {
|
17 | return (0, helpers_js_1.asArray)(value).map((listMember) => transformInputValue(nullableType.ofType, listMember, inputLeafValueTransformer, inputObjectValueTransformer));
|
18 | }
|
19 | else if ((0, graphql_1.isInputObjectType)(nullableType)) {
|
20 | const fields = nullableType.getFields();
|
21 | const newValue = {};
|
22 | for (const key in value) {
|
23 | const field = fields[key];
|
24 | if (field != null) {
|
25 | newValue[key] = transformInputValue(field.type, value[key], inputLeafValueTransformer, inputObjectValueTransformer);
|
26 | }
|
27 | }
|
28 | return inputObjectValueTransformer != null
|
29 | ? inputObjectValueTransformer(nullableType, newValue)
|
30 | : newValue;
|
31 | }
|
32 |
|
33 | }
|
34 | exports.transformInputValue = transformInputValue;
|
35 | function serializeInputValue(type, value) {
|
36 | return transformInputValue(type, value, (t, v) => {
|
37 | try {
|
38 | return t.serialize(v);
|
39 | }
|
40 | catch {
|
41 | return v;
|
42 | }
|
43 | });
|
44 | }
|
45 | exports.serializeInputValue = serializeInputValue;
|
46 | function parseInputValue(type, value) {
|
47 | return transformInputValue(type, value, (t, v) => {
|
48 | try {
|
49 | return t.parseValue(v);
|
50 | }
|
51 | catch {
|
52 | return v;
|
53 | }
|
54 | });
|
55 | }
|
56 | exports.parseInputValue = parseInputValue;
|
57 | function parseInputValueLiteral(type, value) {
|
58 | return transformInputValue(type, value, (t, v) => t.parseLiteral(v, {}));
|
59 | }
|
60 | exports.parseInputValueLiteral = parseInputValueLiteral;
|