UNPKG

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