ConvertEnumValues.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. Object.defineProperty(exports, "__esModule", { value: true });
  2. var graphql_1 = require("graphql");
  3. var visitSchema_1 = require("../transforms/visitSchema");
  4. // Transformation used to modifiy `GraphQLEnumType` values in a schema.
  5. var ConvertEnumValues = /** @class */ (function () {
  6. function ConvertEnumValues(enumValueMap) {
  7. this.enumValueMap = enumValueMap;
  8. }
  9. // Walk a schema looking for `GraphQLEnumType` types. If found, and
  10. // matching types have been identified in `this.enumValueMap`, create new
  11. // `GraphQLEnumType` types using the `this.enumValueMap` specified new
  12. // values, and return them in the new schema.
  13. ConvertEnumValues.prototype.transformSchema = function (schema) {
  14. var _a;
  15. var enumValueMap = this.enumValueMap;
  16. if (!enumValueMap || Object.keys(enumValueMap).length === 0) {
  17. return schema;
  18. }
  19. var transformedSchema = visitSchema_1.visitSchema(schema, (_a = {},
  20. _a[visitSchema_1.VisitSchemaKind.ENUM_TYPE] = function (enumType) {
  21. var externalToInternalValueMap = enumValueMap[enumType.name];
  22. if (externalToInternalValueMap) {
  23. var values = enumType.getValues();
  24. var newValues_1 = {};
  25. values.forEach(function (value) {
  26. var newValue = Object.keys(externalToInternalValueMap).includes(value.name)
  27. ? externalToInternalValueMap[value.name]
  28. : value.name;
  29. newValues_1[value.name] = {
  30. value: newValue,
  31. deprecationReason: value.deprecationReason,
  32. description: value.description,
  33. astNode: value.astNode,
  34. };
  35. });
  36. return new graphql_1.GraphQLEnumType({
  37. name: enumType.name,
  38. description: enumType.description,
  39. astNode: enumType.astNode,
  40. values: newValues_1,
  41. });
  42. }
  43. return enumType;
  44. },
  45. _a));
  46. return transformedSchema;
  47. };
  48. return ConvertEnumValues;
  49. }());
  50. exports.default = ConvertEnumValues;
  51. //# sourceMappingURL=ConvertEnumValues.js.map