1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.rewireTypes = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const stub_js_1 = require("./stub.js");
|
6 | function rewireTypes(originalTypeMap, directives) {
|
7 | const referenceTypeMap = Object.create(null);
|
8 | for (const typeName in originalTypeMap) {
|
9 | referenceTypeMap[typeName] = originalTypeMap[typeName];
|
10 | }
|
11 | const newTypeMap = Object.create(null);
|
12 | for (const typeName in referenceTypeMap) {
|
13 | const namedType = referenceTypeMap[typeName];
|
14 | if (namedType == null || typeName.startsWith('__')) {
|
15 | continue;
|
16 | }
|
17 | const newName = namedType.name;
|
18 | if (newName.startsWith('__')) {
|
19 | continue;
|
20 | }
|
21 | if (newTypeMap[newName] != null) {
|
22 | console.warn(`Duplicate schema type name ${newName} found; keeping the existing one found in the schema`);
|
23 | continue;
|
24 | }
|
25 | newTypeMap[newName] = namedType;
|
26 | }
|
27 | for (const typeName in newTypeMap) {
|
28 | newTypeMap[typeName] = rewireNamedType(newTypeMap[typeName]);
|
29 | }
|
30 | const newDirectives = directives.map(directive => rewireDirective(directive));
|
31 | return {
|
32 | typeMap: newTypeMap,
|
33 | directives: newDirectives,
|
34 | };
|
35 | function rewireDirective(directive) {
|
36 | if ((0, graphql_1.isSpecifiedDirective)(directive)) {
|
37 | return directive;
|
38 | }
|
39 | const directiveConfig = directive.toConfig();
|
40 | directiveConfig.args = rewireArgs(directiveConfig.args);
|
41 | return new graphql_1.GraphQLDirective(directiveConfig);
|
42 | }
|
43 | function rewireArgs(args) {
|
44 | const rewiredArgs = {};
|
45 | for (const argName in args) {
|
46 | const arg = args[argName];
|
47 | const rewiredArgType = rewireType(arg.type);
|
48 | if (rewiredArgType != null) {
|
49 | arg.type = rewiredArgType;
|
50 | rewiredArgs[argName] = arg;
|
51 | }
|
52 | }
|
53 | return rewiredArgs;
|
54 | }
|
55 | function rewireNamedType(type) {
|
56 | if ((0, graphql_1.isObjectType)(type)) {
|
57 | const config = type.toConfig();
|
58 | const newConfig = {
|
59 | ...config,
|
60 | fields: () => rewireFields(config.fields),
|
61 | interfaces: () => rewireNamedTypes(config.interfaces),
|
62 | };
|
63 | return new graphql_1.GraphQLObjectType(newConfig);
|
64 | }
|
65 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
66 | const config = type.toConfig();
|
67 | const newConfig = {
|
68 | ...config,
|
69 | fields: () => rewireFields(config.fields),
|
70 | };
|
71 | if ('interfaces' in newConfig) {
|
72 | newConfig.interfaces = () => rewireNamedTypes(config.interfaces);
|
73 | }
|
74 | return new graphql_1.GraphQLInterfaceType(newConfig);
|
75 | }
|
76 | else if ((0, graphql_1.isUnionType)(type)) {
|
77 | const config = type.toConfig();
|
78 | const newConfig = {
|
79 | ...config,
|
80 | types: () => rewireNamedTypes(config.types),
|
81 | };
|
82 | return new graphql_1.GraphQLUnionType(newConfig);
|
83 | }
|
84 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
85 | const config = type.toConfig();
|
86 | const newConfig = {
|
87 | ...config,
|
88 | fields: () => rewireInputFields(config.fields),
|
89 | };
|
90 | return new graphql_1.GraphQLInputObjectType(newConfig);
|
91 | }
|
92 | else if ((0, graphql_1.isEnumType)(type)) {
|
93 | const enumConfig = type.toConfig();
|
94 | return new graphql_1.GraphQLEnumType(enumConfig);
|
95 | }
|
96 | else if ((0, graphql_1.isScalarType)(type)) {
|
97 | if ((0, graphql_1.isSpecifiedScalarType)(type)) {
|
98 | return type;
|
99 | }
|
100 | const scalarConfig = type.toConfig();
|
101 | return new graphql_1.GraphQLScalarType(scalarConfig);
|
102 | }
|
103 | throw new Error(`Unexpected schema type: ${type}`);
|
104 | }
|
105 | function rewireFields(fields) {
|
106 | const rewiredFields = {};
|
107 | for (const fieldName in fields) {
|
108 | const field = fields[fieldName];
|
109 | const rewiredFieldType = rewireType(field.type);
|
110 | if (rewiredFieldType != null && field.args) {
|
111 | field.type = rewiredFieldType;
|
112 | field.args = rewireArgs(field.args);
|
113 | rewiredFields[fieldName] = field;
|
114 | }
|
115 | }
|
116 | return rewiredFields;
|
117 | }
|
118 | function rewireInputFields(fields) {
|
119 | const rewiredFields = {};
|
120 | for (const fieldName in fields) {
|
121 | const field = fields[fieldName];
|
122 | const rewiredFieldType = rewireType(field.type);
|
123 | if (rewiredFieldType != null) {
|
124 | field.type = rewiredFieldType;
|
125 | rewiredFields[fieldName] = field;
|
126 | }
|
127 | }
|
128 | return rewiredFields;
|
129 | }
|
130 | function rewireNamedTypes(namedTypes) {
|
131 | const rewiredTypes = [];
|
132 | for (const namedType of namedTypes) {
|
133 | const rewiredType = rewireType(namedType);
|
134 | if (rewiredType != null) {
|
135 | rewiredTypes.push(rewiredType);
|
136 | }
|
137 | }
|
138 | return rewiredTypes;
|
139 | }
|
140 | function rewireType(type) {
|
141 | if ((0, graphql_1.isListType)(type)) {
|
142 | const rewiredType = rewireType(type.ofType);
|
143 | return rewiredType != null ? new graphql_1.GraphQLList(rewiredType) : null;
|
144 | }
|
145 | else if ((0, graphql_1.isNonNullType)(type)) {
|
146 | const rewiredType = rewireType(type.ofType);
|
147 | return rewiredType != null ? new graphql_1.GraphQLNonNull(rewiredType) : null;
|
148 | }
|
149 | else if ((0, graphql_1.isNamedType)(type)) {
|
150 | let rewiredType = referenceTypeMap[type.name];
|
151 | if (rewiredType === undefined) {
|
152 | rewiredType = (0, stub_js_1.isNamedStub)(type) ? (0, stub_js_1.getBuiltInForStub)(type) : rewireNamedType(type);
|
153 | newTypeMap[rewiredType.name] = referenceTypeMap[type.name] = rewiredType;
|
154 | }
|
155 | return rewiredType != null ? newTypeMap[rewiredType.name] : null;
|
156 | }
|
157 | return null;
|
158 | }
|
159 | }
|
160 | exports.rewireTypes = rewireTypes;
|