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 (actualName in actualNamedTypeMap) {
|
53 | throw new Error(`Duplicate schema type name ${actualName}`);
|
54 | }
|
55 | actualNamedTypeMap[actualName] = namedType;
|
56 |
|
57 |
|
58 |
|
59 | }
|
60 |
|
61 | for (const typeName in actualNamedTypeMap) {
|
62 | const namedType = actualNamedTypeMap[typeName];
|
63 | originalTypeMap[typeName] = namedType;
|
64 | }
|
65 |
|
66 | for (const decl of directives) {
|
67 | decl.args = decl.args.filter(arg => {
|
68 | arg.type = healType(arg.type);
|
69 | return arg.type !== null;
|
70 | });
|
71 | }
|
72 | for (const typeName in originalTypeMap) {
|
73 | const namedType = originalTypeMap[typeName];
|
74 |
|
75 | if (!typeName.startsWith('__') && typeName in actualNamedTypeMap) {
|
76 | if (namedType != null) {
|
77 | healNamedType(namedType);
|
78 | }
|
79 | }
|
80 | }
|
81 | for (const typeName in originalTypeMap) {
|
82 | if (!typeName.startsWith('__') && !(typeName in actualNamedTypeMap)) {
|
83 | delete originalTypeMap[typeName];
|
84 | }
|
85 | }
|
86 | function healNamedType(type) {
|
87 | if ((0, graphql_1.isObjectType)(type)) {
|
88 | healFields(type);
|
89 | healInterfaces(type);
|
90 | return;
|
91 | }
|
92 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
93 | healFields(type);
|
94 | if ('getInterfaces' in type) {
|
95 | healInterfaces(type);
|
96 | }
|
97 | return;
|
98 | }
|
99 | else if ((0, graphql_1.isUnionType)(type)) {
|
100 | healUnderlyingTypes(type);
|
101 | return;
|
102 | }
|
103 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
104 | healInputFields(type);
|
105 | return;
|
106 | }
|
107 | else if ((0, graphql_1.isLeafType)(type)) {
|
108 | return;
|
109 | }
|
110 | throw new Error(`Unexpected schema type: ${type}`);
|
111 | }
|
112 | function healFields(type) {
|
113 | const fieldMap = type.getFields();
|
114 | for (const [key, field] of Object.entries(fieldMap)) {
|
115 | field.args
|
116 | .map(arg => {
|
117 | arg.type = healType(arg.type);
|
118 | return arg.type === null ? null : arg;
|
119 | })
|
120 | .filter(Boolean);
|
121 | field.type = healType(field.type);
|
122 | if (field.type === null) {
|
123 | delete fieldMap[key];
|
124 | }
|
125 | }
|
126 | }
|
127 | function healInterfaces(type) {
|
128 | if ('getInterfaces' in type) {
|
129 | const interfaces = type.getInterfaces();
|
130 | interfaces.push(...interfaces
|
131 | .splice(0)
|
132 | .map(iface => healType(iface))
|
133 | .filter(Boolean));
|
134 | }
|
135 | }
|
136 | function healInputFields(type) {
|
137 | const fieldMap = type.getFields();
|
138 | for (const [key, field] of Object.entries(fieldMap)) {
|
139 | field.type = healType(field.type);
|
140 | if (field.type === null) {
|
141 | delete fieldMap[key];
|
142 | }
|
143 | }
|
144 | }
|
145 | function healUnderlyingTypes(type) {
|
146 | const types = type.getTypes();
|
147 | types.push(...types
|
148 | .splice(0)
|
149 | .map(t => healType(t))
|
150 | .filter(Boolean));
|
151 | }
|
152 | function healType(type) {
|
153 |
|
154 | if ((0, graphql_1.isListType)(type)) {
|
155 | const healedType = healType(type.ofType);
|
156 | return healedType != null ? new graphql_1.GraphQLList(healedType) : null;
|
157 | }
|
158 | else if ((0, graphql_1.isNonNullType)(type)) {
|
159 | const healedType = healType(type.ofType);
|
160 | return healedType != null ? new graphql_1.GraphQLNonNull(healedType) : null;
|
161 | }
|
162 | else if ((0, graphql_1.isNamedType)(type)) {
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | const officialType = originalTypeMap[type.name];
|
170 | if (officialType && type !== officialType) {
|
171 | return officialType;
|
172 | }
|
173 | }
|
174 | return type;
|
175 | }
|
176 | }
|
177 | exports.healTypes = healTypes;
|