1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.healTypes = exports.healSchema = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function healSchema(schema) {
|
34 | healTypes(schema.getTypeMap(), schema.getDirectives());
|
35 | return schema;
|
36 | }
|
37 | exports.healSchema = healSchema;
|
38 | function healTypes(originalTypeMap, directives) {
|
39 | const actualNamedTypeMap = Object.create(null);
|
40 |
|
41 |
|
42 |
|
43 | for (const typeName in originalTypeMap) {
|
44 | const namedType = originalTypeMap[typeName];
|
45 | if (namedType == null || typeName.startsWith('__')) {
|
46 | continue;
|
47 | }
|
48 | const actualName = namedType.name;
|
49 | if (actualName.startsWith('__')) {
|
50 | continue;
|
51 | }
|
52 | if (actualNamedTypeMap[actualName] != null) {
|
53 | console.warn(`Duplicate schema type name ${actualName} found; keeping the existing one found in the schema`);
|
54 | continue;
|
55 | }
|
56 | actualNamedTypeMap[actualName] = namedType;
|
57 |
|
58 |
|
59 |
|
60 | }
|
61 |
|
62 | for (const typeName in actualNamedTypeMap) {
|
63 | const namedType = actualNamedTypeMap[typeName];
|
64 | originalTypeMap[typeName] = namedType;
|
65 | }
|
66 |
|
67 | for (const decl of directives) {
|
68 | decl.args = decl.args.filter(arg => {
|
69 | arg.type = healType(arg.type);
|
70 | return arg.type !== null;
|
71 | });
|
72 | }
|
73 | for (const typeName in originalTypeMap) {
|
74 | const namedType = originalTypeMap[typeName];
|
75 |
|
76 | if (!typeName.startsWith('__') && typeName in actualNamedTypeMap) {
|
77 | if (namedType != null) {
|
78 | healNamedType(namedType);
|
79 | }
|
80 | }
|
81 | }
|
82 | for (const typeName in originalTypeMap) {
|
83 | if (!typeName.startsWith('__') && !(typeName in actualNamedTypeMap)) {
|
84 | delete originalTypeMap[typeName];
|
85 | }
|
86 | }
|
87 | function healNamedType(type) {
|
88 | if ((0, graphql_1.isObjectType)(type)) {
|
89 | healFields(type);
|
90 | healInterfaces(type);
|
91 | return;
|
92 | }
|
93 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
94 | healFields(type);
|
95 | if ('getInterfaces' in type) {
|
96 | healInterfaces(type);
|
97 | }
|
98 | return;
|
99 | }
|
100 | else if ((0, graphql_1.isUnionType)(type)) {
|
101 | healUnderlyingTypes(type);
|
102 | return;
|
103 | }
|
104 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
105 | healInputFields(type);
|
106 | return;
|
107 | }
|
108 | else if ((0, graphql_1.isLeafType)(type)) {
|
109 | return;
|
110 | }
|
111 | throw new Error(`Unexpected schema type: ${type}`);
|
112 | }
|
113 | function healFields(type) {
|
114 | const fieldMap = type.getFields();
|
115 | for (const [key, field] of Object.entries(fieldMap)) {
|
116 | field.args
|
117 | .map(arg => {
|
118 | arg.type = healType(arg.type);
|
119 | return arg.type === null ? null : arg;
|
120 | })
|
121 | .filter(Boolean);
|
122 | field.type = healType(field.type);
|
123 | if (field.type === null) {
|
124 | delete fieldMap[key];
|
125 | }
|
126 | }
|
127 | }
|
128 | function healInterfaces(type) {
|
129 | if ('getInterfaces' in type) {
|
130 | const interfaces = type.getInterfaces();
|
131 | interfaces.push(...interfaces
|
132 | .splice(0)
|
133 | .map(iface => healType(iface))
|
134 | .filter(Boolean));
|
135 | }
|
136 | }
|
137 | function healInputFields(type) {
|
138 | const fieldMap = type.getFields();
|
139 | for (const [key, field] of Object.entries(fieldMap)) {
|
140 | field.type = healType(field.type);
|
141 | if (field.type === null) {
|
142 | delete fieldMap[key];
|
143 | }
|
144 | }
|
145 | }
|
146 | function healUnderlyingTypes(type) {
|
147 | const types = type.getTypes();
|
148 | types.push(...types
|
149 | .splice(0)
|
150 | .map(t => healType(t))
|
151 | .filter(Boolean));
|
152 | }
|
153 | function healType(type) {
|
154 |
|
155 | if ((0, graphql_1.isListType)(type)) {
|
156 | const healedType = healType(type.ofType);
|
157 | return healedType != null ? new graphql_1.GraphQLList(healedType) : null;
|
158 | }
|
159 | else if ((0, graphql_1.isNonNullType)(type)) {
|
160 | const healedType = healType(type.ofType);
|
161 | return healedType != null ? new graphql_1.GraphQLNonNull(healedType) : null;
|
162 | }
|
163 | else if ((0, graphql_1.isNamedType)(type)) {
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | const officialType = originalTypeMap[type.name];
|
171 | if (officialType && type !== officialType) {
|
172 | return officialType;
|
173 | }
|
174 | }
|
175 | return type;
|
176 | }
|
177 | }
|
178 | exports.healTypes = healTypes;
|