1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.correctASTNodes = exports.mapSchema = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const getObjectTypeFromTypeMap_js_1 = require("./getObjectTypeFromTypeMap.js");
|
6 | const Interfaces_js_1 = require("./Interfaces.js");
|
7 | const rewire_js_1 = require("./rewire.js");
|
8 | const transformInputValue_js_1 = require("./transformInputValue.js");
|
9 | function mapSchema(schema, schemaMapper = {}) {
|
10 | const newTypeMap = mapArguments(mapFields(mapTypes(mapDefaultValues(mapEnumValues(mapTypes(mapDefaultValues(schema.getTypeMap(), schema, transformInputValue_js_1.serializeInputValue), schema, schemaMapper, type => (0, graphql_1.isLeafType)(type)), schema, schemaMapper), schema, transformInputValue_js_1.parseInputValue), schema, schemaMapper, type => !(0, graphql_1.isLeafType)(type)), schema, schemaMapper), schema, schemaMapper);
|
11 | const originalDirectives = schema.getDirectives();
|
12 | const newDirectives = mapDirectives(originalDirectives, schema, schemaMapper);
|
13 | const { typeMap, directives } = (0, rewire_js_1.rewireTypes)(newTypeMap, newDirectives);
|
14 | return new graphql_1.GraphQLSchema({
|
15 | ...schema.toConfig(),
|
16 | query: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(newTypeMap, schema.getQueryType())),
|
17 | mutation: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(newTypeMap, schema.getMutationType())),
|
18 | subscription: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(newTypeMap, schema.getSubscriptionType())),
|
19 | types: Object.values(typeMap),
|
20 | directives,
|
21 | });
|
22 | }
|
23 | exports.mapSchema = mapSchema;
|
24 | function mapTypes(originalTypeMap, schema, schemaMapper, testFn = () => true) {
|
25 | const newTypeMap = {};
|
26 | for (const typeName in originalTypeMap) {
|
27 | if (!typeName.startsWith('__')) {
|
28 | const originalType = originalTypeMap[typeName];
|
29 | if (originalType == null || !testFn(originalType)) {
|
30 | newTypeMap[typeName] = originalType;
|
31 | continue;
|
32 | }
|
33 | const typeMapper = getTypeMapper(schema, schemaMapper, typeName);
|
34 | if (typeMapper == null) {
|
35 | newTypeMap[typeName] = originalType;
|
36 | continue;
|
37 | }
|
38 | const maybeNewType = typeMapper(originalType, schema);
|
39 | if (maybeNewType === undefined) {
|
40 | newTypeMap[typeName] = originalType;
|
41 | continue;
|
42 | }
|
43 | newTypeMap[typeName] = maybeNewType;
|
44 | }
|
45 | }
|
46 | return newTypeMap;
|
47 | }
|
48 | function mapEnumValues(originalTypeMap, schema, schemaMapper) {
|
49 | const enumValueMapper = getEnumValueMapper(schemaMapper);
|
50 | if (!enumValueMapper) {
|
51 | return originalTypeMap;
|
52 | }
|
53 | return mapTypes(originalTypeMap, schema, {
|
54 | [Interfaces_js_1.MapperKind.ENUM_TYPE]: type => {
|
55 | const config = type.toConfig();
|
56 | const originalEnumValueConfigMap = config.values;
|
57 | const newEnumValueConfigMap = {};
|
58 | for (const externalValue in originalEnumValueConfigMap) {
|
59 | const originalEnumValueConfig = originalEnumValueConfigMap[externalValue];
|
60 | const mappedEnumValue = enumValueMapper(originalEnumValueConfig, type.name, schema, externalValue);
|
61 | if (mappedEnumValue === undefined) {
|
62 | newEnumValueConfigMap[externalValue] = originalEnumValueConfig;
|
63 | }
|
64 | else if (Array.isArray(mappedEnumValue)) {
|
65 | const [newExternalValue, newEnumValueConfig] = mappedEnumValue;
|
66 | newEnumValueConfigMap[newExternalValue] =
|
67 | newEnumValueConfig === undefined ? originalEnumValueConfig : newEnumValueConfig;
|
68 | }
|
69 | else if (mappedEnumValue !== null) {
|
70 | newEnumValueConfigMap[externalValue] = mappedEnumValue;
|
71 | }
|
72 | }
|
73 | return correctASTNodes(new graphql_1.GraphQLEnumType({
|
74 | ...config,
|
75 | values: newEnumValueConfigMap,
|
76 | }));
|
77 | },
|
78 | }, type => (0, graphql_1.isEnumType)(type));
|
79 | }
|
80 | function mapDefaultValues(originalTypeMap, schema, fn) {
|
81 | const newTypeMap = mapArguments(originalTypeMap, schema, {
|
82 | [Interfaces_js_1.MapperKind.ARGUMENT]: argumentConfig => {
|
83 | if (argumentConfig.defaultValue === undefined) {
|
84 | return argumentConfig;
|
85 | }
|
86 | const maybeNewType = getNewType(originalTypeMap, argumentConfig.type);
|
87 | if (maybeNewType != null) {
|
88 | return {
|
89 | ...argumentConfig,
|
90 | defaultValue: fn(maybeNewType, argumentConfig.defaultValue),
|
91 | };
|
92 | }
|
93 | },
|
94 | });
|
95 | return mapFields(newTypeMap, schema, {
|
96 | [Interfaces_js_1.MapperKind.INPUT_OBJECT_FIELD]: inputFieldConfig => {
|
97 | if (inputFieldConfig.defaultValue === undefined) {
|
98 | return inputFieldConfig;
|
99 | }
|
100 | const maybeNewType = getNewType(newTypeMap, inputFieldConfig.type);
|
101 | if (maybeNewType != null) {
|
102 | return {
|
103 | ...inputFieldConfig,
|
104 | defaultValue: fn(maybeNewType, inputFieldConfig.defaultValue),
|
105 | };
|
106 | }
|
107 | },
|
108 | });
|
109 | }
|
110 | function getNewType(newTypeMap, type) {
|
111 | if ((0, graphql_1.isListType)(type)) {
|
112 | const newType = getNewType(newTypeMap, type.ofType);
|
113 | return newType != null ? new graphql_1.GraphQLList(newType) : null;
|
114 | }
|
115 | else if ((0, graphql_1.isNonNullType)(type)) {
|
116 | const newType = getNewType(newTypeMap, type.ofType);
|
117 | return newType != null ? new graphql_1.GraphQLNonNull(newType) : null;
|
118 | }
|
119 | else if ((0, graphql_1.isNamedType)(type)) {
|
120 | const newType = newTypeMap[type.name];
|
121 | return newType != null ? newType : null;
|
122 | }
|
123 | return null;
|
124 | }
|
125 | function mapFields(originalTypeMap, schema, schemaMapper) {
|
126 | const newTypeMap = {};
|
127 | for (const typeName in originalTypeMap) {
|
128 | if (!typeName.startsWith('__')) {
|
129 | const originalType = originalTypeMap[typeName];
|
130 | if (!(0, graphql_1.isObjectType)(originalType) &&
|
131 | !(0, graphql_1.isInterfaceType)(originalType) &&
|
132 | !(0, graphql_1.isInputObjectType)(originalType)) {
|
133 | newTypeMap[typeName] = originalType;
|
134 | continue;
|
135 | }
|
136 | const fieldMapper = getFieldMapper(schema, schemaMapper, typeName);
|
137 | if (fieldMapper == null) {
|
138 | newTypeMap[typeName] = originalType;
|
139 | continue;
|
140 | }
|
141 | const config = originalType.toConfig();
|
142 | const originalFieldConfigMap = config.fields;
|
143 | const newFieldConfigMap = {};
|
144 | for (const fieldName in originalFieldConfigMap) {
|
145 | const originalFieldConfig = originalFieldConfigMap[fieldName];
|
146 | const mappedField = fieldMapper(originalFieldConfig, fieldName, typeName, schema);
|
147 | if (mappedField === undefined) {
|
148 | newFieldConfigMap[fieldName] = originalFieldConfig;
|
149 | }
|
150 | else if (Array.isArray(mappedField)) {
|
151 | const [newFieldName, newFieldConfig] = mappedField;
|
152 | if (newFieldConfig.astNode != null) {
|
153 | newFieldConfig.astNode = {
|
154 | ...newFieldConfig.astNode,
|
155 | name: {
|
156 | ...newFieldConfig.astNode.name,
|
157 | value: newFieldName,
|
158 | },
|
159 | };
|
160 | }
|
161 | newFieldConfigMap[newFieldName] =
|
162 | newFieldConfig === undefined ? originalFieldConfig : newFieldConfig;
|
163 | }
|
164 | else if (mappedField !== null) {
|
165 | newFieldConfigMap[fieldName] = mappedField;
|
166 | }
|
167 | }
|
168 | if ((0, graphql_1.isObjectType)(originalType)) {
|
169 | newTypeMap[typeName] = correctASTNodes(new graphql_1.GraphQLObjectType({
|
170 | ...config,
|
171 | fields: newFieldConfigMap,
|
172 | }));
|
173 | }
|
174 | else if ((0, graphql_1.isInterfaceType)(originalType)) {
|
175 | newTypeMap[typeName] = correctASTNodes(new graphql_1.GraphQLInterfaceType({
|
176 | ...config,
|
177 | fields: newFieldConfigMap,
|
178 | }));
|
179 | }
|
180 | else {
|
181 | newTypeMap[typeName] = correctASTNodes(new graphql_1.GraphQLInputObjectType({
|
182 | ...config,
|
183 | fields: newFieldConfigMap,
|
184 | }));
|
185 | }
|
186 | }
|
187 | }
|
188 | return newTypeMap;
|
189 | }
|
190 | function mapArguments(originalTypeMap, schema, schemaMapper) {
|
191 | const newTypeMap = {};
|
192 | for (const typeName in originalTypeMap) {
|
193 | if (!typeName.startsWith('__')) {
|
194 | const originalType = originalTypeMap[typeName];
|
195 | if (!(0, graphql_1.isObjectType)(originalType) && !(0, graphql_1.isInterfaceType)(originalType)) {
|
196 | newTypeMap[typeName] = originalType;
|
197 | continue;
|
198 | }
|
199 | const argumentMapper = getArgumentMapper(schemaMapper);
|
200 | if (argumentMapper == null) {
|
201 | newTypeMap[typeName] = originalType;
|
202 | continue;
|
203 | }
|
204 | const config = originalType.toConfig();
|
205 | const originalFieldConfigMap = config.fields;
|
206 | const newFieldConfigMap = {};
|
207 | for (const fieldName in originalFieldConfigMap) {
|
208 | const originalFieldConfig = originalFieldConfigMap[fieldName];
|
209 | const originalArgumentConfigMap = originalFieldConfig.args;
|
210 | if (originalArgumentConfigMap == null) {
|
211 | newFieldConfigMap[fieldName] = originalFieldConfig;
|
212 | continue;
|
213 | }
|
214 | const argumentNames = Object.keys(originalArgumentConfigMap);
|
215 | if (!argumentNames.length) {
|
216 | newFieldConfigMap[fieldName] = originalFieldConfig;
|
217 | continue;
|
218 | }
|
219 | const newArgumentConfigMap = {};
|
220 | for (const argumentName of argumentNames) {
|
221 | const originalArgumentConfig = originalArgumentConfigMap[argumentName];
|
222 | const mappedArgument = argumentMapper(originalArgumentConfig, fieldName, typeName, schema);
|
223 | if (mappedArgument === undefined) {
|
224 | newArgumentConfigMap[argumentName] = originalArgumentConfig;
|
225 | }
|
226 | else if (Array.isArray(mappedArgument)) {
|
227 | const [newArgumentName, newArgumentConfig] = mappedArgument;
|
228 | newArgumentConfigMap[newArgumentName] = newArgumentConfig;
|
229 | }
|
230 | else if (mappedArgument !== null) {
|
231 | newArgumentConfigMap[argumentName] = mappedArgument;
|
232 | }
|
233 | }
|
234 | newFieldConfigMap[fieldName] = {
|
235 | ...originalFieldConfig,
|
236 | args: newArgumentConfigMap,
|
237 | };
|
238 | }
|
239 | if ((0, graphql_1.isObjectType)(originalType)) {
|
240 | newTypeMap[typeName] = new graphql_1.GraphQLObjectType({
|
241 | ...config,
|
242 | fields: newFieldConfigMap,
|
243 | });
|
244 | }
|
245 | else if ((0, graphql_1.isInterfaceType)(originalType)) {
|
246 | newTypeMap[typeName] = new graphql_1.GraphQLInterfaceType({
|
247 | ...config,
|
248 | fields: newFieldConfigMap,
|
249 | });
|
250 | }
|
251 | else {
|
252 | newTypeMap[typeName] = new graphql_1.GraphQLInputObjectType({
|
253 | ...config,
|
254 | fields: newFieldConfigMap,
|
255 | });
|
256 | }
|
257 | }
|
258 | }
|
259 | return newTypeMap;
|
260 | }
|
261 | function mapDirectives(originalDirectives, schema, schemaMapper) {
|
262 | const directiveMapper = getDirectiveMapper(schemaMapper);
|
263 | if (directiveMapper == null) {
|
264 | return originalDirectives.slice();
|
265 | }
|
266 | const newDirectives = [];
|
267 | for (const directive of originalDirectives) {
|
268 | const mappedDirective = directiveMapper(directive, schema);
|
269 | if (mappedDirective === undefined) {
|
270 | newDirectives.push(directive);
|
271 | }
|
272 | else if (mappedDirective !== null) {
|
273 | newDirectives.push(mappedDirective);
|
274 | }
|
275 | }
|
276 | return newDirectives;
|
277 | }
|
278 | function getTypeSpecifiers(schema, typeName) {
|
279 | const type = schema.getType(typeName);
|
280 | const specifiers = [Interfaces_js_1.MapperKind.TYPE];
|
281 | if ((0, graphql_1.isObjectType)(type)) {
|
282 | specifiers.push(Interfaces_js_1.MapperKind.COMPOSITE_TYPE, Interfaces_js_1.MapperKind.OBJECT_TYPE);
|
283 | if (typeName === schema.getQueryType()?.name) {
|
284 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_OBJECT, Interfaces_js_1.MapperKind.QUERY);
|
285 | }
|
286 | else if (typeName === schema.getMutationType()?.name) {
|
287 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_OBJECT, Interfaces_js_1.MapperKind.MUTATION);
|
288 | }
|
289 | else if (typeName === schema.getSubscriptionType()?.name) {
|
290 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_OBJECT, Interfaces_js_1.MapperKind.SUBSCRIPTION);
|
291 | }
|
292 | }
|
293 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
294 | specifiers.push(Interfaces_js_1.MapperKind.INPUT_OBJECT_TYPE);
|
295 | }
|
296 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
297 | specifiers.push(Interfaces_js_1.MapperKind.COMPOSITE_TYPE, Interfaces_js_1.MapperKind.ABSTRACT_TYPE, Interfaces_js_1.MapperKind.INTERFACE_TYPE);
|
298 | }
|
299 | else if ((0, graphql_1.isUnionType)(type)) {
|
300 | specifiers.push(Interfaces_js_1.MapperKind.COMPOSITE_TYPE, Interfaces_js_1.MapperKind.ABSTRACT_TYPE, Interfaces_js_1.MapperKind.UNION_TYPE);
|
301 | }
|
302 | else if ((0, graphql_1.isEnumType)(type)) {
|
303 | specifiers.push(Interfaces_js_1.MapperKind.ENUM_TYPE);
|
304 | }
|
305 | else if ((0, graphql_1.isScalarType)(type)) {
|
306 | specifiers.push(Interfaces_js_1.MapperKind.SCALAR_TYPE);
|
307 | }
|
308 | return specifiers;
|
309 | }
|
310 | function getTypeMapper(schema, schemaMapper, typeName) {
|
311 | const specifiers = getTypeSpecifiers(schema, typeName);
|
312 | let typeMapper;
|
313 | const stack = [...specifiers];
|
314 | while (!typeMapper && stack.length > 0) {
|
315 |
|
316 | const next = stack.pop();
|
317 | typeMapper = schemaMapper[next];
|
318 | }
|
319 | return typeMapper != null ? typeMapper : null;
|
320 | }
|
321 | function getFieldSpecifiers(schema, typeName) {
|
322 | const type = schema.getType(typeName);
|
323 | const specifiers = [Interfaces_js_1.MapperKind.FIELD];
|
324 | if ((0, graphql_1.isObjectType)(type)) {
|
325 | specifiers.push(Interfaces_js_1.MapperKind.COMPOSITE_FIELD, Interfaces_js_1.MapperKind.OBJECT_FIELD);
|
326 | if (typeName === schema.getQueryType()?.name) {
|
327 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_FIELD, Interfaces_js_1.MapperKind.QUERY_ROOT_FIELD);
|
328 | }
|
329 | else if (typeName === schema.getMutationType()?.name) {
|
330 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_FIELD, Interfaces_js_1.MapperKind.MUTATION_ROOT_FIELD);
|
331 | }
|
332 | else if (typeName === schema.getSubscriptionType()?.name) {
|
333 | specifiers.push(Interfaces_js_1.MapperKind.ROOT_FIELD, Interfaces_js_1.MapperKind.SUBSCRIPTION_ROOT_FIELD);
|
334 | }
|
335 | }
|
336 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
337 | specifiers.push(Interfaces_js_1.MapperKind.COMPOSITE_FIELD, Interfaces_js_1.MapperKind.INTERFACE_FIELD);
|
338 | }
|
339 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
340 | specifiers.push(Interfaces_js_1.MapperKind.INPUT_OBJECT_FIELD);
|
341 | }
|
342 | return specifiers;
|
343 | }
|
344 | function getFieldMapper(schema, schemaMapper, typeName) {
|
345 | const specifiers = getFieldSpecifiers(schema, typeName);
|
346 | let fieldMapper;
|
347 | const stack = [...specifiers];
|
348 | while (!fieldMapper && stack.length > 0) {
|
349 |
|
350 | const next = stack.pop();
|
351 |
|
352 | fieldMapper = schemaMapper[next];
|
353 | }
|
354 | return fieldMapper ?? null;
|
355 | }
|
356 | function getArgumentMapper(schemaMapper) {
|
357 | const argumentMapper = schemaMapper[Interfaces_js_1.MapperKind.ARGUMENT];
|
358 | return argumentMapper != null ? argumentMapper : null;
|
359 | }
|
360 | function getDirectiveMapper(schemaMapper) {
|
361 | const directiveMapper = schemaMapper[Interfaces_js_1.MapperKind.DIRECTIVE];
|
362 | return directiveMapper != null ? directiveMapper : null;
|
363 | }
|
364 | function getEnumValueMapper(schemaMapper) {
|
365 | const enumValueMapper = schemaMapper[Interfaces_js_1.MapperKind.ENUM_VALUE];
|
366 | return enumValueMapper != null ? enumValueMapper : null;
|
367 | }
|
368 | function correctASTNodes(type) {
|
369 | if ((0, graphql_1.isObjectType)(type)) {
|
370 | const config = type.toConfig();
|
371 | if (config.astNode != null) {
|
372 | const fields = [];
|
373 | for (const fieldName in config.fields) {
|
374 | const fieldConfig = config.fields[fieldName];
|
375 | if (fieldConfig.astNode != null) {
|
376 | fields.push(fieldConfig.astNode);
|
377 | }
|
378 | }
|
379 | config.astNode = {
|
380 | ...config.astNode,
|
381 | kind: graphql_1.Kind.OBJECT_TYPE_DEFINITION,
|
382 | fields,
|
383 | };
|
384 | }
|
385 | if (config.extensionASTNodes != null) {
|
386 | config.extensionASTNodes = config.extensionASTNodes.map(node => ({
|
387 | ...node,
|
388 | kind: graphql_1.Kind.OBJECT_TYPE_EXTENSION,
|
389 | fields: undefined,
|
390 | }));
|
391 | }
|
392 | return new graphql_1.GraphQLObjectType(config);
|
393 | }
|
394 | else if ((0, graphql_1.isInterfaceType)(type)) {
|
395 | const config = type.toConfig();
|
396 | if (config.astNode != null) {
|
397 | const fields = [];
|
398 | for (const fieldName in config.fields) {
|
399 | const fieldConfig = config.fields[fieldName];
|
400 | if (fieldConfig.astNode != null) {
|
401 | fields.push(fieldConfig.astNode);
|
402 | }
|
403 | }
|
404 | config.astNode = {
|
405 | ...config.astNode,
|
406 | kind: graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
|
407 | fields,
|
408 | };
|
409 | }
|
410 | if (config.extensionASTNodes != null) {
|
411 | config.extensionASTNodes = config.extensionASTNodes.map(node => ({
|
412 | ...node,
|
413 | kind: graphql_1.Kind.INTERFACE_TYPE_EXTENSION,
|
414 | fields: undefined,
|
415 | }));
|
416 | }
|
417 | return new graphql_1.GraphQLInterfaceType(config);
|
418 | }
|
419 | else if ((0, graphql_1.isInputObjectType)(type)) {
|
420 | const config = type.toConfig();
|
421 | if (config.astNode != null) {
|
422 | const fields = [];
|
423 | for (const fieldName in config.fields) {
|
424 | const fieldConfig = config.fields[fieldName];
|
425 | if (fieldConfig.astNode != null) {
|
426 | fields.push(fieldConfig.astNode);
|
427 | }
|
428 | }
|
429 | config.astNode = {
|
430 | ...config.astNode,
|
431 | kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
432 | fields,
|
433 | };
|
434 | }
|
435 | if (config.extensionASTNodes != null) {
|
436 | config.extensionASTNodes = config.extensionASTNodes.map(node => ({
|
437 | ...node,
|
438 | kind: graphql_1.Kind.INPUT_OBJECT_TYPE_EXTENSION,
|
439 | fields: undefined,
|
440 | }));
|
441 | }
|
442 | return new graphql_1.GraphQLInputObjectType(config);
|
443 | }
|
444 | else if ((0, graphql_1.isEnumType)(type)) {
|
445 | const config = type.toConfig();
|
446 | if (config.astNode != null) {
|
447 | const values = [];
|
448 | for (const enumKey in config.values) {
|
449 | const enumValueConfig = config.values[enumKey];
|
450 | if (enumValueConfig.astNode != null) {
|
451 | values.push(enumValueConfig.astNode);
|
452 | }
|
453 | }
|
454 | config.astNode = {
|
455 | ...config.astNode,
|
456 | values,
|
457 | };
|
458 | }
|
459 | if (config.extensionASTNodes != null) {
|
460 | config.extensionASTNodes = config.extensionASTNodes.map(node => ({
|
461 | ...node,
|
462 | values: undefined,
|
463 | }));
|
464 | }
|
465 | return new graphql_1.GraphQLEnumType(config);
|
466 | }
|
467 | else {
|
468 | return type;
|
469 | }
|
470 | }
|
471 | exports.correctASTNodes = correctASTNodes;
|