1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | const graphql = require('graphql');
|
6 | const utils = require('@graphql-tools/utils');
|
7 | const delegate = require('@graphql-tools/delegate');
|
8 | const valueOrPromise = require('value-or-promise');
|
9 |
|
10 | function generateProxyingResolvers(subschemaConfig) {
|
11 | var _a;
|
12 | const targetSchema = subschemaConfig.schema;
|
13 | const createProxyingResolver = (_a = subschemaConfig.createProxyingResolver) !== null && _a !== void 0 ? _a : defaultCreateProxyingResolver;
|
14 | const transformedSchema = delegate.applySchemaTransforms(targetSchema, subschemaConfig);
|
15 | const rootTypeMap = utils.getRootTypeMap(targetSchema);
|
16 | const resolvers = {};
|
17 | for (const [operation, rootType] of rootTypeMap.entries()) {
|
18 | const typeName = rootType.name;
|
19 | const fields = rootType.getFields();
|
20 | resolvers[typeName] = {};
|
21 | for (const fieldName in fields) {
|
22 | const proxyingResolver = createProxyingResolver({
|
23 | subschemaConfig,
|
24 | transformedSchema,
|
25 | operation,
|
26 | fieldName,
|
27 | });
|
28 | const finalResolver = createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver);
|
29 | if (operation === 'subscription') {
|
30 | resolvers[typeName][fieldName] = {
|
31 | subscribe: finalResolver,
|
32 | resolve: identical,
|
33 | };
|
34 | }
|
35 | else {
|
36 | resolvers[typeName][fieldName] = {
|
37 | resolve: finalResolver,
|
38 | };
|
39 | }
|
40 | }
|
41 | }
|
42 | return resolvers;
|
43 | }
|
44 | function identical(value) {
|
45 | return value;
|
46 | }
|
47 | function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) {
|
48 | return function possiblyNestedProxyingResolver(parent, args, context, info) {
|
49 | if (parent != null) {
|
50 | const responseKey = utils.getResponseKeyFromInfo(info);
|
51 |
|
52 | if (delegate.isExternalObject(parent)) {
|
53 | const unpathedErrors = delegate.getUnpathedErrors(parent);
|
54 | const subschema = delegate.getSubschema(parent, responseKey);
|
55 |
|
56 |
|
57 |
|
58 | if (subschemaConfig === subschema && parent[responseKey] !== undefined) {
|
59 | return delegate.resolveExternalValue(parent[responseKey], unpathedErrors, subschema, context, info);
|
60 | }
|
61 | }
|
62 | }
|
63 | return proxyingResolver(parent, args, context, info);
|
64 | };
|
65 | }
|
66 | function defaultCreateProxyingResolver({ subschemaConfig, operation, transformedSchema, }) {
|
67 | return function proxyingResolver(_parent, _args, context, info) {
|
68 | return delegate.delegateToSchema({
|
69 | schema: subschemaConfig,
|
70 | operation,
|
71 | context,
|
72 | info,
|
73 | transformedSchema,
|
74 | });
|
75 | };
|
76 | }
|
77 |
|
78 | function wrapSchema(subschemaConfig) {
|
79 | const targetSchema = subschemaConfig.schema;
|
80 | const proxyingResolvers = generateProxyingResolvers(subschemaConfig);
|
81 | const schema = createWrappingSchema(targetSchema, proxyingResolvers);
|
82 | const transformedSchema = delegate.applySchemaTransforms(schema, subschemaConfig);
|
83 | return delegate.applySchemaTransforms(schema, subschemaConfig, transformedSchema);
|
84 | }
|
85 | function createWrappingSchema(schema, proxyingResolvers) {
|
86 | return utils.mapSchema(schema, {
|
87 | [utils.MapperKind.ROOT_OBJECT]: type => {
|
88 | var _a;
|
89 | const config = type.toConfig();
|
90 | const fieldConfigMap = config.fields;
|
91 | for (const fieldName in fieldConfigMap) {
|
92 | const field = fieldConfigMap[fieldName];
|
93 | if (field == null) {
|
94 | continue;
|
95 | }
|
96 | fieldConfigMap[fieldName] = {
|
97 | ...field,
|
98 | ...(_a = proxyingResolvers[type.name]) === null || _a === void 0 ? void 0 : _a[fieldName],
|
99 | };
|
100 | }
|
101 | return new graphql.GraphQLObjectType(config);
|
102 | },
|
103 | [utils.MapperKind.OBJECT_TYPE]: type => {
|
104 | const config = type.toConfig();
|
105 | config.isTypeOf = undefined;
|
106 | for (const fieldName in config.fields) {
|
107 | const field = config.fields[fieldName];
|
108 | if (field == null) {
|
109 | continue;
|
110 | }
|
111 | field.resolve = delegate.defaultMergedResolver;
|
112 | field.subscribe = undefined;
|
113 | }
|
114 | return new graphql.GraphQLObjectType(config);
|
115 | },
|
116 | [utils.MapperKind.INTERFACE_TYPE]: type => {
|
117 | const config = type.toConfig();
|
118 | delete config.resolveType;
|
119 | return new graphql.GraphQLInterfaceType(config);
|
120 | },
|
121 | [utils.MapperKind.UNION_TYPE]: type => {
|
122 | const config = type.toConfig();
|
123 | delete config.resolveType;
|
124 | return new graphql.GraphQLUnionType(config);
|
125 | },
|
126 | });
|
127 | }
|
128 |
|
129 | class RenameTypes {
|
130 | constructor(renamer, options) {
|
131 | this.renamer = renamer;
|
132 | this.map = Object.create(null);
|
133 | this.reverseMap = Object.create(null);
|
134 | const { renameBuiltins = false, renameScalars = true } = options != null ? options : {};
|
135 | this.renameBuiltins = renameBuiltins;
|
136 | this.renameScalars = renameScalars;
|
137 | }
|
138 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
139 | return utils.mapSchema(originalWrappingSchema, {
|
140 | [utils.MapperKind.TYPE]: (type) => {
|
141 | if (graphql.isSpecifiedScalarType(type) && !this.renameBuiltins) {
|
142 | return undefined;
|
143 | }
|
144 | if (graphql.isScalarType(type) && !this.renameScalars) {
|
145 | return undefined;
|
146 | }
|
147 | const oldName = type.name;
|
148 | const newName = this.renamer(oldName);
|
149 | if (newName !== undefined && newName !== oldName) {
|
150 | this.map[oldName] = newName;
|
151 | this.reverseMap[newName] = oldName;
|
152 | return utils.renameType(type, newName);
|
153 | }
|
154 | },
|
155 | [utils.MapperKind.ROOT_OBJECT]() {
|
156 | return undefined;
|
157 | },
|
158 | });
|
159 | }
|
160 | transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
161 | const document = graphql.visit(originalRequest.document, {
|
162 | [graphql.Kind.NAMED_TYPE]: (node) => {
|
163 | const name = node.name.value;
|
164 | if (name in this.reverseMap) {
|
165 | return {
|
166 | ...node,
|
167 | name: {
|
168 | kind: graphql.Kind.NAME,
|
169 | value: this.reverseMap[name],
|
170 | },
|
171 | };
|
172 | }
|
173 | },
|
174 | });
|
175 | return {
|
176 | ...originalRequest,
|
177 | document,
|
178 | };
|
179 | }
|
180 | transformResult(originalResult, _delegationContext, _transformationContext) {
|
181 | return {
|
182 | ...originalResult,
|
183 | data: utils.visitData(originalResult.data, object => {
|
184 | const typeName = object === null || object === void 0 ? void 0 : object.__typename;
|
185 | if (typeName != null && typeName in this.map) {
|
186 | object.__typename = this.map[typeName];
|
187 | }
|
188 | return object;
|
189 | }),
|
190 | };
|
191 | }
|
192 | }
|
193 |
|
194 | class FilterTypes {
|
195 | constructor(filter) {
|
196 | this.filter = filter;
|
197 | }
|
198 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
199 | return utils.mapSchema(originalWrappingSchema, {
|
200 | [utils.MapperKind.TYPE]: (type) => {
|
201 | if (this.filter(type)) {
|
202 | return undefined;
|
203 | }
|
204 | return null;
|
205 | },
|
206 | });
|
207 | }
|
208 | }
|
209 |
|
210 | class RenameRootTypes {
|
211 | constructor(renamer) {
|
212 | this.renamer = renamer;
|
213 | this.map = Object.create(null);
|
214 | this.reverseMap = Object.create(null);
|
215 | }
|
216 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
217 | return utils.mapSchema(originalWrappingSchema, {
|
218 | [utils.MapperKind.ROOT_OBJECT]: type => {
|
219 | const oldName = type.name;
|
220 | const newName = this.renamer(oldName);
|
221 | if (newName !== undefined && newName !== oldName) {
|
222 | this.map[oldName] = newName;
|
223 | this.reverseMap[newName] = oldName;
|
224 | return utils.renameType(type, newName);
|
225 | }
|
226 | },
|
227 | });
|
228 | }
|
229 | transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
230 | const document = graphql.visit(originalRequest.document, {
|
231 | [graphql.Kind.NAMED_TYPE]: (node) => {
|
232 | const name = node.name.value;
|
233 | if (name in this.reverseMap) {
|
234 | return {
|
235 | ...node,
|
236 | name: {
|
237 | kind: graphql.Kind.NAME,
|
238 | value: this.reverseMap[name],
|
239 | },
|
240 | };
|
241 | }
|
242 | },
|
243 | });
|
244 | return {
|
245 | ...originalRequest,
|
246 | document,
|
247 | };
|
248 | }
|
249 | transformResult(originalResult, _delegationContext, _transformationContext) {
|
250 | return {
|
251 | ...originalResult,
|
252 | data: utils.visitData(originalResult.data, object => {
|
253 | const typeName = object === null || object === void 0 ? void 0 : object.__typename;
|
254 | if (typeName != null && typeName in this.map) {
|
255 | object.__typename = this.map[typeName];
|
256 | }
|
257 | return object;
|
258 | }),
|
259 | };
|
260 | }
|
261 | }
|
262 |
|
263 | class TransformCompositeFields {
|
264 | constructor(fieldTransformer, fieldNodeTransformer, dataTransformer, errorsTransformer) {
|
265 | this.fieldTransformer = fieldTransformer;
|
266 | this.fieldNodeTransformer = fieldNodeTransformer;
|
267 | this.dataTransformer = dataTransformer;
|
268 | this.errorsTransformer = errorsTransformer;
|
269 | this.mapping = {};
|
270 | }
|
271 | _getTypeInfo() {
|
272 | const typeInfo = this.typeInfo;
|
273 | if (typeInfo === undefined) {
|
274 | throw new Error(`The TransformCompositeFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
275 | }
|
276 | return typeInfo;
|
277 | }
|
278 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
279 | var _a;
|
280 | this.transformedSchema = utils.mapSchema(originalWrappingSchema, {
|
281 | [utils.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
|
282 | const transformedField = this.fieldTransformer(typeName, fieldName, fieldConfig);
|
283 | if (Array.isArray(transformedField)) {
|
284 | const newFieldName = transformedField[0];
|
285 | if (newFieldName !== fieldName) {
|
286 | if (!(typeName in this.mapping)) {
|
287 | this.mapping[typeName] = {};
|
288 | }
|
289 | this.mapping[typeName][newFieldName] = fieldName;
|
290 | }
|
291 | }
|
292 | return transformedField;
|
293 | },
|
294 | });
|
295 | this.typeInfo = new graphql.TypeInfo(this.transformedSchema);
|
296 | this.subscriptionTypeName = (_a = originalWrappingSchema.getSubscriptionType()) === null || _a === void 0 ? void 0 : _a.name;
|
297 | return this.transformedSchema;
|
298 | }
|
299 | transformRequest(originalRequest, _delegationContext, transformationContext) {
|
300 | const document = originalRequest.document;
|
301 | return {
|
302 | ...originalRequest,
|
303 | document: this.transformDocument(document, transformationContext),
|
304 | };
|
305 | }
|
306 | transformResult(result, _delegationContext, transformationContext) {
|
307 | const dataTransformer = this.dataTransformer;
|
308 | if (dataTransformer != null) {
|
309 | result.data = utils.visitData(result.data, value => dataTransformer(value, transformationContext));
|
310 | }
|
311 | if (this.errorsTransformer != null && Array.isArray(result.errors)) {
|
312 | result.errors = this.errorsTransformer(result.errors, transformationContext);
|
313 | }
|
314 | return result;
|
315 | }
|
316 | transformDocument(document, transformationContext) {
|
317 | const fragments = Object.create(null);
|
318 | for (const def of document.definitions) {
|
319 | if (def.kind === graphql.Kind.FRAGMENT_DEFINITION) {
|
320 | fragments[def.name.value] = def;
|
321 | }
|
322 | }
|
323 | return graphql.visit(document, graphql.visitWithTypeInfo(this._getTypeInfo(), {
|
324 | leave: {
|
325 | [graphql.Kind.SELECTION_SET]: node => this.transformSelectionSet(node, this._getTypeInfo(), fragments, transformationContext),
|
326 | },
|
327 | }));
|
328 | }
|
329 | transformSelectionSet(node, typeInfo, fragments, transformationContext) {
|
330 | var _a, _b;
|
331 | const parentType = typeInfo.getParentType();
|
332 | if (parentType == null) {
|
333 | return undefined;
|
334 | }
|
335 | const parentTypeName = parentType.name;
|
336 | let newSelections = [];
|
337 | for (const selection of node.selections) {
|
338 | if (selection.kind !== graphql.Kind.FIELD) {
|
339 | newSelections.push(selection);
|
340 | continue;
|
341 | }
|
342 | const newName = selection.name.value;
|
343 |
|
344 | if ((this.dataTransformer != null || this.errorsTransformer != null) &&
|
345 | (this.subscriptionTypeName == null || parentTypeName !== this.subscriptionTypeName)) {
|
346 | newSelections.push({
|
347 | kind: graphql.Kind.FIELD,
|
348 | name: {
|
349 | kind: graphql.Kind.NAME,
|
350 | value: '__typename',
|
351 | },
|
352 | });
|
353 | }
|
354 | let transformedSelection;
|
355 | if (this.fieldNodeTransformer == null) {
|
356 | transformedSelection = selection;
|
357 | }
|
358 | else {
|
359 | transformedSelection = this.fieldNodeTransformer(parentTypeName, newName, selection, fragments, transformationContext);
|
360 | transformedSelection = transformedSelection === undefined ? selection : transformedSelection;
|
361 | }
|
362 | if (transformedSelection == null) {
|
363 | continue;
|
364 | }
|
365 | else if (Array.isArray(transformedSelection)) {
|
366 | newSelections = newSelections.concat(transformedSelection);
|
367 | continue;
|
368 | }
|
369 | else if (transformedSelection.kind !== graphql.Kind.FIELD) {
|
370 | newSelections.push(transformedSelection);
|
371 | continue;
|
372 | }
|
373 | const typeMapping = this.mapping[parentTypeName];
|
374 | if (typeMapping == null) {
|
375 | newSelections.push(transformedSelection);
|
376 | continue;
|
377 | }
|
378 | const oldName = this.mapping[parentTypeName][newName];
|
379 | if (oldName == null) {
|
380 | newSelections.push(transformedSelection);
|
381 | continue;
|
382 | }
|
383 | newSelections.push({
|
384 | ...transformedSelection,
|
385 | name: {
|
386 | kind: graphql.Kind.NAME,
|
387 | value: oldName,
|
388 | },
|
389 | alias: {
|
390 | kind: graphql.Kind.NAME,
|
391 | value: (_b = (_a = transformedSelection.alias) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : newName,
|
392 | },
|
393 | });
|
394 | }
|
395 | return {
|
396 | ...node,
|
397 | selections: newSelections,
|
398 | };
|
399 | }
|
400 | }
|
401 |
|
402 | class TransformObjectFields {
|
403 | constructor(objectFieldTransformer, fieldNodeTransformer) {
|
404 | this.objectFieldTransformer = objectFieldTransformer;
|
405 | this.fieldNodeTransformer = fieldNodeTransformer;
|
406 | }
|
407 | _getTransformer() {
|
408 | const transformer = this.transformer;
|
409 | if (transformer === undefined) {
|
410 | throw new Error(`The TransformObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
411 | }
|
412 | return transformer;
|
413 | }
|
414 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
415 | const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
416 | if (graphql.isObjectType(originalWrappingSchema.getType(typeName))) {
|
417 | return this.objectFieldTransformer(typeName, fieldName, fieldConfig);
|
418 | }
|
419 | return undefined;
|
420 | };
|
421 | this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
422 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
423 | }
|
424 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
425 | return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
426 | }
|
427 | transformResult(originalResult, delegationContext, transformationContext) {
|
428 | return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
429 | }
|
430 | }
|
431 |
|
432 | class TransformRootFields {
|
433 | constructor(rootFieldTransformer, fieldNodeTransformer) {
|
434 | this.rootFieldTransformer = rootFieldTransformer;
|
435 | this.fieldNodeTransformer = fieldNodeTransformer;
|
436 | }
|
437 | _getTransformer() {
|
438 | const transformer = this.transformer;
|
439 | if (transformer === undefined) {
|
440 | throw new Error(`The TransformRootFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
441 | }
|
442 | return transformer;
|
443 | }
|
444 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
445 | const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
446 | var _a, _b, _c;
|
447 | if (typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
|
448 | return this.rootFieldTransformer('Query', fieldName, fieldConfig);
|
449 | }
|
450 | if (typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
|
451 | return this.rootFieldTransformer('Mutation', fieldName, fieldConfig);
|
452 | }
|
453 | if (typeName === ((_c = originalWrappingSchema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
|
454 | return this.rootFieldTransformer('Subscription', fieldName, fieldConfig);
|
455 | }
|
456 | return undefined;
|
457 | };
|
458 | this.transformer = new TransformObjectFields(rootToObjectFieldTransformer, this.fieldNodeTransformer);
|
459 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
460 | }
|
461 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
462 | return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
463 | }
|
464 | transformResult(originalResult, delegationContext, transformationContext) {
|
465 | return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
466 | }
|
467 | }
|
468 |
|
469 | class RenameRootFields {
|
470 | constructor(renamer) {
|
471 | this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => [renamer(operation, fieldName, fieldConfig), fieldConfig]);
|
472 | }
|
473 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
474 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
475 | }
|
476 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
477 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
478 | }
|
479 | }
|
480 |
|
481 | class FilterRootFields {
|
482 | constructor(filter) {
|
483 | this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => {
|
484 | if (filter(operation, fieldName, fieldConfig)) {
|
485 | return undefined;
|
486 | }
|
487 | return null;
|
488 | });
|
489 | }
|
490 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
491 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
492 | }
|
493 | }
|
494 |
|
495 | class RenameObjectFields {
|
496 | constructor(renamer) {
|
497 | this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => [
|
498 | renamer(typeName, fieldName, fieldConfig),
|
499 | fieldConfig,
|
500 | ]);
|
501 | }
|
502 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
503 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
504 | }
|
505 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
506 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
507 | }
|
508 | }
|
509 |
|
510 | class FilterObjectFields {
|
511 | constructor(filter) {
|
512 | this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
|
513 | }
|
514 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
515 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
516 | }
|
517 | }
|
518 |
|
519 | class TransformInterfaceFields {
|
520 | constructor(interfaceFieldTransformer, fieldNodeTransformer) {
|
521 | this.interfaceFieldTransformer = interfaceFieldTransformer;
|
522 | this.fieldNodeTransformer = fieldNodeTransformer;
|
523 | }
|
524 | _getTransformer() {
|
525 | const transformer = this.transformer;
|
526 | if (transformer === undefined) {
|
527 | throw new Error(`The TransformInterfaceFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
528 | }
|
529 | return transformer;
|
530 | }
|
531 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
532 | const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
533 | if (graphql.isInterfaceType(originalWrappingSchema.getType(typeName))) {
|
534 | return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig);
|
535 | }
|
536 | return undefined;
|
537 | };
|
538 | this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
539 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
540 | }
|
541 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
542 | return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
543 | }
|
544 | transformResult(originalResult, delegationContext, transformationContext) {
|
545 | return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
546 | }
|
547 | }
|
548 |
|
549 | class RenameInterfaceFields {
|
550 | constructor(renamer) {
|
551 | this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => [
|
552 | renamer(typeName, fieldName, fieldConfig),
|
553 | fieldConfig,
|
554 | ]);
|
555 | }
|
556 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
557 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
558 | }
|
559 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
560 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
561 | }
|
562 | }
|
563 |
|
564 | class FilterInterfaceFields {
|
565 | constructor(filter) {
|
566 | this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
|
567 | }
|
568 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
569 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
570 | }
|
571 | }
|
572 |
|
573 | class TransformInputObjectFields {
|
574 | constructor(inputFieldTransformer, inputFieldNodeTransformer, inputObjectNodeTransformer) {
|
575 | this.inputFieldTransformer = inputFieldTransformer;
|
576 | this.inputFieldNodeTransformer = inputFieldNodeTransformer;
|
577 | this.inputObjectNodeTransformer = inputObjectNodeTransformer;
|
578 | this.mapping = {};
|
579 | }
|
580 | _getTransformedSchema() {
|
581 | const transformedSchema = this.transformedSchema;
|
582 | if (transformedSchema === undefined) {
|
583 | throw new Error(`The TransformInputObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
584 | }
|
585 | return transformedSchema;
|
586 | }
|
587 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
588 | this.transformedSchema = utils.mapSchema(originalWrappingSchema, {
|
589 | [utils.MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
|
590 | const transformedInputField = this.inputFieldTransformer(typeName, fieldName, inputFieldConfig);
|
591 | if (Array.isArray(transformedInputField)) {
|
592 | const newFieldName = transformedInputField[0];
|
593 | if (newFieldName !== fieldName) {
|
594 | if (!(typeName in this.mapping)) {
|
595 | this.mapping[typeName] = {};
|
596 | }
|
597 | this.mapping[typeName][newFieldName] = fieldName;
|
598 | }
|
599 | }
|
600 | return transformedInputField;
|
601 | },
|
602 | });
|
603 | return this.transformedSchema;
|
604 | }
|
605 | transformRequest(originalRequest, delegationContext, _transformationContext) {
|
606 | var _a;
|
607 | const variableValues = (_a = originalRequest.variables) !== null && _a !== void 0 ? _a : {};
|
608 | const fragments = Object.create(null);
|
609 | const operations = [];
|
610 | for (const def of originalRequest.document.definitions) {
|
611 | if (def.kind === graphql.Kind.OPERATION_DEFINITION) {
|
612 | operations.push(def);
|
613 | }
|
614 | else if (def.kind === graphql.Kind.FRAGMENT_DEFINITION) {
|
615 | fragments[def.name.value] = def;
|
616 | }
|
617 | }
|
618 | for (const def of operations) {
|
619 | const variableDefs = def.variableDefinitions;
|
620 | if (variableDefs != null) {
|
621 | for (const variableDef of variableDefs) {
|
622 | const varName = variableDef.variable.name.value;
|
623 | if (variableDef.type.kind !== graphql.Kind.NAMED_TYPE) {
|
624 | throw new Error(`Expected ${variableDef.type} to be a named type`);
|
625 | }
|
626 |
|
627 | const varType = graphql.typeFromAST(delegationContext.transformedSchema, variableDef.type);
|
628 | if (!graphql.isInputType(varType)) {
|
629 | throw new Error(`Expected ${varType} to be an input type`);
|
630 | }
|
631 | variableValues[varName] = utils.transformInputValue(varType, variableValues[varName], undefined, (type, originalValue) => {
|
632 | var _a;
|
633 | const newValue = Object.create(null);
|
634 | const fields = type.getFields();
|
635 | for (const key in originalValue) {
|
636 | const field = fields[key];
|
637 | if (field != null) {
|
638 | const newFieldName = (_a = this.mapping[type.name]) === null || _a === void 0 ? void 0 : _a[field.name];
|
639 | if (newFieldName != null) {
|
640 | newValue[newFieldName] = originalValue[field.name];
|
641 | }
|
642 | else {
|
643 | newValue[field.name] = originalValue[field.name];
|
644 | }
|
645 | }
|
646 | }
|
647 | return newValue;
|
648 | });
|
649 | }
|
650 | }
|
651 | }
|
652 | for (const def of originalRequest.document.definitions.filter(def => def.kind === graphql.Kind.FRAGMENT_DEFINITION)) {
|
653 | fragments[def.name.value] = def;
|
654 | }
|
655 | const document = this.transformDocument(originalRequest.document, this.mapping, this.inputFieldNodeTransformer, this.inputObjectNodeTransformer, originalRequest, delegationContext);
|
656 | return {
|
657 | ...originalRequest,
|
658 | document,
|
659 | variables: variableValues,
|
660 | };
|
661 | }
|
662 | transformDocument(document, mapping, inputFieldNodeTransformer, inputObjectNodeTransformer, request, delegationContext) {
|
663 | const typeInfo = new graphql.TypeInfo(this._getTransformedSchema());
|
664 | const newDocument = graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, {
|
665 | leave: {
|
666 | [graphql.Kind.OBJECT]: (node) => {
|
667 |
|
668 | const parentType = typeInfo.getInputType();
|
669 | if (parentType != null) {
|
670 | const parentTypeName = parentType.name;
|
671 | const newInputFields = [];
|
672 | for (const inputField of node.fields) {
|
673 | const newName = inputField.name.value;
|
674 | const transformedInputField = inputFieldNodeTransformer != null
|
675 | ? inputFieldNodeTransformer(parentTypeName, newName, inputField, request, delegationContext)
|
676 | : inputField;
|
677 | if (Array.isArray(transformedInputField)) {
|
678 | for (const individualTransformedInputField of transformedInputField) {
|
679 | const typeMapping = mapping[parentTypeName];
|
680 | if (typeMapping == null) {
|
681 | newInputFields.push(individualTransformedInputField);
|
682 | continue;
|
683 | }
|
684 | const oldName = typeMapping[newName];
|
685 | if (oldName == null) {
|
686 | newInputFields.push(individualTransformedInputField);
|
687 | continue;
|
688 | }
|
689 | newInputFields.push({
|
690 | ...individualTransformedInputField,
|
691 | name: {
|
692 | ...individualTransformedInputField.name,
|
693 | value: oldName,
|
694 | },
|
695 | });
|
696 | }
|
697 | continue;
|
698 | }
|
699 | const typeMapping = mapping[parentTypeName];
|
700 | if (typeMapping == null) {
|
701 | newInputFields.push(transformedInputField);
|
702 | continue;
|
703 | }
|
704 | const oldName = typeMapping[newName];
|
705 | if (oldName == null) {
|
706 | newInputFields.push(transformedInputField);
|
707 | continue;
|
708 | }
|
709 | newInputFields.push({
|
710 | ...transformedInputField,
|
711 | name: {
|
712 | ...transformedInputField.name,
|
713 | value: oldName,
|
714 | },
|
715 | });
|
716 | }
|
717 | const newNode = {
|
718 | ...node,
|
719 | fields: newInputFields,
|
720 | };
|
721 | return inputObjectNodeTransformer != null
|
722 | ? inputObjectNodeTransformer(parentTypeName, newNode, request, delegationContext)
|
723 | : newNode;
|
724 | }
|
725 | },
|
726 | },
|
727 | }));
|
728 | return newDocument;
|
729 | }
|
730 | }
|
731 |
|
732 | class RenameInputObjectFields {
|
733 | constructor(renamer) {
|
734 | this.renamer = renamer;
|
735 | this.transformer = new TransformInputObjectFields((typeName, inputFieldName, inputFieldConfig) => {
|
736 | const newName = renamer(typeName, inputFieldName, inputFieldConfig);
|
737 | if (newName !== undefined && newName !== inputFieldName) {
|
738 | const value = renamer(typeName, inputFieldName, inputFieldConfig);
|
739 | if (value != null) {
|
740 | return [value, inputFieldConfig];
|
741 | }
|
742 | }
|
743 | }, (typeName, inputFieldName, inputFieldNode) => {
|
744 | if (!(typeName in this.reverseMap)) {
|
745 | return inputFieldNode;
|
746 | }
|
747 | const inputFieldNameMap = this.reverseMap[typeName];
|
748 | if (!(inputFieldName in inputFieldNameMap)) {
|
749 | return inputFieldNode;
|
750 | }
|
751 | return {
|
752 | ...inputFieldNode,
|
753 | name: {
|
754 | ...inputFieldNode.name,
|
755 | value: inputFieldNameMap[inputFieldName],
|
756 | },
|
757 | };
|
758 | });
|
759 | this.reverseMap = Object.create(null);
|
760 | }
|
761 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
762 | utils.mapSchema(originalWrappingSchema, {
|
763 | [utils.MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
|
764 | const newName = this.renamer(typeName, fieldName, inputFieldConfig);
|
765 | if (newName !== undefined && newName !== fieldName) {
|
766 | if (this.reverseMap[typeName] == null) {
|
767 | this.reverseMap[typeName] = Object.create(null);
|
768 | }
|
769 | this.reverseMap[typeName][newName] = fieldName;
|
770 | }
|
771 | return undefined;
|
772 | },
|
773 | [utils.MapperKind.ROOT_OBJECT]() {
|
774 | return undefined;
|
775 | },
|
776 | });
|
777 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
778 | }
|
779 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
780 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
781 | }
|
782 | }
|
783 |
|
784 | class FilterInputObjectFields {
|
785 | constructor(filter, inputObjectNodeTransformer) {
|
786 | this.transformer = new TransformInputObjectFields((typeName, fieldName, inputFieldConfig) => filter(typeName, fieldName, inputFieldConfig) ? undefined : null, undefined, inputObjectNodeTransformer);
|
787 | }
|
788 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
789 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
790 | }
|
791 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
792 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
793 | }
|
794 | }
|
795 |
|
796 | class MapLeafValues {
|
797 | constructor(inputValueTransformer, outputValueTransformer) {
|
798 | this.inputValueTransformer = inputValueTransformer;
|
799 | this.outputValueTransformer = outputValueTransformer;
|
800 | this.resultVisitorMap = Object.create(null);
|
801 | }
|
802 | _getTypeInfo() {
|
803 | const typeInfo = this.typeInfo;
|
804 | if (typeInfo === undefined) {
|
805 | throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
806 | }
|
807 | return typeInfo;
|
808 | }
|
809 | _getOriginalWrappingSchema() {
|
810 | const originalWrappingSchema = this.originalWrappingSchema;
|
811 | if (originalWrappingSchema === undefined) {
|
812 | throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
813 | }
|
814 | return originalWrappingSchema;
|
815 | }
|
816 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
817 | this.originalWrappingSchema = originalWrappingSchema;
|
818 | const typeMap = originalWrappingSchema.getTypeMap();
|
819 | for (const typeName in typeMap) {
|
820 | const type = typeMap[typeName];
|
821 | if (!typeName.startsWith('__')) {
|
822 | if (graphql.isLeafType(type)) {
|
823 | this.resultVisitorMap[typeName] = (value) => this.outputValueTransformer(typeName, value);
|
824 | }
|
825 | }
|
826 | }
|
827 | this.typeInfo = new graphql.TypeInfo(originalWrappingSchema);
|
828 | return originalWrappingSchema;
|
829 | }
|
830 | transformRequest(originalRequest, _delegationContext, transformationContext) {
|
831 | const document = originalRequest.document;
|
832 | const variableValues = (originalRequest.variables = {});
|
833 | const operations = document.definitions.filter(def => def.kind === graphql.Kind.OPERATION_DEFINITION);
|
834 | const fragments = document.definitions.filter(def => def.kind === graphql.Kind.FRAGMENT_DEFINITION);
|
835 | const newOperations = this.transformOperations(operations, variableValues);
|
836 | const transformedRequest = {
|
837 | ...originalRequest,
|
838 | document: {
|
839 | ...document,
|
840 | definitions: [...newOperations, ...fragments],
|
841 | },
|
842 | variables: variableValues,
|
843 | };
|
844 | transformationContext.transformedRequest = transformedRequest;
|
845 | return transformedRequest;
|
846 | }
|
847 | transformResult(originalResult, _delegationContext, transformationContext) {
|
848 | return utils.visitResult(originalResult, transformationContext.transformedRequest, this._getOriginalWrappingSchema(), this.resultVisitorMap);
|
849 | }
|
850 | transformOperations(operations, variableValues) {
|
851 | return operations.map((operation) => {
|
852 | var _a;
|
853 | const variableDefinitionMap = ((_a = operation.variableDefinitions) !== null && _a !== void 0 ? _a : []).reduce((prev, def) => ({
|
854 | ...prev,
|
855 | [def.variable.name.value]: def,
|
856 | }), {});
|
857 | const newOperation = graphql.visit(operation, graphql.visitWithTypeInfo(this._getTypeInfo(), {
|
858 | [graphql.Kind.FIELD]: node => this.transformFieldNode(node, variableDefinitionMap, variableValues),
|
859 | }));
|
860 | return {
|
861 | ...newOperation,
|
862 | variableDefinitions: Object.values(variableDefinitionMap),
|
863 | };
|
864 | });
|
865 | }
|
866 | transformFieldNode(field, variableDefinitionMap, variableValues) {
|
867 | const targetField = this._getTypeInfo().getFieldDef();
|
868 | const generateVariableName = utils.createVariableNameGenerator(variableDefinitionMap);
|
869 | if (!targetField.name.startsWith('__')) {
|
870 | const argumentNodes = field.arguments;
|
871 | if (argumentNodes != null) {
|
872 | const argumentNodeMap = argumentNodes.reduce((prev, argument) => ({
|
873 | ...prev,
|
874 | [argument.name.value]: argument,
|
875 | }), Object.create(null));
|
876 | for (const argument of targetField.args) {
|
877 | const argName = argument.name;
|
878 | const argType = argument.type;
|
879 | const argumentNode = argumentNodeMap[argName];
|
880 | const argValue = argumentNode === null || argumentNode === void 0 ? void 0 : argumentNode.value;
|
881 | let value;
|
882 | if (argValue != null) {
|
883 | value = graphql.valueFromAST(argValue, argType, variableValues);
|
884 | }
|
885 | utils.updateArgument(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), argType, utils.transformInputValue(argType, value, (t, v) => {
|
886 | const newValue = this.inputValueTransformer(t.name, v);
|
887 | return newValue === undefined ? v : newValue;
|
888 | }));
|
889 | }
|
890 | return {
|
891 | ...field,
|
892 | arguments: Object.values(argumentNodeMap),
|
893 | };
|
894 | }
|
895 | }
|
896 | }
|
897 | }
|
898 |
|
899 | class TransformEnumValues {
|
900 | constructor(enumValueTransformer, inputValueTransformer, outputValueTransformer) {
|
901 | this.enumValueTransformer = enumValueTransformer;
|
902 | this.mapping = Object.create(null);
|
903 | this.reverseMapping = Object.create(null);
|
904 | this.transformer = new MapLeafValues(generateValueTransformer(inputValueTransformer, this.reverseMapping), generateValueTransformer(outputValueTransformer, this.mapping));
|
905 | }
|
906 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
907 | const mappingSchema = this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
908 | this.transformedSchema = utils.mapSchema(mappingSchema, {
|
909 | [utils.MapperKind.ENUM_VALUE]: (valueConfig, typeName, _schema, externalValue) => this.transformEnumValue(typeName, externalValue, valueConfig),
|
910 | });
|
911 | return this.transformedSchema;
|
912 | }
|
913 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
914 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
915 | }
|
916 | transformResult(originalResult, delegationContext, transformationContext) {
|
917 | return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
918 | }
|
919 | transformEnumValue(typeName, externalValue, enumValueConfig) {
|
920 | const transformedEnumValue = this.enumValueTransformer(typeName, externalValue, enumValueConfig);
|
921 | if (Array.isArray(transformedEnumValue)) {
|
922 | const newExternalValue = transformedEnumValue[0];
|
923 | if (newExternalValue !== externalValue) {
|
924 | if (!(typeName in this.mapping)) {
|
925 | this.mapping[typeName] = Object.create(null);
|
926 | this.reverseMapping[typeName] = Object.create(null);
|
927 | }
|
928 | this.mapping[typeName][externalValue] = newExternalValue;
|
929 | this.reverseMapping[typeName][newExternalValue] = externalValue;
|
930 | }
|
931 | }
|
932 | return transformedEnumValue;
|
933 | }
|
934 | }
|
935 | function mapEnumValues(typeName, value, mapping) {
|
936 | var _a;
|
937 | const newExternalValue = (_a = mapping[typeName]) === null || _a === void 0 ? void 0 : _a[value];
|
938 | return newExternalValue != null ? newExternalValue : value;
|
939 | }
|
940 | function generateValueTransformer(valueTransformer, mapping) {
|
941 | if (valueTransformer == null) {
|
942 | return (typeName, value) => mapEnumValues(typeName, value, mapping);
|
943 | }
|
944 | else {
|
945 | return (typeName, value) => mapEnumValues(typeName, valueTransformer(typeName, value), mapping);
|
946 | }
|
947 | }
|
948 |
|
949 | class TransformQuery {
|
950 | constructor({ path, queryTransformer, resultTransformer = result => result, errorPathTransformer = errorPath => [...errorPath], fragments = {}, }) {
|
951 | this.path = path;
|
952 | this.queryTransformer = queryTransformer;
|
953 | this.resultTransformer = resultTransformer;
|
954 | this.errorPathTransformer = errorPathTransformer;
|
955 | this.fragments = fragments;
|
956 | }
|
957 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
958 | const pathLength = this.path.length;
|
959 | let index = 0;
|
960 | const document = graphql.visit(originalRequest.document, {
|
961 | [graphql.Kind.FIELD]: {
|
962 | enter: node => {
|
963 | if (index === pathLength || node.name.value !== this.path[index] || node.selectionSet == null) {
|
964 | return false;
|
965 | }
|
966 | index++;
|
967 | if (index === pathLength) {
|
968 | const selectionSet = this.queryTransformer(node.selectionSet, this.fragments, delegationContext, transformationContext);
|
969 | return {
|
970 | ...node,
|
971 | selectionSet,
|
972 | };
|
973 | }
|
974 | },
|
975 | leave: () => {
|
976 | index--;
|
977 | },
|
978 | },
|
979 | });
|
980 | return {
|
981 | ...originalRequest,
|
982 | document,
|
983 | };
|
984 | }
|
985 | transformResult(originalResult, delegationContext, transformationContext) {
|
986 | const data = this.transformData(originalResult.data, delegationContext, transformationContext);
|
987 | const errors = originalResult.errors;
|
988 | return {
|
989 | data,
|
990 | errors: errors != null ? this.transformErrors(errors) : undefined,
|
991 | };
|
992 | }
|
993 | transformData(data, delegationContext, transformationContext) {
|
994 | const leafIndex = this.path.length - 1;
|
995 | let index = 0;
|
996 | let newData = data;
|
997 | if (newData) {
|
998 | let next = this.path[index];
|
999 | while (index < leafIndex) {
|
1000 | if (data[next]) {
|
1001 | newData = newData[next];
|
1002 | }
|
1003 | else {
|
1004 | break;
|
1005 | }
|
1006 | index++;
|
1007 | next = this.path[index];
|
1008 | }
|
1009 | newData[next] = this.resultTransformer(newData[next], delegationContext, transformationContext);
|
1010 | }
|
1011 | return newData;
|
1012 | }
|
1013 | transformErrors(errors) {
|
1014 | return errors.map(error => {
|
1015 | const path = error.path;
|
1016 | if (path == null) {
|
1017 | return error;
|
1018 | }
|
1019 | let match = true;
|
1020 | let index = 0;
|
1021 | while (index < this.path.length) {
|
1022 | if (path[index] !== this.path[index]) {
|
1023 | match = false;
|
1024 | break;
|
1025 | }
|
1026 | index++;
|
1027 | }
|
1028 | const newPath = match ? path.slice(0, index).concat(this.errorPathTransformer(path.slice(index))) : path;
|
1029 | return utils.relocatedError(error, newPath);
|
1030 | });
|
1031 | }
|
1032 | }
|
1033 |
|
1034 | class FilterObjectFieldDirectives {
|
1035 | constructor(filter) {
|
1036 | this.filter = filter;
|
1037 | }
|
1038 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1039 | const transformer = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => {
|
1040 | var _a, _b, _c, _d;
|
1041 | const keepDirectives = (_c = (_b = (_a = fieldConfig.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.filter(dir => {
|
1042 | const directiveDef = originalWrappingSchema.getDirective(dir.name.value);
|
1043 | const directiveValue = directiveDef ? utils.getArgumentValues(directiveDef, dir) : undefined;
|
1044 | return this.filter(dir.name.value, directiveValue);
|
1045 | })) !== null && _c !== void 0 ? _c : [];
|
1046 | if (((_d = fieldConfig.astNode) === null || _d === void 0 ? void 0 : _d.directives) != null &&
|
1047 | keepDirectives.length !== fieldConfig.astNode.directives.length) {
|
1048 | fieldConfig = {
|
1049 | ...fieldConfig,
|
1050 | astNode: {
|
1051 | ...fieldConfig.astNode,
|
1052 | directives: keepDirectives,
|
1053 | },
|
1054 | };
|
1055 | return fieldConfig;
|
1056 | }
|
1057 | });
|
1058 | return transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1059 | }
|
1060 | }
|
1061 |
|
1062 | class RemoveObjectFieldDirectives {
|
1063 | constructor(directiveName, args = {}) {
|
1064 | this.transformer = new FilterObjectFieldDirectives((dirName, dirValue) => {
|
1065 | return !(utils.valueMatchesCriteria(dirName, directiveName) && utils.valueMatchesCriteria(dirValue, args));
|
1066 | });
|
1067 | }
|
1068 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1069 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1070 | }
|
1071 | }
|
1072 |
|
1073 | class RemoveObjectFieldsWithDirective {
|
1074 | constructor(directiveName, args = {}) {
|
1075 | this.directiveName = directiveName;
|
1076 | this.args = args;
|
1077 | }
|
1078 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1079 | const transformer = new FilterObjectFields((_typeName, _fieldName, fieldConfig) => {
|
1080 | const directives = utils.getDirectives(originalWrappingSchema, fieldConfig);
|
1081 | return !directives.some(directive => utils.valueMatchesCriteria(directive.name, this.directiveName) && utils.valueMatchesCriteria(directive.args, this.args));
|
1082 | });
|
1083 | return transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1084 | }
|
1085 | }
|
1086 |
|
1087 | class RemoveObjectFieldDeprecations {
|
1088 | constructor(reason) {
|
1089 | const args = { reason };
|
1090 | this.removeDirectives = new FilterObjectFieldDirectives((dirName, dirValue) => {
|
1091 | return !(dirName === 'deprecated' && utils.valueMatchesCriteria(dirValue, args));
|
1092 | });
|
1093 | this.removeDeprecations = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => {
|
1094 | if (fieldConfig.deprecationReason && utils.valueMatchesCriteria(fieldConfig.deprecationReason, reason)) {
|
1095 | fieldConfig = { ...fieldConfig };
|
1096 | delete fieldConfig.deprecationReason;
|
1097 | }
|
1098 | return fieldConfig;
|
1099 | });
|
1100 | }
|
1101 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1102 | return this.removeDeprecations.transformSchema(this.removeDirectives.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema), subschemaConfig, transformedSchema);
|
1103 | }
|
1104 | }
|
1105 |
|
1106 | class RemoveObjectFieldsWithDeprecation {
|
1107 | constructor(reason) {
|
1108 | this.transformer = new FilterObjectFields((_typeName, _fieldName, fieldConfig) => {
|
1109 | if (fieldConfig.deprecationReason) {
|
1110 | return !utils.valueMatchesCriteria(fieldConfig.deprecationReason, reason);
|
1111 | }
|
1112 | return true;
|
1113 | });
|
1114 | }
|
1115 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1116 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1117 | }
|
1118 | }
|
1119 |
|
1120 | class PruneTypes {
|
1121 | constructor(options = {}) {
|
1122 | this.options = options;
|
1123 | }
|
1124 | transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
1125 | return utils.pruneSchema(originalWrappingSchema, this.options);
|
1126 | }
|
1127 | }
|
1128 |
|
1129 | class MapFields {
|
1130 | constructor(fieldNodeTransformerMap, objectValueTransformerMap, errorsTransformer) {
|
1131 | this.fieldNodeTransformerMap = fieldNodeTransformerMap;
|
1132 | this.objectValueTransformerMap = objectValueTransformerMap;
|
1133 | this.errorsTransformer = errorsTransformer;
|
1134 | }
|
1135 | _getTransformer() {
|
1136 | const transformer = this.transformer;
|
1137 | if (transformer === undefined) {
|
1138 | throw new Error(`The MapFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
1139 | }
|
1140 | return transformer;
|
1141 | }
|
1142 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1143 | var _a;
|
1144 | const subscriptionTypeName = (_a = originalWrappingSchema.getSubscriptionType()) === null || _a === void 0 ? void 0 : _a.name;
|
1145 | const objectValueTransformerMap = this.objectValueTransformerMap;
|
1146 | this.transformer = new TransformCompositeFields(() => undefined, (typeName, fieldName, fieldNode, fragments, transformationContext) => {
|
1147 | const typeTransformers = this.fieldNodeTransformerMap[typeName];
|
1148 | if (typeTransformers == null) {
|
1149 | return undefined;
|
1150 | }
|
1151 | const fieldNodeTransformer = typeTransformers[fieldName];
|
1152 | if (fieldNodeTransformer == null) {
|
1153 | return undefined;
|
1154 | }
|
1155 | return fieldNodeTransformer(fieldNode, fragments, transformationContext);
|
1156 | }, objectValueTransformerMap != null
|
1157 | ? (data, transformationContext) => {
|
1158 | if (data == null) {
|
1159 | return data;
|
1160 | }
|
1161 | let typeName = data.__typename;
|
1162 | if (typeName == null) {
|
1163 |
|
1164 | typeName = subscriptionTypeName;
|
1165 | if (typeName == null) {
|
1166 | return data;
|
1167 | }
|
1168 | }
|
1169 | const transformer = objectValueTransformerMap[typeName];
|
1170 | if (transformer == null) {
|
1171 | return data;
|
1172 | }
|
1173 | return transformer(data, transformationContext);
|
1174 | }
|
1175 | : undefined, this.errorsTransformer != null ? this.errorsTransformer : undefined);
|
1176 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1177 | }
|
1178 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
1179 | return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
1180 | }
|
1181 | transformResult(originalResult, delegationContext, transformationContext) {
|
1182 | return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
1183 | }
|
1184 | }
|
1185 |
|
1186 | class WrapFields {
|
1187 | constructor(outerTypeName, wrappingFieldNames, wrappingTypeNames, fieldNames, prefix = 'gqtld') {
|
1188 | this.outerTypeName = outerTypeName;
|
1189 | this.wrappingFieldNames = wrappingFieldNames;
|
1190 | this.wrappingTypeNames = wrappingTypeNames;
|
1191 | this.numWraps = wrappingFieldNames.length;
|
1192 | this.fieldNames = fieldNames;
|
1193 | const remainingWrappingFieldNames = this.wrappingFieldNames.slice();
|
1194 | const outerMostWrappingFieldName = remainingWrappingFieldNames.shift();
|
1195 | if (outerMostWrappingFieldName == null) {
|
1196 | throw new Error(`Cannot wrap fields, no wrapping field name provided.`);
|
1197 | }
|
1198 | this.transformer = new MapFields({
|
1199 | [outerTypeName]: {
|
1200 | [outerMostWrappingFieldName]: (fieldNode, fragments, transformationContext) => hoistFieldNodes({
|
1201 | fieldNode,
|
1202 | path: remainingWrappingFieldNames,
|
1203 | fieldNames,
|
1204 | fragments,
|
1205 | transformationContext: transformationContext,
|
1206 | prefix,
|
1207 | }),
|
1208 | },
|
1209 | }, {
|
1210 | [outerTypeName]: (value, context) => dehoistValue(value, context),
|
1211 | }, (errors, context) => dehoistErrors(errors, context));
|
1212 | }
|
1213 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1214 | var _a, _b, _c, _d;
|
1215 | const fieldNames = this.fieldNames;
|
1216 | const targetFieldConfigMap = utils.selectObjectFields(originalWrappingSchema, this.outerTypeName, !fieldNames ? () => true : fieldName => fieldNames.includes(fieldName));
|
1217 | const newTargetFieldConfigMap = Object.create(null);
|
1218 | for (const fieldName in targetFieldConfigMap) {
|
1219 | const field = targetFieldConfigMap[fieldName];
|
1220 | const newField = {
|
1221 | ...field,
|
1222 | resolve: delegate.defaultMergedResolver,
|
1223 | };
|
1224 | newTargetFieldConfigMap[fieldName] = newField;
|
1225 | }
|
1226 | let wrapIndex = this.numWraps - 1;
|
1227 | let wrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
1228 | let wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
1229 | let newSchema = utils.appendObjectFields(originalWrappingSchema, wrappingTypeName, newTargetFieldConfigMap);
|
1230 | for (wrapIndex--; wrapIndex > -1; wrapIndex--) {
|
1231 | const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
1232 | newSchema = utils.appendObjectFields(newSchema, nextWrappingTypeName, {
|
1233 | [wrappingFieldName]: {
|
1234 | type: newSchema.getType(wrappingTypeName),
|
1235 | resolve: delegate.defaultMergedResolver,
|
1236 | },
|
1237 | });
|
1238 | wrappingTypeName = nextWrappingTypeName;
|
1239 | wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
1240 | }
|
1241 | const wrappingRootField = this.outerTypeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
|
1242 | this.outerTypeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
|
1243 | let resolve;
|
1244 | if (transformedSchema) {
|
1245 | if (wrappingRootField) {
|
1246 | const targetSchema = subschemaConfig.schema;
|
1247 | const operation = this.outerTypeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
|
1248 | const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : defaultCreateProxyingResolver;
|
1249 | resolve = createProxyingResolver({
|
1250 | subschemaConfig,
|
1251 | transformedSchema,
|
1252 | operation,
|
1253 | fieldName: wrappingFieldName,
|
1254 | });
|
1255 | }
|
1256 | else {
|
1257 | resolve = delegate.defaultMergedResolver;
|
1258 | }
|
1259 | }
|
1260 | [newSchema] = utils.modifyObjectFields(newSchema, this.outerTypeName, fieldName => !!newTargetFieldConfigMap[fieldName], {
|
1261 | [wrappingFieldName]: {
|
1262 | type: newSchema.getType(wrappingTypeName),
|
1263 | resolve,
|
1264 | },
|
1265 | });
|
1266 | return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
|
1267 | }
|
1268 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
1269 | transformationContext.nextIndex = 0;
|
1270 | transformationContext.paths = Object.create(null);
|
1271 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
1272 | }
|
1273 | transformResult(originalResult, delegationContext, transformationContext) {
|
1274 | return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
1275 | }
|
1276 | }
|
1277 | function collectFields(selectionSet, fragments, fields = [], visitedFragmentNames = {}) {
|
1278 | if (selectionSet != null) {
|
1279 | for (const selection of selectionSet.selections) {
|
1280 | switch (selection.kind) {
|
1281 | case graphql.Kind.FIELD:
|
1282 | fields.push(selection);
|
1283 | break;
|
1284 | case graphql.Kind.INLINE_FRAGMENT:
|
1285 | collectFields(selection.selectionSet, fragments, fields, visitedFragmentNames);
|
1286 | break;
|
1287 | case graphql.Kind.FRAGMENT_SPREAD: {
|
1288 | const fragmentName = selection.name.value;
|
1289 | if (!visitedFragmentNames[fragmentName]) {
|
1290 | visitedFragmentNames[fragmentName] = true;
|
1291 | collectFields(fragments[fragmentName].selectionSet, fragments, fields, visitedFragmentNames);
|
1292 | }
|
1293 | break;
|
1294 | }
|
1295 | }
|
1296 | }
|
1297 | }
|
1298 | return fields;
|
1299 | }
|
1300 | function aliasFieldNode(fieldNode, str) {
|
1301 | return {
|
1302 | ...fieldNode,
|
1303 | alias: {
|
1304 | kind: graphql.Kind.NAME,
|
1305 | value: str,
|
1306 | },
|
1307 | };
|
1308 | }
|
1309 | function hoistFieldNodes({ fieldNode, fieldNames, path, fragments, transformationContext, prefix, index = 0, wrappingPath = [], }) {
|
1310 | const alias = fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value;
|
1311 | let newFieldNodes = [];
|
1312 | if (index < path.length) {
|
1313 | const pathSegment = path[index];
|
1314 | for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
1315 | if (possibleFieldNode.name.value === pathSegment) {
|
1316 | const newWrappingPath = wrappingPath.concat([alias]);
|
1317 | newFieldNodes = newFieldNodes.concat(hoistFieldNodes({
|
1318 | fieldNode: possibleFieldNode,
|
1319 | fieldNames,
|
1320 | path,
|
1321 | fragments,
|
1322 | transformationContext,
|
1323 | prefix,
|
1324 | index: index + 1,
|
1325 | wrappingPath: newWrappingPath,
|
1326 | }));
|
1327 | }
|
1328 | }
|
1329 | }
|
1330 | else {
|
1331 | for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
1332 | if (!fieldNames || fieldNames.includes(possibleFieldNode.name.value)) {
|
1333 | const nextIndex = transformationContext.nextIndex;
|
1334 | transformationContext.nextIndex++;
|
1335 | const indexingAlias = `__${prefix}${nextIndex}__`;
|
1336 | transformationContext.paths[indexingAlias] = {
|
1337 | pathToField: wrappingPath.concat([alias]),
|
1338 | alias: possibleFieldNode.alias != null ? possibleFieldNode.alias.value : possibleFieldNode.name.value,
|
1339 | };
|
1340 | newFieldNodes.push(aliasFieldNode(possibleFieldNode, indexingAlias));
|
1341 | }
|
1342 | }
|
1343 | }
|
1344 | return newFieldNodes;
|
1345 | }
|
1346 | function dehoistValue(originalValue, context) {
|
1347 | if (originalValue == null) {
|
1348 | return originalValue;
|
1349 | }
|
1350 | const newValue = Object.create(null);
|
1351 | for (const alias in originalValue) {
|
1352 | let obj = newValue;
|
1353 | const path = context.paths[alias];
|
1354 | if (path == null) {
|
1355 | newValue[alias] = originalValue[alias];
|
1356 | continue;
|
1357 | }
|
1358 | const pathToField = path.pathToField;
|
1359 | const fieldAlias = path.alias;
|
1360 | for (const key of pathToField) {
|
1361 | obj = obj[key] = obj[key] || Object.create(null);
|
1362 | }
|
1363 | obj[fieldAlias] = originalValue[alias];
|
1364 | }
|
1365 | return newValue;
|
1366 | }
|
1367 | function dehoistErrors(errors, context) {
|
1368 | if (errors === undefined) {
|
1369 | return undefined;
|
1370 | }
|
1371 | return errors.map(error => {
|
1372 | const originalPath = error.path;
|
1373 | if (originalPath == null) {
|
1374 | return error;
|
1375 | }
|
1376 | let newPath = [];
|
1377 | for (const pathSegment of originalPath) {
|
1378 | if (typeof pathSegment !== 'string') {
|
1379 | newPath.push(pathSegment);
|
1380 | continue;
|
1381 | }
|
1382 | const path = context.paths[pathSegment];
|
1383 | if (path == null) {
|
1384 | newPath.push(pathSegment);
|
1385 | continue;
|
1386 | }
|
1387 | newPath = newPath.concat(path.pathToField, [path.alias]);
|
1388 | }
|
1389 | return utils.relocatedError(error, newPath);
|
1390 | });
|
1391 | }
|
1392 |
|
1393 | class WrapType {
|
1394 | constructor(outerTypeName, innerTypeName, fieldName) {
|
1395 | this.transformer = new WrapFields(outerTypeName, [fieldName], [innerTypeName]);
|
1396 | }
|
1397 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1398 | return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
1399 | }
|
1400 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
1401 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
1402 | }
|
1403 | transformResult(originalResult, delegationContext, transformationContext) {
|
1404 | return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
1405 | }
|
1406 | }
|
1407 |
|
1408 | class HoistField {
|
1409 | constructor(typeName, pathConfig, newFieldName, alias = '__gqtlw__') {
|
1410 | this.typeName = typeName;
|
1411 | this.newFieldName = newFieldName;
|
1412 | const path = pathConfig.map(segment => (typeof segment === 'string' ? segment : segment.fieldName));
|
1413 | this.argFilters = pathConfig.map((segment, index) => {
|
1414 | if (typeof segment === 'string' || segment.argFilter == null) {
|
1415 | return index === pathConfig.length - 1 ? () => true : () => false;
|
1416 | }
|
1417 | return segment.argFilter;
|
1418 | });
|
1419 | const pathToField = path.slice();
|
1420 | const oldFieldName = pathToField.pop();
|
1421 | if (oldFieldName == null) {
|
1422 | throw new Error(`Cannot hoist field to ${newFieldName} on type ${typeName}, no path provided.`);
|
1423 | }
|
1424 | this.oldFieldName = oldFieldName;
|
1425 | this.pathToField = pathToField;
|
1426 | const argLevels = Object.create(null);
|
1427 | this.transformer = new MapFields({
|
1428 | [typeName]: {
|
1429 | [newFieldName]: fieldNode => wrapFieldNode(renameFieldNode(fieldNode, oldFieldName), pathToField, alias, argLevels),
|
1430 | },
|
1431 | }, {
|
1432 | [typeName]: value => unwrapValue(value, alias),
|
1433 | }, errors => (errors != null ? unwrapErrors(errors, alias) : undefined));
|
1434 | this.argLevels = argLevels;
|
1435 | }
|
1436 | transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
1437 | var _a, _b, _c, _d;
|
1438 | const argsMap = Object.create(null);
|
1439 | const innerType = this.pathToField.reduce((acc, pathSegment, index) => {
|
1440 | const field = acc.getFields()[pathSegment];
|
1441 | for (const arg of field.args) {
|
1442 | if (this.argFilters[index](arg)) {
|
1443 | argsMap[arg.name] = arg;
|
1444 | this.argLevels[arg.name] = index;
|
1445 | }
|
1446 | }
|
1447 | return graphql.getNullableType(field.type);
|
1448 | }, originalWrappingSchema.getType(this.typeName));
|
1449 | let [newSchema, targetFieldConfigMap] = utils.removeObjectFields(originalWrappingSchema, innerType.name, fieldName => fieldName === this.oldFieldName);
|
1450 | const targetField = targetFieldConfigMap[this.oldFieldName];
|
1451 | let resolve;
|
1452 | if (transformedSchema) {
|
1453 | const hoistingToRootField = this.typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
|
1454 | this.typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
|
1455 | if (hoistingToRootField) {
|
1456 | const targetSchema = subschemaConfig.schema;
|
1457 | const operation = this.typeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
|
1458 | const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : defaultCreateProxyingResolver;
|
1459 | resolve = createProxyingResolver({
|
1460 | subschemaConfig,
|
1461 | transformedSchema,
|
1462 | operation,
|
1463 | fieldName: this.newFieldName,
|
1464 | });
|
1465 | }
|
1466 | else {
|
1467 | resolve = delegate.defaultMergedResolver;
|
1468 | }
|
1469 | }
|
1470 | const newTargetField = {
|
1471 | ...targetField,
|
1472 | resolve: resolve,
|
1473 | };
|
1474 | const level = this.pathToField.length;
|
1475 | const args = targetField.args;
|
1476 | if (args != null) {
|
1477 | for (const argName in args) {
|
1478 | const argConfig = args[argName];
|
1479 | if (argConfig == null) {
|
1480 | continue;
|
1481 | }
|
1482 | const arg = {
|
1483 | ...argConfig,
|
1484 | name: argName,
|
1485 | description: argConfig.description,
|
1486 | defaultValue: argConfig.defaultValue,
|
1487 | extensions: argConfig.extensions,
|
1488 | astNode: argConfig.astNode,
|
1489 | };
|
1490 | if (this.argFilters[level](arg)) {
|
1491 | argsMap[argName] = arg;
|
1492 | this.argLevels[arg.name] = level;
|
1493 | }
|
1494 | }
|
1495 | }
|
1496 | newTargetField.args = argsMap;
|
1497 | newSchema = utils.appendObjectFields(newSchema, this.typeName, {
|
1498 | [this.newFieldName]: newTargetField,
|
1499 | });
|
1500 | return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
|
1501 | }
|
1502 | transformRequest(originalRequest, delegationContext, transformationContext) {
|
1503 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
1504 | }
|
1505 | transformResult(originalResult, delegationContext, transformationContext) {
|
1506 | return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
1507 | }
|
1508 | }
|
1509 | function wrapFieldNode(fieldNode, path, alias, argLevels) {
|
1510 | return path.reduceRight((acc, fieldName, index) => ({
|
1511 | kind: graphql.Kind.FIELD,
|
1512 | alias: {
|
1513 | kind: graphql.Kind.NAME,
|
1514 | value: alias,
|
1515 | },
|
1516 | name: {
|
1517 | kind: graphql.Kind.NAME,
|
1518 | value: fieldName,
|
1519 | },
|
1520 | selectionSet: {
|
1521 | kind: graphql.Kind.SELECTION_SET,
|
1522 | selections: [acc],
|
1523 | },
|
1524 | arguments: fieldNode.arguments != null
|
1525 | ? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === index)
|
1526 | : undefined,
|
1527 | }), {
|
1528 | ...fieldNode,
|
1529 | arguments: fieldNode.arguments != null
|
1530 | ? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === path.length)
|
1531 | : undefined,
|
1532 | });
|
1533 | }
|
1534 | function renameFieldNode(fieldNode, name) {
|
1535 | return {
|
1536 | ...fieldNode,
|
1537 | alias: {
|
1538 | kind: graphql.Kind.NAME,
|
1539 | value: fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value,
|
1540 | },
|
1541 | name: {
|
1542 | kind: graphql.Kind.NAME,
|
1543 | value: name,
|
1544 | },
|
1545 | };
|
1546 | }
|
1547 | function unwrapValue(originalValue, alias) {
|
1548 | let newValue = originalValue;
|
1549 | let object = newValue[alias];
|
1550 | while (object != null) {
|
1551 | newValue = object;
|
1552 | object = newValue[alias];
|
1553 | }
|
1554 | delete originalValue[alias];
|
1555 | Object.assign(originalValue, newValue);
|
1556 | return originalValue;
|
1557 | }
|
1558 | function unwrapErrors(errors, alias) {
|
1559 | if (errors === undefined) {
|
1560 | return undefined;
|
1561 | }
|
1562 | return errors.map(error => {
|
1563 | const originalPath = error.path;
|
1564 | if (originalPath == null) {
|
1565 | return error;
|
1566 | }
|
1567 | const newPath = originalPath.filter(pathSegment => pathSegment !== alias);
|
1568 | return utils.relocatedError(error, newPath);
|
1569 | });
|
1570 | }
|
1571 |
|
1572 | class WrapQuery {
|
1573 | constructor(path, wrapper, extractor) {
|
1574 | this.path = path;
|
1575 | this.wrapper = wrapper;
|
1576 | this.extractor = extractor;
|
1577 | }
|
1578 | transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
1579 | const fieldPath = [];
|
1580 | const ourPath = JSON.stringify(this.path);
|
1581 | const document = graphql.visit(originalRequest.document, {
|
1582 | [graphql.Kind.FIELD]: {
|
1583 | enter: (node) => {
|
1584 | fieldPath.push(node.name.value);
|
1585 | if (node.selectionSet != null && ourPath === JSON.stringify(fieldPath)) {
|
1586 | const wrapResult = this.wrapper(node.selectionSet);
|
1587 |
|
1588 |
|
1589 | const selectionSet = wrapResult != null && wrapResult.kind === graphql.Kind.SELECTION_SET
|
1590 | ? wrapResult
|
1591 | : {
|
1592 | kind: graphql.Kind.SELECTION_SET,
|
1593 | selections: [wrapResult],
|
1594 | };
|
1595 | return {
|
1596 | ...node,
|
1597 | selectionSet,
|
1598 | };
|
1599 | }
|
1600 | },
|
1601 | leave: () => {
|
1602 | fieldPath.pop();
|
1603 | },
|
1604 | },
|
1605 | });
|
1606 | return {
|
1607 | ...originalRequest,
|
1608 | document,
|
1609 | };
|
1610 | }
|
1611 | transformResult(originalResult, _delegationContext, _transformationContext) {
|
1612 | const rootData = originalResult.data;
|
1613 | if (rootData != null) {
|
1614 | let data = rootData;
|
1615 | const path = [...this.path];
|
1616 | while (path.length > 1) {
|
1617 | const next = path.shift();
|
1618 | if (data[next]) {
|
1619 | data = data[next];
|
1620 | }
|
1621 | }
|
1622 | data[path[0]] = this.extractor(data[path[0]]);
|
1623 | }
|
1624 | return {
|
1625 | data: rootData,
|
1626 | errors: originalResult.errors,
|
1627 | };
|
1628 | }
|
1629 | }
|
1630 |
|
1631 | class ExtractField {
|
1632 | constructor({ from, to }) {
|
1633 | this.from = from;
|
1634 | this.to = to;
|
1635 | }
|
1636 | transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
1637 | let fromSelection;
|
1638 | const ourPathFrom = JSON.stringify(this.from);
|
1639 | const ourPathTo = JSON.stringify(this.to);
|
1640 | let fieldPath = [];
|
1641 | graphql.visit(originalRequest.document, {
|
1642 | [graphql.Kind.FIELD]: {
|
1643 | enter: (node) => {
|
1644 | fieldPath.push(node.name.value);
|
1645 | if (ourPathFrom === JSON.stringify(fieldPath)) {
|
1646 | fromSelection = node.selectionSet;
|
1647 | return graphql.BREAK;
|
1648 | }
|
1649 | },
|
1650 | leave: () => {
|
1651 | fieldPath.pop();
|
1652 | },
|
1653 | },
|
1654 | });
|
1655 | fieldPath = [];
|
1656 | const document = graphql.visit(originalRequest.document, {
|
1657 | [graphql.Kind.FIELD]: {
|
1658 | enter: (node) => {
|
1659 | fieldPath.push(node.name.value);
|
1660 | if (ourPathTo === JSON.stringify(fieldPath) && fromSelection != null) {
|
1661 | return {
|
1662 | ...node,
|
1663 | selectionSet: fromSelection,
|
1664 | };
|
1665 | }
|
1666 | },
|
1667 | leave: () => {
|
1668 | fieldPath.pop();
|
1669 | },
|
1670 | },
|
1671 | });
|
1672 | return {
|
1673 | ...originalRequest,
|
1674 | document,
|
1675 | };
|
1676 | }
|
1677 | }
|
1678 |
|
1679 | function getSchemaFromIntrospection(introspectionResult, options) {
|
1680 | var _a, _b;
|
1681 | if ((_a = introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.data) === null || _a === void 0 ? void 0 : _a.__schema) {
|
1682 | return graphql.buildClientSchema(introspectionResult.data, options);
|
1683 | }
|
1684 | else if ((_b = introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.errors) === null || _b === void 0 ? void 0 : _b.length) {
|
1685 | if (introspectionResult.errors.length > 1) {
|
1686 | const combinedError = new utils.AggregateError(introspectionResult.errors, 'Could not obtain introspection result');
|
1687 | throw combinedError;
|
1688 | }
|
1689 | const error = introspectionResult.errors[0];
|
1690 | throw error.originalError || error;
|
1691 | }
|
1692 | else {
|
1693 | throw new Error('Could not obtain introspection result, received: ' + JSON.stringify(introspectionResult));
|
1694 | }
|
1695 | }
|
1696 | function introspectSchema(executor, context, options) {
|
1697 | const parsedIntrospectionQuery = graphql.parse(graphql.getIntrospectionQuery(options), options);
|
1698 | return new valueOrPromise.ValueOrPromise(() => executor({
|
1699 | document: parsedIntrospectionQuery,
|
1700 | operationType: 'query',
|
1701 | context,
|
1702 | }))
|
1703 | .then(introspection => {
|
1704 | if (utils.isAsyncIterable(introspection)) {
|
1705 | return introspection.next().then(({ value }) => value);
|
1706 | }
|
1707 | return introspection;
|
1708 | })
|
1709 | .then(introspection => getSchemaFromIntrospection(introspection, options))
|
1710 | .resolve();
|
1711 | }
|
1712 |
|
1713 | exports.ExtractField = ExtractField;
|
1714 | exports.FilterInputObjectFields = FilterInputObjectFields;
|
1715 | exports.FilterInterfaceFields = FilterInterfaceFields;
|
1716 | exports.FilterObjectFieldDirectives = FilterObjectFieldDirectives;
|
1717 | exports.FilterObjectFields = FilterObjectFields;
|
1718 | exports.FilterRootFields = FilterRootFields;
|
1719 | exports.FilterTypes = FilterTypes;
|
1720 | exports.HoistField = HoistField;
|
1721 | exports.MapFields = MapFields;
|
1722 | exports.MapLeafValues = MapLeafValues;
|
1723 | exports.PruneSchema = PruneTypes;
|
1724 | exports.RemoveObjectFieldDeprecations = RemoveObjectFieldDeprecations;
|
1725 | exports.RemoveObjectFieldDirectives = RemoveObjectFieldDirectives;
|
1726 | exports.RemoveObjectFieldsWithDeprecation = RemoveObjectFieldsWithDeprecation;
|
1727 | exports.RemoveObjectFieldsWithDirective = RemoveObjectFieldsWithDirective;
|
1728 | exports.RenameInputObjectFields = RenameInputObjectFields;
|
1729 | exports.RenameInterfaceFields = RenameInterfaceFields;
|
1730 | exports.RenameObjectFields = RenameObjectFields;
|
1731 | exports.RenameRootFields = RenameRootFields;
|
1732 | exports.RenameRootTypes = RenameRootTypes;
|
1733 | exports.RenameTypes = RenameTypes;
|
1734 | exports.TransformCompositeFields = TransformCompositeFields;
|
1735 | exports.TransformEnumValues = TransformEnumValues;
|
1736 | exports.TransformInputObjectFields = TransformInputObjectFields;
|
1737 | exports.TransformInterfaceFields = TransformInterfaceFields;
|
1738 | exports.TransformObjectFields = TransformObjectFields;
|
1739 | exports.TransformQuery = TransformQuery;
|
1740 | exports.TransformRootFields = TransformRootFields;
|
1741 | exports.WrapFields = WrapFields;
|
1742 | exports.WrapQuery = WrapQuery;
|
1743 | exports.WrapType = WrapType;
|
1744 | exports.defaultCreateProxyingResolver = defaultCreateProxyingResolver;
|
1745 | exports.generateProxyingResolvers = generateProxyingResolvers;
|
1746 | exports.introspectSchema = introspectSchema;
|
1747 | exports.wrapSchema = wrapSchema;
|