visitSchema.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var __spreadArrays = (this && this.__spreadArrays) || function () {
  2. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  3. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  4. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  5. r[k] = a[j];
  6. return r;
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. var graphql_1 = require("graphql");
  10. var schemaRecreation_1 = require("../stitching/schemaRecreation");
  11. var VisitSchemaKind;
  12. (function (VisitSchemaKind) {
  13. VisitSchemaKind["TYPE"] = "VisitSchemaKind.TYPE";
  14. VisitSchemaKind["SCALAR_TYPE"] = "VisitSchemaKind.SCALAR_TYPE";
  15. VisitSchemaKind["ENUM_TYPE"] = "VisitSchemaKind.ENUM_TYPE";
  16. VisitSchemaKind["COMPOSITE_TYPE"] = "VisitSchemaKind.COMPOSITE_TYPE";
  17. VisitSchemaKind["OBJECT_TYPE"] = "VisitSchemaKind.OBJECT_TYPE";
  18. VisitSchemaKind["INPUT_OBJECT_TYPE"] = "VisitSchemaKind.INPUT_OBJECT_TYPE";
  19. VisitSchemaKind["ABSTRACT_TYPE"] = "VisitSchemaKind.ABSTRACT_TYPE";
  20. VisitSchemaKind["UNION_TYPE"] = "VisitSchemaKind.UNION_TYPE";
  21. VisitSchemaKind["INTERFACE_TYPE"] = "VisitSchemaKind.INTERFACE_TYPE";
  22. VisitSchemaKind["ROOT_OBJECT"] = "VisitSchemaKind.ROOT_OBJECT";
  23. VisitSchemaKind["QUERY"] = "VisitSchemaKind.QUERY";
  24. VisitSchemaKind["MUTATION"] = "VisitSchemaKind.MUTATION";
  25. VisitSchemaKind["SUBSCRIPTION"] = "VisitSchemaKind.SUBSCRIPTION";
  26. })(VisitSchemaKind = exports.VisitSchemaKind || (exports.VisitSchemaKind = {}));
  27. function visitSchema(schema, visitor, stripResolvers) {
  28. var types = {};
  29. var resolveType = schemaRecreation_1.createResolveType(function (name) {
  30. if (typeof types[name] === 'undefined') {
  31. throw new Error("Can't find type " + name + ".");
  32. }
  33. return types[name];
  34. });
  35. var queryType = schema.getQueryType();
  36. var mutationType = schema.getMutationType();
  37. var subscriptionType = schema.getSubscriptionType();
  38. var typeMap = schema.getTypeMap();
  39. Object.keys(typeMap).map(function (typeName) {
  40. var type = typeMap[typeName];
  41. if (graphql_1.isNamedType(type) && graphql_1.getNamedType(type).name.slice(0, 2) !== '__') {
  42. var specifiers = getTypeSpecifiers(type, schema);
  43. var typeVisitor = getVisitor(visitor, specifiers);
  44. if (typeVisitor) {
  45. var result = typeVisitor(type, schema);
  46. if (typeof result === 'undefined') {
  47. types[typeName] = schemaRecreation_1.recreateType(type, resolveType, !stripResolvers);
  48. }
  49. else if (result === null) {
  50. types[typeName] = null;
  51. }
  52. else {
  53. types[typeName] = schemaRecreation_1.recreateType(result, resolveType, !stripResolvers);
  54. }
  55. }
  56. else {
  57. types[typeName] = schemaRecreation_1.recreateType(type, resolveType, !stripResolvers);
  58. }
  59. }
  60. });
  61. return new graphql_1.GraphQLSchema({
  62. query: queryType ? types[queryType.name] : null,
  63. mutation: mutationType
  64. ? types[mutationType.name]
  65. : null,
  66. subscription: subscriptionType
  67. ? types[subscriptionType.name]
  68. : null,
  69. types: Object.keys(types).map(function (name) { return types[name]; }),
  70. });
  71. }
  72. exports.visitSchema = visitSchema;
  73. function getTypeSpecifiers(type, schema) {
  74. var specifiers = [VisitSchemaKind.TYPE];
  75. if (type instanceof graphql_1.GraphQLObjectType) {
  76. specifiers.unshift(VisitSchemaKind.COMPOSITE_TYPE, VisitSchemaKind.OBJECT_TYPE);
  77. var query = schema.getQueryType();
  78. var mutation = schema.getMutationType();
  79. var subscription = schema.getSubscriptionType();
  80. if (type === query) {
  81. specifiers.push(VisitSchemaKind.ROOT_OBJECT, VisitSchemaKind.QUERY);
  82. }
  83. else if (type === mutation) {
  84. specifiers.push(VisitSchemaKind.ROOT_OBJECT, VisitSchemaKind.MUTATION);
  85. }
  86. else if (type === subscription) {
  87. specifiers.push(VisitSchemaKind.ROOT_OBJECT, VisitSchemaKind.SUBSCRIPTION);
  88. }
  89. }
  90. else if (type instanceof graphql_1.GraphQLInputObjectType) {
  91. specifiers.push(VisitSchemaKind.INPUT_OBJECT_TYPE);
  92. }
  93. else if (type instanceof graphql_1.GraphQLInterfaceType) {
  94. specifiers.push(VisitSchemaKind.COMPOSITE_TYPE, VisitSchemaKind.ABSTRACT_TYPE, VisitSchemaKind.INTERFACE_TYPE);
  95. }
  96. else if (type instanceof graphql_1.GraphQLUnionType) {
  97. specifiers.push(VisitSchemaKind.COMPOSITE_TYPE, VisitSchemaKind.ABSTRACT_TYPE, VisitSchemaKind.UNION_TYPE);
  98. }
  99. else if (type instanceof graphql_1.GraphQLEnumType) {
  100. specifiers.push(VisitSchemaKind.ENUM_TYPE);
  101. }
  102. else if (type instanceof graphql_1.GraphQLScalarType) {
  103. specifiers.push(VisitSchemaKind.SCALAR_TYPE);
  104. }
  105. return specifiers;
  106. }
  107. function getVisitor(visitor, specifiers) {
  108. var typeVisitor = null;
  109. var stack = __spreadArrays(specifiers);
  110. while (!typeVisitor && stack.length > 0) {
  111. var next = stack.pop();
  112. typeVisitor = visitor[next];
  113. }
  114. return typeVisitor;
  115. }
  116. //# sourceMappingURL=visitSchema.js.map