transformInputValue.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. function 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. }
  29. exports.transformInputValue = transformInputValue;
  30. function 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. }
  40. exports.serializeInputValue = serializeInputValue;
  41. function 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. }
  51. exports.parseInputValue = parseInputValue;
  52. function parseInputValueLiteral(type, value) {
  53. return transformInputValue(type, value, (t, v) => t.parseLiteral(v, {}));
  54. }
  55. exports.parseInputValueLiteral = parseInputValueLiteral;