transformSchema.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformSchema = void 0;
  4. const graphql_1 = require("graphql");
  5. const mapValues_1 = require("../utilities/mapValues");
  6. function transformSchema(schema, transformType) {
  7. const typeMap = Object.create(null);
  8. for (const oldType of Object.values(schema.getTypeMap())) {
  9. if ((0, graphql_1.isIntrospectionType)(oldType))
  10. continue;
  11. const result = transformType(oldType);
  12. if (result === null)
  13. continue;
  14. const newType = result || oldType;
  15. typeMap[newType.name] = recreateNamedType(newType);
  16. }
  17. const schemaConfig = schema.toConfig();
  18. return new graphql_1.GraphQLSchema(Object.assign(Object.assign({}, schemaConfig), { types: Object.values(typeMap), query: replaceMaybeType(schemaConfig.query), mutation: replaceMaybeType(schemaConfig.mutation), subscription: replaceMaybeType(schemaConfig.subscription), directives: replaceDirectives(schemaConfig.directives) }));
  19. function recreateNamedType(type) {
  20. if ((0, graphql_1.isObjectType)(type)) {
  21. const config = type.toConfig();
  22. return new graphql_1.GraphQLObjectType(Object.assign(Object.assign({}, config), { interfaces: () => config.interfaces.map(replaceNamedType), fields: () => replaceFields(config.fields) }));
  23. }
  24. else if ((0, graphql_1.isInterfaceType)(type)) {
  25. const config = type.toConfig();
  26. return new graphql_1.GraphQLInterfaceType(Object.assign(Object.assign({}, config), { interfaces: () => config.interfaces.map(replaceNamedType), fields: () => replaceFields(config.fields) }));
  27. }
  28. else if ((0, graphql_1.isUnionType)(type)) {
  29. const config = type.toConfig();
  30. return new graphql_1.GraphQLUnionType(Object.assign(Object.assign({}, config), { types: () => config.types.map(replaceNamedType) }));
  31. }
  32. else if ((0, graphql_1.isInputObjectType)(type)) {
  33. const config = type.toConfig();
  34. return new graphql_1.GraphQLInputObjectType(Object.assign(Object.assign({}, config), { fields: () => replaceInputFields(config.fields) }));
  35. }
  36. return type;
  37. }
  38. function replaceType(type) {
  39. if ((0, graphql_1.isListType)(type)) {
  40. return new graphql_1.GraphQLList(replaceType(type.ofType));
  41. }
  42. else if ((0, graphql_1.isNonNullType)(type)) {
  43. return new graphql_1.GraphQLNonNull(replaceType(type.ofType));
  44. }
  45. return replaceNamedType(type);
  46. }
  47. function replaceNamedType(type) {
  48. const newType = typeMap[type.name];
  49. return newType ? newType : type;
  50. }
  51. function replaceMaybeType(type) {
  52. return type ? replaceNamedType(type) : undefined;
  53. }
  54. function replaceFields(fieldsMap) {
  55. return (0, mapValues_1.mapValues)(fieldsMap, (field) => (Object.assign(Object.assign({}, field), { type: replaceType(field.type), args: field.args ? replaceArgs(field.args) : undefined })));
  56. }
  57. function replaceInputFields(fieldsMap) {
  58. return (0, mapValues_1.mapValues)(fieldsMap, (field) => (Object.assign(Object.assign({}, field), { type: replaceType(field.type) })));
  59. }
  60. function replaceArgs(args) {
  61. return (0, mapValues_1.mapValues)(args, (arg) => (Object.assign(Object.assign({}, arg), { type: replaceType(arg.type) })));
  62. }
  63. function replaceDirectives(directives) {
  64. return directives.map((directive) => {
  65. const config = directive.toConfig();
  66. return new graphql_1.GraphQLDirective(Object.assign(Object.assign({}, config), { args: replaceArgs(config.args) }));
  67. });
  68. }
  69. }
  70. exports.transformSchema = transformSchema;
  71. //# sourceMappingURL=transformSchema.js.map