UNPKG

2.31 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");
5const helpers_js_1 = require("./helpers.js");
6function 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 // unreachable, no other possible return value
33}
34exports.transformInputValue = transformInputValue;
35function 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}
45exports.serializeInputValue = serializeInputValue;
46function 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}
56exports.parseInputValue = parseInputValue;
57function parseInputValueLiteral(type, value) {
58 return transformInputValue(type, value, (t, v) => t.parseLiteral(v, {}));
59}
60exports.parseInputValueLiteral = parseInputValueLiteral;