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