@depup/graphql-tools__wrap 11.1.12-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2356 @@
1
+ import { delegateToSchema, isExternalObject, getUnpathedErrors, getSubschema, resolveExternalValue, applySchemaTransforms, defaultMergedResolver, getTypeInfo, isPrototypePollutingKey } from '@graphql-tools/delegate';
2
+ import { getRootTypeMap, getResponseKeyFromInfo, memoize1, mapSchema, MapperKind, renameType, visitData, transformInputValue, visitResult, astFromValueUntyped, getOperationASTFromRequest, relocatedError, getArgumentValues, valueMatchesCriteria, getDirectives, pruneSchema, selectObjectFields, appendObjectFields, modifyObjectFields, removeObjectFields, isAsyncIterable, createGraphQLError, inspect } from '@graphql-tools/utils';
3
+ import { isSpecifiedScalarType, GraphQLScalarType, GraphQLUnionType, GraphQLInterfaceType, GraphQLObjectType, isScalarType, visit, Kind, visitWithTypeInfo, isObjectType, isInterfaceType, typeFromAST, isInputType, getNamedType, isLeafType, valueFromAST, valueFromASTUntyped, astFromValue, GraphQLNonNull, getNullableType, isListType, GraphQLList, BREAK, parse, getIntrospectionQuery, buildClientSchema } from 'graphql';
4
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
5
+
6
+ function generateProxyingResolvers(subschemaConfig) {
7
+ const targetSchema = subschemaConfig.schema;
8
+ const createProxyingResolver = subschemaConfig.createProxyingResolver ?? defaultCreateProxyingResolver;
9
+ const rootTypeMap = getRootTypeMap(targetSchema);
10
+ const resolvers = {};
11
+ for (const [operation, rootType] of rootTypeMap.entries()) {
12
+ const typeName = rootType.name;
13
+ const fields = rootType.getFields();
14
+ resolvers[typeName] = {};
15
+ for (const fieldName in fields) {
16
+ const proxyingResolver = createProxyingResolver({
17
+ subschemaConfig,
18
+ operation,
19
+ fieldName
20
+ });
21
+ const finalResolver = createPossiblyNestedProxyingResolver(
22
+ subschemaConfig,
23
+ proxyingResolver
24
+ );
25
+ if (operation === "subscription") {
26
+ resolvers[typeName][fieldName] = {
27
+ subscribe: finalResolver,
28
+ resolve: identical
29
+ };
30
+ } else {
31
+ resolvers[typeName][fieldName] = {
32
+ resolve: finalResolver
33
+ };
34
+ }
35
+ }
36
+ }
37
+ return resolvers;
38
+ }
39
+ function identical(value) {
40
+ return value;
41
+ }
42
+ function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) {
43
+ return function possiblyNestedProxyingResolver(parent, args, context, info) {
44
+ if (parent != null) {
45
+ const responseKey = getResponseKeyFromInfo(info);
46
+ if (isExternalObject(parent)) {
47
+ const unpathedErrors = getUnpathedErrors(parent);
48
+ const subschema = getSubschema(parent, responseKey);
49
+ if (subschemaConfig === subschema && parent[responseKey] !== void 0) {
50
+ return resolveExternalValue(
51
+ parent[responseKey],
52
+ unpathedErrors,
53
+ subschema,
54
+ context,
55
+ info
56
+ );
57
+ }
58
+ }
59
+ }
60
+ return proxyingResolver(parent, args, context, info);
61
+ };
62
+ }
63
+ function defaultCreateProxyingResolver({
64
+ subschemaConfig,
65
+ operation
66
+ }) {
67
+ return function proxyingResolver(rootValue, args, context, info) {
68
+ return delegateToSchema({
69
+ schema: subschemaConfig,
70
+ operation,
71
+ rootValue,
72
+ args,
73
+ context,
74
+ info
75
+ });
76
+ };
77
+ }
78
+
79
+ const wrapSchema = memoize1(function wrapSchema2(subschemaConfig) {
80
+ const targetSchema = subschemaConfig.schema;
81
+ const proxyingResolvers = generateProxyingResolvers(subschemaConfig);
82
+ const schema = createWrappingSchema(targetSchema, proxyingResolvers);
83
+ const transformed = applySchemaTransforms(schema, subschemaConfig);
84
+ return transformed;
85
+ });
86
+ function createWrappingSchema(schema, proxyingResolvers) {
87
+ return mapSchema(schema, {
88
+ [MapperKind.ROOT_FIELD]: (fieldConfig, fieldName, typeName) => {
89
+ return {
90
+ ...fieldConfig,
91
+ ...proxyingResolvers[typeName]?.[fieldName]
92
+ };
93
+ },
94
+ [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
95
+ return {
96
+ ...fieldConfig,
97
+ resolve: defaultMergedResolver,
98
+ subscribe: void 0
99
+ };
100
+ },
101
+ [MapperKind.OBJECT_TYPE]: (type) => {
102
+ const config = type.toConfig();
103
+ return new GraphQLObjectType({
104
+ ...config,
105
+ isTypeOf: void 0
106
+ });
107
+ },
108
+ [MapperKind.INTERFACE_TYPE]: (type) => {
109
+ const config = type.toConfig();
110
+ return new GraphQLInterfaceType({
111
+ ...config,
112
+ resolveType: void 0
113
+ });
114
+ },
115
+ [MapperKind.UNION_TYPE]: (type) => {
116
+ const config = type.toConfig();
117
+ return new GraphQLUnionType({
118
+ ...config,
119
+ resolveType: void 0
120
+ });
121
+ },
122
+ [MapperKind.ENUM_VALUE]: (valueConfig) => {
123
+ return {
124
+ ...valueConfig,
125
+ value: void 0
126
+ };
127
+ },
128
+ [MapperKind.SCALAR_TYPE]: (type) => {
129
+ if (isSpecifiedScalarType(type)) {
130
+ return type;
131
+ }
132
+ return new GraphQLScalarType({
133
+ ...type.toConfig(),
134
+ serialize: void 0,
135
+ parseValue: void 0,
136
+ parseLiteral: void 0
137
+ });
138
+ }
139
+ });
140
+ }
141
+
142
+ class RenameTypes {
143
+ renamer;
144
+ map;
145
+ reverseMap;
146
+ renameBuiltins;
147
+ renameScalars;
148
+ constructor(renamer, options) {
149
+ this.renamer = renamer;
150
+ this.map = /* @__PURE__ */ Object.create(null);
151
+ this.reverseMap = /* @__PURE__ */ Object.create(null);
152
+ const { renameBuiltins = false, renameScalars = true } = options != null ? options : {};
153
+ this.renameBuiltins = renameBuiltins;
154
+ this.renameScalars = renameScalars;
155
+ }
156
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
157
+ const typeNames = new Set(
158
+ Object.keys(originalWrappingSchema.getTypeMap())
159
+ );
160
+ return mapSchema(originalWrappingSchema, {
161
+ [MapperKind.TYPE]: (type) => {
162
+ if (isSpecifiedScalarType(type) && !this.renameBuiltins) {
163
+ return void 0;
164
+ }
165
+ if (isScalarType(type) && !this.renameScalars) {
166
+ return void 0;
167
+ }
168
+ const oldName = type.name;
169
+ const newName = this.renamer(oldName);
170
+ if (newName !== void 0 && newName !== oldName) {
171
+ if (typeNames.has(newName)) {
172
+ console.warn(
173
+ `New type name ${newName} for ${oldName} already exists in the schema. Skip renaming.`
174
+ );
175
+ return;
176
+ }
177
+ this.map[oldName] = newName;
178
+ this.reverseMap[newName] = oldName;
179
+ typeNames.delete(oldName);
180
+ typeNames.add(newName);
181
+ return renameType(type, newName);
182
+ }
183
+ return void 0;
184
+ },
185
+ [MapperKind.ROOT_OBJECT]() {
186
+ return void 0;
187
+ }
188
+ });
189
+ }
190
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
191
+ const document = visit(originalRequest.document, {
192
+ [Kind.NAMED_TYPE]: (node) => {
193
+ const name = node.name.value;
194
+ if (name in this.reverseMap) {
195
+ return {
196
+ ...node,
197
+ name: {
198
+ kind: Kind.NAME,
199
+ value: this.reverseMap[name]
200
+ }
201
+ };
202
+ }
203
+ return void 0;
204
+ }
205
+ });
206
+ return {
207
+ ...originalRequest,
208
+ document
209
+ };
210
+ }
211
+ transformResult(originalResult, _delegationContext, _transformationContext) {
212
+ return {
213
+ ...originalResult,
214
+ data: visitData(originalResult.data, (object) => {
215
+ const typeName = object?.__typename;
216
+ if (typeName != null && typeName in this.map) {
217
+ object.__typename = this.map[typeName];
218
+ }
219
+ return object;
220
+ })
221
+ };
222
+ }
223
+ }
224
+
225
+ class FilterTypes {
226
+ filter;
227
+ constructor(filter) {
228
+ this.filter = filter;
229
+ }
230
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
231
+ return mapSchema(originalWrappingSchema, {
232
+ [MapperKind.TYPE]: (type) => {
233
+ if (this.filter(type)) {
234
+ return void 0;
235
+ }
236
+ return null;
237
+ }
238
+ });
239
+ }
240
+ }
241
+
242
+ class RenameRootTypes {
243
+ renamer;
244
+ map;
245
+ reverseMap;
246
+ constructor(renamer) {
247
+ this.renamer = renamer;
248
+ this.map = /* @__PURE__ */ Object.create(null);
249
+ this.reverseMap = /* @__PURE__ */ Object.create(null);
250
+ }
251
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
252
+ return mapSchema(originalWrappingSchema, {
253
+ [MapperKind.ROOT_OBJECT]: (type) => {
254
+ const oldName = type.name;
255
+ const newName = this.renamer(oldName);
256
+ if (newName !== void 0 && newName !== oldName) {
257
+ this.map[oldName] = newName;
258
+ this.reverseMap[newName] = oldName;
259
+ return renameType(type, newName);
260
+ }
261
+ return void 0;
262
+ }
263
+ });
264
+ }
265
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
266
+ const document = visit(originalRequest.document, {
267
+ [Kind.NAMED_TYPE]: (node) => {
268
+ const name = node.name.value;
269
+ if (name in this.reverseMap) {
270
+ return {
271
+ ...node,
272
+ name: {
273
+ kind: Kind.NAME,
274
+ value: this.reverseMap[name]
275
+ }
276
+ };
277
+ }
278
+ return void 0;
279
+ }
280
+ });
281
+ return {
282
+ ...originalRequest,
283
+ document
284
+ };
285
+ }
286
+ transformResult(originalResult, _delegationContext, _transformationContext) {
287
+ return {
288
+ ...originalResult,
289
+ data: visitData(originalResult.data, (object) => {
290
+ const typeName = object?.__typename;
291
+ if (typeName != null && typeName in this.map) {
292
+ object.__typename = this.map[typeName];
293
+ }
294
+ return object;
295
+ })
296
+ };
297
+ }
298
+ }
299
+
300
+ class TransformCompositeFields {
301
+ fieldTransformer;
302
+ fieldNodeTransformer;
303
+ dataTransformer;
304
+ errorsTransformer;
305
+ transformedSchema;
306
+ typeInfo;
307
+ mapping;
308
+ subscriptionTypeName;
309
+ constructor(fieldTransformer, fieldNodeTransformer, dataTransformer, errorsTransformer) {
310
+ this.fieldTransformer = fieldTransformer;
311
+ this.fieldNodeTransformer = fieldNodeTransformer;
312
+ this.dataTransformer = dataTransformer;
313
+ this.errorsTransformer = errorsTransformer;
314
+ this.mapping = {};
315
+ }
316
+ _getTypeInfo() {
317
+ const typeInfo = this.typeInfo;
318
+ if (typeInfo === void 0) {
319
+ throw new Error(
320
+ `The TransformCompositeFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
321
+ );
322
+ }
323
+ return typeInfo;
324
+ }
325
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
326
+ this.transformedSchema = mapSchema(originalWrappingSchema, {
327
+ [MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
328
+ const transformedField = this.fieldTransformer(
329
+ typeName,
330
+ fieldName,
331
+ fieldConfig
332
+ );
333
+ if (Array.isArray(transformedField)) {
334
+ const newFieldName = transformedField[0];
335
+ if (newFieldName !== fieldName) {
336
+ if (!this.mapping[typeName]) {
337
+ this.mapping[typeName] = {};
338
+ }
339
+ this.mapping[typeName][newFieldName] = fieldName;
340
+ }
341
+ }
342
+ return transformedField;
343
+ }
344
+ });
345
+ this.typeInfo = getTypeInfo(this.transformedSchema);
346
+ this.subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name;
347
+ return this.transformedSchema;
348
+ }
349
+ transformRequest(originalRequest, _delegationContext, transformationContext) {
350
+ const document = originalRequest.document;
351
+ return {
352
+ ...originalRequest,
353
+ document: this.transformDocument(document, transformationContext)
354
+ };
355
+ }
356
+ transformResult(result, _delegationContext, transformationContext) {
357
+ const dataTransformer = this.dataTransformer;
358
+ if (dataTransformer != null) {
359
+ result.data = visitData(
360
+ result.data,
361
+ (value) => dataTransformer(value, transformationContext)
362
+ );
363
+ }
364
+ if (this.errorsTransformer != null && Array.isArray(result.errors)) {
365
+ result.errors = this.errorsTransformer(
366
+ result.errors,
367
+ transformationContext
368
+ );
369
+ }
370
+ return result;
371
+ }
372
+ transformDocument(document, transformationContext) {
373
+ const fragments = /* @__PURE__ */ Object.create(null);
374
+ for (const def of document.definitions) {
375
+ if (def.kind === Kind.FRAGMENT_DEFINITION) {
376
+ fragments[def.name.value] = def;
377
+ }
378
+ }
379
+ return visit(
380
+ document,
381
+ visitWithTypeInfo(this._getTypeInfo(), {
382
+ [Kind.SELECTION_SET]: {
383
+ leave: (node) => this.transformSelectionSet(
384
+ node,
385
+ this._getTypeInfo(),
386
+ fragments,
387
+ transformationContext
388
+ )
389
+ }
390
+ })
391
+ );
392
+ }
393
+ transformSelectionSet(node, typeInfo, fragments, transformationContext) {
394
+ const parentType = typeInfo.getParentType();
395
+ if (parentType == null) {
396
+ return void 0;
397
+ }
398
+ const parentTypeName = parentType.name;
399
+ let newSelections = [];
400
+ let isTypenameSelected = false;
401
+ for (const selection of node.selections) {
402
+ if (selection.kind !== Kind.FIELD) {
403
+ newSelections.push(selection);
404
+ continue;
405
+ }
406
+ if (selection.name.value === "__typename" && (!selection.alias || selection.alias.value === "__typename")) {
407
+ isTypenameSelected = true;
408
+ }
409
+ const newName = selection.name.value;
410
+ let transformedSelection;
411
+ if (this.fieldNodeTransformer == null) {
412
+ transformedSelection = selection;
413
+ } else {
414
+ transformedSelection = this.fieldNodeTransformer(
415
+ parentTypeName,
416
+ newName,
417
+ selection,
418
+ fragments,
419
+ transformationContext
420
+ );
421
+ transformedSelection = transformedSelection === void 0 ? selection : transformedSelection;
422
+ }
423
+ if (transformedSelection == null) {
424
+ continue;
425
+ } else if (Array.isArray(transformedSelection)) {
426
+ newSelections = newSelections.concat(transformedSelection);
427
+ continue;
428
+ } else if (transformedSelection.kind !== Kind.FIELD) {
429
+ newSelections.push(transformedSelection);
430
+ continue;
431
+ }
432
+ const typeMapping = this.mapping[parentTypeName];
433
+ if (typeMapping == null) {
434
+ newSelections.push(transformedSelection);
435
+ continue;
436
+ }
437
+ const oldName = this.mapping[parentTypeName][newName];
438
+ if (oldName == null) {
439
+ newSelections.push(transformedSelection);
440
+ continue;
441
+ }
442
+ newSelections.push({
443
+ ...transformedSelection,
444
+ name: {
445
+ kind: Kind.NAME,
446
+ value: oldName
447
+ },
448
+ alias: {
449
+ kind: Kind.NAME,
450
+ value: transformedSelection.alias?.value ?? newName
451
+ }
452
+ });
453
+ }
454
+ if (!isTypenameSelected && (this.dataTransformer != null || this.errorsTransformer != null) && (this.subscriptionTypeName == null || parentTypeName !== this.subscriptionTypeName)) {
455
+ newSelections.push({
456
+ kind: Kind.FIELD,
457
+ name: {
458
+ kind: Kind.NAME,
459
+ value: "__typename"
460
+ }
461
+ });
462
+ }
463
+ return {
464
+ ...node,
465
+ selections: newSelections
466
+ };
467
+ }
468
+ }
469
+
470
+ class TransformObjectFields {
471
+ objectFieldTransformer;
472
+ fieldNodeTransformer;
473
+ transformer;
474
+ constructor(objectFieldTransformer, fieldNodeTransformer) {
475
+ this.objectFieldTransformer = objectFieldTransformer;
476
+ this.fieldNodeTransformer = fieldNodeTransformer;
477
+ }
478
+ _getTransformer() {
479
+ const transformer = this.transformer;
480
+ if (transformer === void 0) {
481
+ throw new Error(
482
+ `The TransformObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
483
+ );
484
+ }
485
+ return transformer;
486
+ }
487
+ transformSchema(originalWrappingSchema, subschemaConfig) {
488
+ const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
489
+ if (isObjectType(originalWrappingSchema.getType(typeName))) {
490
+ return this.objectFieldTransformer(typeName, fieldName, fieldConfig);
491
+ }
492
+ return void 0;
493
+ };
494
+ this.transformer = new TransformCompositeFields(
495
+ compositeToObjectFieldTransformer,
496
+ this.fieldNodeTransformer
497
+ );
498
+ return this.transformer.transformSchema(
499
+ originalWrappingSchema,
500
+ subschemaConfig
501
+ );
502
+ }
503
+ transformRequest(originalRequest, delegationContext, transformationContext) {
504
+ return this._getTransformer().transformRequest(
505
+ originalRequest,
506
+ delegationContext,
507
+ transformationContext
508
+ );
509
+ }
510
+ transformResult(originalResult, delegationContext, transformationContext) {
511
+ return this._getTransformer().transformResult(
512
+ originalResult,
513
+ delegationContext,
514
+ transformationContext
515
+ );
516
+ }
517
+ }
518
+
519
+ class TransformRootFields {
520
+ rootFieldTransformer;
521
+ fieldNodeTransformer;
522
+ transformer;
523
+ constructor(rootFieldTransformer, fieldNodeTransformer) {
524
+ this.rootFieldTransformer = rootFieldTransformer;
525
+ this.fieldNodeTransformer = fieldNodeTransformer;
526
+ }
527
+ _getTransformer() {
528
+ const transformer = this.transformer;
529
+ if (transformer === void 0) {
530
+ throw new Error(
531
+ `The TransformRootFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
532
+ );
533
+ }
534
+ return transformer;
535
+ }
536
+ transformSchema(originalWrappingSchema, subschemaConfig) {
537
+ const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
538
+ if (typeName === originalWrappingSchema.getQueryType()?.name) {
539
+ return this.rootFieldTransformer("Query", fieldName, fieldConfig);
540
+ }
541
+ if (typeName === originalWrappingSchema.getMutationType()?.name) {
542
+ return this.rootFieldTransformer("Mutation", fieldName, fieldConfig);
543
+ }
544
+ if (typeName === originalWrappingSchema.getSubscriptionType()?.name) {
545
+ return this.rootFieldTransformer(
546
+ "Subscription",
547
+ fieldName,
548
+ fieldConfig
549
+ );
550
+ }
551
+ return void 0;
552
+ };
553
+ this.transformer = new TransformObjectFields(
554
+ rootToObjectFieldTransformer,
555
+ this.fieldNodeTransformer
556
+ );
557
+ return this.transformer.transformSchema(
558
+ originalWrappingSchema,
559
+ subschemaConfig
560
+ );
561
+ }
562
+ transformRequest(originalRequest, delegationContext, transformationContext) {
563
+ return this._getTransformer().transformRequest(
564
+ originalRequest,
565
+ delegationContext,
566
+ transformationContext
567
+ );
568
+ }
569
+ transformResult(originalResult, delegationContext, transformationContext) {
570
+ return this._getTransformer().transformResult(
571
+ originalResult,
572
+ delegationContext,
573
+ transformationContext
574
+ );
575
+ }
576
+ }
577
+
578
+ class RenameRootFields {
579
+ transformer;
580
+ constructor(renamer) {
581
+ this.transformer = new TransformRootFields(
582
+ (operation, fieldName, fieldConfig) => [renamer(operation, fieldName, fieldConfig), fieldConfig]
583
+ );
584
+ }
585
+ transformSchema(originalWrappingSchema, subschemaConfig) {
586
+ return this.transformer.transformSchema(
587
+ originalWrappingSchema,
588
+ subschemaConfig
589
+ );
590
+ }
591
+ transformRequest(originalRequest, delegationContext, transformationContext) {
592
+ return this.transformer.transformRequest(
593
+ originalRequest,
594
+ delegationContext,
595
+ transformationContext
596
+ );
597
+ }
598
+ }
599
+
600
+ class FilterRootFields {
601
+ transformer;
602
+ constructor(filter) {
603
+ this.transformer = new TransformRootFields(
604
+ (operation, fieldName, fieldConfig) => {
605
+ if (filter(operation, fieldName, fieldConfig)) {
606
+ return void 0;
607
+ }
608
+ return null;
609
+ }
610
+ );
611
+ }
612
+ transformSchema(originalWrappingSchema, subschemaConfig) {
613
+ return this.transformer.transformSchema(
614
+ originalWrappingSchema,
615
+ subschemaConfig
616
+ );
617
+ }
618
+ }
619
+
620
+ class RenameObjectFields {
621
+ transformer;
622
+ constructor(renamer) {
623
+ this.transformer = new TransformObjectFields(
624
+ (typeName, fieldName, fieldConfig) => [renamer(typeName, fieldName, fieldConfig), fieldConfig]
625
+ );
626
+ }
627
+ transformSchema(originalWrappingSchema, subschemaConfig) {
628
+ return this.transformer.transformSchema(
629
+ originalWrappingSchema,
630
+ subschemaConfig
631
+ );
632
+ }
633
+ transformRequest(originalRequest, delegationContext, transformationContext) {
634
+ return this.transformer.transformRequest(
635
+ originalRequest,
636
+ delegationContext,
637
+ transformationContext
638
+ );
639
+ }
640
+ }
641
+
642
+ class RenameObjectFieldArguments {
643
+ renamer;
644
+ transformer;
645
+ reverseMap;
646
+ transformedSchema;
647
+ constructor(renamer) {
648
+ this.renamer = renamer;
649
+ this.transformer = new TransformObjectFields(
650
+ (typeName, fieldName, fieldConfig) => {
651
+ const argsConfig = Object.fromEntries(
652
+ Object.entries(fieldConfig.args || []).map(([argName, conf]) => {
653
+ const newName = renamer(typeName, fieldName, argName);
654
+ if (newName !== void 0 && newName !== argName) {
655
+ if (newName != null) {
656
+ return [newName, conf];
657
+ }
658
+ }
659
+ return [argName, conf];
660
+ })
661
+ );
662
+ return [fieldName, { ...fieldConfig, args: argsConfig }];
663
+ },
664
+ (typeName, fieldName, inputFieldNode) => {
665
+ if (!(typeName in this.reverseMap)) {
666
+ return inputFieldNode;
667
+ }
668
+ if (!(fieldName in this.reverseMap[typeName])) {
669
+ return inputFieldNode;
670
+ }
671
+ const fieldNameMap = this.reverseMap[typeName][fieldName];
672
+ return {
673
+ ...inputFieldNode,
674
+ arguments: (inputFieldNode.arguments || []).map((argNode) => {
675
+ return argNode.name.value in fieldNameMap ? {
676
+ ...argNode,
677
+ name: {
678
+ ...argNode.name,
679
+ value: fieldNameMap[argNode.name.value]
680
+ }
681
+ } : argNode;
682
+ })
683
+ };
684
+ }
685
+ );
686
+ this.reverseMap = /* @__PURE__ */ Object.create(null);
687
+ }
688
+ transformSchema(originalWrappingSchema, subschemaConfig) {
689
+ mapSchema(originalWrappingSchema, {
690
+ [MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => {
691
+ Object.entries(fieldConfig.args || {}).forEach(([argName]) => {
692
+ const newName = this.renamer(typeName, fieldName, argName);
693
+ if (newName !== void 0 && newName !== fieldName) {
694
+ if (this.reverseMap[typeName] == null) {
695
+ this.reverseMap[typeName] = /* @__PURE__ */ Object.create(null);
696
+ }
697
+ if (this.reverseMap[typeName][fieldName] == null) {
698
+ this.reverseMap[typeName][fieldName] = /* @__PURE__ */ Object.create(null);
699
+ }
700
+ this.reverseMap[typeName][fieldName][newName] = argName;
701
+ }
702
+ });
703
+ return void 0;
704
+ },
705
+ [MapperKind.ROOT_OBJECT]() {
706
+ return void 0;
707
+ }
708
+ });
709
+ this.transformedSchema = this.transformer.transformSchema(
710
+ originalWrappingSchema,
711
+ subschemaConfig
712
+ );
713
+ return this.transformedSchema;
714
+ }
715
+ transformRequest(originalRequest, delegationContext, transformationContext) {
716
+ return this.transformer.transformRequest(
717
+ originalRequest,
718
+ delegationContext,
719
+ transformationContext
720
+ );
721
+ }
722
+ }
723
+
724
+ class FilterObjectFields {
725
+ transformer;
726
+ constructor(filter) {
727
+ this.transformer = new TransformObjectFields(
728
+ (typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? void 0 : null
729
+ );
730
+ }
731
+ transformSchema(originalWrappingSchema, subschemaConfig) {
732
+ return this.transformer.transformSchema(
733
+ originalWrappingSchema,
734
+ subschemaConfig
735
+ );
736
+ }
737
+ }
738
+
739
+ class TransformInterfaceFields {
740
+ interfaceFieldTransformer;
741
+ fieldNodeTransformer;
742
+ transformer;
743
+ constructor(interfaceFieldTransformer, fieldNodeTransformer) {
744
+ this.interfaceFieldTransformer = interfaceFieldTransformer;
745
+ this.fieldNodeTransformer = fieldNodeTransformer;
746
+ }
747
+ _getTransformer() {
748
+ const transformer = this.transformer;
749
+ if (transformer === void 0) {
750
+ throw new Error(
751
+ `The TransformInterfaceFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
752
+ );
753
+ }
754
+ return transformer;
755
+ }
756
+ transformSchema(originalWrappingSchema, subschemaConfig) {
757
+ const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
758
+ if (isInterfaceType(originalWrappingSchema.getType(typeName))) {
759
+ return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig);
760
+ }
761
+ return void 0;
762
+ };
763
+ this.transformer = new TransformCompositeFields(
764
+ compositeToObjectFieldTransformer,
765
+ this.fieldNodeTransformer
766
+ );
767
+ return this.transformer.transformSchema(
768
+ originalWrappingSchema,
769
+ subschemaConfig
770
+ );
771
+ }
772
+ transformRequest(originalRequest, delegationContext, transformationContext) {
773
+ return this._getTransformer().transformRequest(
774
+ originalRequest,
775
+ delegationContext,
776
+ transformationContext
777
+ );
778
+ }
779
+ transformResult(originalResult, delegationContext, transformationContext) {
780
+ return this._getTransformer().transformResult(
781
+ originalResult,
782
+ delegationContext,
783
+ transformationContext
784
+ );
785
+ }
786
+ }
787
+
788
+ class RenameInterfaceFields {
789
+ transformer;
790
+ constructor(renamer) {
791
+ this.transformer = new TransformInterfaceFields(
792
+ (typeName, fieldName, fieldConfig) => [renamer(typeName, fieldName, fieldConfig), fieldConfig]
793
+ );
794
+ }
795
+ transformSchema(originalWrappingSchema, subschemaConfig) {
796
+ return this.transformer.transformSchema(
797
+ originalWrappingSchema,
798
+ subschemaConfig
799
+ );
800
+ }
801
+ transformRequest(originalRequest, delegationContext, transformationContext) {
802
+ return this.transformer.transformRequest(
803
+ originalRequest,
804
+ delegationContext,
805
+ transformationContext
806
+ );
807
+ }
808
+ }
809
+
810
+ class FilterInterfaceFields {
811
+ transformer;
812
+ constructor(filter) {
813
+ this.transformer = new TransformInterfaceFields(
814
+ (typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? void 0 : null
815
+ );
816
+ }
817
+ transformSchema(originalWrappingSchema, subschemaConfig) {
818
+ return this.transformer.transformSchema(
819
+ originalWrappingSchema,
820
+ subschemaConfig
821
+ );
822
+ }
823
+ }
824
+
825
+ class TransformInputObjectFields {
826
+ inputFieldTransformer;
827
+ inputFieldNodeTransformer;
828
+ inputObjectNodeTransformer;
829
+ transformedSchema;
830
+ mapping;
831
+ constructor(inputFieldTransformer, inputFieldNodeTransformer, inputObjectNodeTransformer) {
832
+ this.inputFieldTransformer = inputFieldTransformer;
833
+ this.inputFieldNodeTransformer = inputFieldNodeTransformer;
834
+ this.inputObjectNodeTransformer = inputObjectNodeTransformer;
835
+ this.mapping = {};
836
+ }
837
+ _getTransformedSchema() {
838
+ const transformedSchema = this.transformedSchema;
839
+ if (transformedSchema === void 0) {
840
+ throw new Error(
841
+ `The TransformInputObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
842
+ );
843
+ }
844
+ return transformedSchema;
845
+ }
846
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
847
+ this.transformedSchema = mapSchema(originalWrappingSchema, {
848
+ [MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
849
+ const transformedInputField = this.inputFieldTransformer(
850
+ typeName,
851
+ fieldName,
852
+ inputFieldConfig
853
+ );
854
+ if (Array.isArray(transformedInputField)) {
855
+ const newFieldName = transformedInputField[0];
856
+ if (newFieldName !== fieldName) {
857
+ if (!this.mapping[typeName]) {
858
+ this.mapping[typeName] = {};
859
+ }
860
+ this.mapping[typeName][newFieldName] = fieldName;
861
+ }
862
+ }
863
+ return transformedInputField;
864
+ }
865
+ });
866
+ return this.transformedSchema;
867
+ }
868
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
869
+ const variableValues = originalRequest.variables ?? {};
870
+ const fragments = /* @__PURE__ */ Object.create(null);
871
+ const operations = [];
872
+ for (const def of originalRequest.document.definitions) {
873
+ if (def.kind === Kind.OPERATION_DEFINITION) {
874
+ operations.push(def);
875
+ } else if (def.kind === Kind.FRAGMENT_DEFINITION) {
876
+ fragments[def.name.value] = def;
877
+ }
878
+ }
879
+ for (const def of operations) {
880
+ const variableDefs = def.variableDefinitions;
881
+ if (variableDefs != null) {
882
+ for (const variableDef of variableDefs) {
883
+ const varName = variableDef.variable.name.value;
884
+ if (!this.transformedSchema) {
885
+ continue;
886
+ }
887
+ const varType = typeFromAST(
888
+ this.transformedSchema,
889
+ variableDef.type
890
+ );
891
+ if (!isInputType(varType)) {
892
+ continue;
893
+ }
894
+ variableValues[varName] = transformInputValue(
895
+ varType,
896
+ variableValues[varName],
897
+ void 0,
898
+ (type, originalValue) => {
899
+ const newValue = /* @__PURE__ */ Object.create(null);
900
+ const fields = type.getFields();
901
+ for (const key in originalValue) {
902
+ const field = fields[key];
903
+ if (field != null) {
904
+ const newFieldName = this.mapping[type.name]?.[field.name];
905
+ if (newFieldName != null) {
906
+ newValue[newFieldName] = originalValue[field.name];
907
+ } else {
908
+ newValue[field.name] = originalValue[field.name];
909
+ }
910
+ }
911
+ }
912
+ return newValue;
913
+ }
914
+ );
915
+ }
916
+ }
917
+ }
918
+ for (const def of originalRequest.document.definitions.filter(
919
+ (def2) => def2.kind === Kind.FRAGMENT_DEFINITION
920
+ )) {
921
+ fragments[def.name.value] = def;
922
+ }
923
+ const document = this.transformDocument(
924
+ originalRequest.document,
925
+ this.mapping,
926
+ this.inputFieldNodeTransformer,
927
+ this.inputObjectNodeTransformer,
928
+ originalRequest
929
+ );
930
+ return {
931
+ ...originalRequest,
932
+ document,
933
+ variables: variableValues
934
+ };
935
+ }
936
+ transformDocument(document, mapping, inputFieldNodeTransformer, inputObjectNodeTransformer, request) {
937
+ const typeInfo = getTypeInfo(this._getTransformedSchema());
938
+ const newDocument = visit(
939
+ document,
940
+ visitWithTypeInfo(typeInfo, {
941
+ [Kind.OBJECT]: {
942
+ leave: (node) => {
943
+ const parentType = typeInfo.getInputType();
944
+ if (parentType != null) {
945
+ const parentTypeName = getNamedType(parentType).name;
946
+ const newInputFields = [];
947
+ for (const inputField of node.fields) {
948
+ const newName = inputField.name.value;
949
+ const transformedInputField = inputFieldNodeTransformer != null ? inputFieldNodeTransformer(
950
+ parentTypeName,
951
+ newName,
952
+ inputField,
953
+ request
954
+ ) : inputField;
955
+ if (Array.isArray(transformedInputField)) {
956
+ for (const individualTransformedInputField of transformedInputField) {
957
+ const typeMapping2 = mapping[parentTypeName];
958
+ if (typeMapping2 == null) {
959
+ newInputFields.push(individualTransformedInputField);
960
+ continue;
961
+ }
962
+ const oldName2 = typeMapping2[newName];
963
+ if (oldName2 == null) {
964
+ newInputFields.push(individualTransformedInputField);
965
+ continue;
966
+ }
967
+ newInputFields.push({
968
+ ...individualTransformedInputField,
969
+ name: {
970
+ ...individualTransformedInputField.name,
971
+ value: oldName2
972
+ }
973
+ });
974
+ }
975
+ continue;
976
+ }
977
+ const typeMapping = mapping[parentTypeName];
978
+ if (typeMapping == null) {
979
+ newInputFields.push(transformedInputField);
980
+ continue;
981
+ }
982
+ const oldName = typeMapping[newName];
983
+ if (oldName == null) {
984
+ newInputFields.push(transformedInputField);
985
+ continue;
986
+ }
987
+ newInputFields.push({
988
+ ...transformedInputField,
989
+ name: {
990
+ ...transformedInputField.name,
991
+ value: oldName
992
+ }
993
+ });
994
+ }
995
+ const newNode = {
996
+ ...node,
997
+ fields: newInputFields
998
+ };
999
+ return inputObjectNodeTransformer != null ? inputObjectNodeTransformer(parentTypeName, newNode, request) : newNode;
1000
+ }
1001
+ }
1002
+ }
1003
+ })
1004
+ );
1005
+ return newDocument;
1006
+ }
1007
+ }
1008
+
1009
+ class RenameInputObjectFields {
1010
+ renamer;
1011
+ transformer;
1012
+ reverseMap;
1013
+ constructor(renamer) {
1014
+ this.renamer = renamer;
1015
+ this.transformer = new TransformInputObjectFields(
1016
+ (typeName, inputFieldName, inputFieldConfig) => {
1017
+ const newName = renamer(typeName, inputFieldName, inputFieldConfig);
1018
+ if (newName !== void 0 && newName !== inputFieldName) {
1019
+ const value = renamer(typeName, inputFieldName, inputFieldConfig);
1020
+ if (value != null) {
1021
+ return [value, inputFieldConfig];
1022
+ }
1023
+ }
1024
+ return void 0;
1025
+ },
1026
+ (typeName, inputFieldName, inputFieldNode) => {
1027
+ if (!(typeName in this.reverseMap)) {
1028
+ return inputFieldNode;
1029
+ }
1030
+ const inputFieldNameMap = this.reverseMap[typeName];
1031
+ if (!(inputFieldName in inputFieldNameMap)) {
1032
+ return inputFieldNode;
1033
+ }
1034
+ return {
1035
+ ...inputFieldNode,
1036
+ name: {
1037
+ ...inputFieldNode.name,
1038
+ value: inputFieldNameMap[inputFieldName]
1039
+ }
1040
+ };
1041
+ }
1042
+ );
1043
+ this.reverseMap = /* @__PURE__ */ Object.create(null);
1044
+ }
1045
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1046
+ mapSchema(originalWrappingSchema, {
1047
+ [MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
1048
+ const newName = this.renamer(typeName, fieldName, inputFieldConfig);
1049
+ if (newName !== void 0 && newName !== fieldName) {
1050
+ if (this.reverseMap[typeName] == null) {
1051
+ this.reverseMap[typeName] = /* @__PURE__ */ Object.create(null);
1052
+ }
1053
+ this.reverseMap[typeName][newName] = fieldName;
1054
+ }
1055
+ return void 0;
1056
+ },
1057
+ [MapperKind.ROOT_OBJECT]() {
1058
+ return void 0;
1059
+ }
1060
+ });
1061
+ return this.transformer.transformSchema(
1062
+ originalWrappingSchema,
1063
+ subschemaConfig
1064
+ );
1065
+ }
1066
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1067
+ return this.transformer.transformRequest(
1068
+ originalRequest,
1069
+ delegationContext,
1070
+ transformationContext
1071
+ );
1072
+ }
1073
+ }
1074
+
1075
+ class FilterInputObjectFields {
1076
+ transformer;
1077
+ constructor(filter, inputObjectNodeTransformer) {
1078
+ this.transformer = new TransformInputObjectFields(
1079
+ (typeName, fieldName, inputFieldConfig) => filter(typeName, fieldName, inputFieldConfig) ? void 0 : null,
1080
+ void 0,
1081
+ inputObjectNodeTransformer
1082
+ );
1083
+ }
1084
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1085
+ return this.transformer.transformSchema(
1086
+ originalWrappingSchema,
1087
+ subschemaConfig
1088
+ );
1089
+ }
1090
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1091
+ return this.transformer.transformRequest(
1092
+ originalRequest,
1093
+ delegationContext,
1094
+ transformationContext
1095
+ );
1096
+ }
1097
+ }
1098
+
1099
+ class MapLeafValues {
1100
+ inputValueTransformer;
1101
+ outputValueTransformer;
1102
+ resultVisitorMap;
1103
+ typeInfo;
1104
+ constructor(inputValueTransformer, outputValueTransformer) {
1105
+ this.inputValueTransformer = inputValueTransformer;
1106
+ this.outputValueTransformer = outputValueTransformer;
1107
+ this.resultVisitorMap = /* @__PURE__ */ Object.create(null);
1108
+ }
1109
+ originalWrappingSchema;
1110
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
1111
+ this.originalWrappingSchema = originalWrappingSchema;
1112
+ const typeMap = originalWrappingSchema.getTypeMap();
1113
+ for (const typeName in typeMap) {
1114
+ const type = typeMap[typeName];
1115
+ if (!typeName.startsWith("__")) {
1116
+ if (isLeafType(type)) {
1117
+ this.resultVisitorMap[typeName] = (value) => this.outputValueTransformer(typeName, value);
1118
+ }
1119
+ }
1120
+ }
1121
+ this.typeInfo = getTypeInfo(originalWrappingSchema);
1122
+ return originalWrappingSchema;
1123
+ }
1124
+ transformRequest(originalRequest, _delegationContext, transformationContext) {
1125
+ const document = originalRequest.document;
1126
+ const variableValues = originalRequest.variables ?? {};
1127
+ const operations = document.definitions.filter(
1128
+ (def) => def.kind === Kind.OPERATION_DEFINITION
1129
+ );
1130
+ const fragments = document.definitions.filter(
1131
+ (def) => def.kind === Kind.FRAGMENT_DEFINITION
1132
+ );
1133
+ const newOperations = this.transformOperations(operations, variableValues);
1134
+ const transformedRequest = {
1135
+ ...originalRequest,
1136
+ document: {
1137
+ ...document,
1138
+ definitions: [...newOperations, ...fragments]
1139
+ },
1140
+ variables: variableValues
1141
+ };
1142
+ transformationContext.transformedRequest = transformedRequest;
1143
+ return transformedRequest;
1144
+ }
1145
+ transformResult(originalResult, _delegationContext, transformationContext) {
1146
+ if (!this.originalWrappingSchema) {
1147
+ throw new Error(
1148
+ `The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
1149
+ );
1150
+ }
1151
+ return visitResult(
1152
+ originalResult,
1153
+ transformationContext.transformedRequest,
1154
+ this.originalWrappingSchema,
1155
+ this.resultVisitorMap
1156
+ );
1157
+ }
1158
+ transformOperations(operations, variableValues) {
1159
+ if (this.typeInfo == null) {
1160
+ throw new Error(
1161
+ `The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
1162
+ );
1163
+ }
1164
+ return operations.map((operation) => {
1165
+ return visit(
1166
+ operation,
1167
+ visitWithTypeInfo(this.typeInfo, {
1168
+ [Kind.FIELD]: (node) => this.transformFieldNode(node, variableValues)
1169
+ })
1170
+ );
1171
+ });
1172
+ }
1173
+ transformFieldNode(field, variableValues) {
1174
+ if (this.typeInfo == null) {
1175
+ throw new Error(
1176
+ `The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
1177
+ );
1178
+ }
1179
+ const targetField = this.typeInfo.getFieldDef();
1180
+ if (!targetField) {
1181
+ return;
1182
+ }
1183
+ if (!targetField.name.startsWith("__")) {
1184
+ const argumentNodes = field.arguments;
1185
+ if (argumentNodes != null) {
1186
+ const argumentNodeMap = argumentNodes.reduce(
1187
+ (prev, argument) => ({
1188
+ ...prev,
1189
+ [argument.name.value]: argument
1190
+ }),
1191
+ /* @__PURE__ */ Object.create(null)
1192
+ );
1193
+ for (const argument of targetField.args) {
1194
+ const argName = argument.name;
1195
+ const argType = argument.type;
1196
+ const argumentNode = argumentNodeMap[argName];
1197
+ let value;
1198
+ const argValue = argumentNode?.value;
1199
+ if (argValue != null) {
1200
+ value = valueFromAST(argValue, argType, variableValues);
1201
+ if (value == null) {
1202
+ value = valueFromASTUntyped(argValue, variableValues);
1203
+ }
1204
+ }
1205
+ const transformedValue = transformInputValue(
1206
+ argType,
1207
+ value,
1208
+ (t, v) => {
1209
+ const newValue = this.inputValueTransformer(t.name, v);
1210
+ return newValue === void 0 ? v : newValue;
1211
+ }
1212
+ );
1213
+ if (argValue?.kind === Kind.VARIABLE) {
1214
+ variableValues[argValue.name.value] = transformedValue;
1215
+ } else {
1216
+ let newValueNode;
1217
+ try {
1218
+ newValueNode = astFromValue(transformedValue, argType);
1219
+ } catch (e) {
1220
+ newValueNode = astFromValueUntyped(transformedValue);
1221
+ }
1222
+ if (newValueNode != null) {
1223
+ argumentNodeMap[argName] = {
1224
+ ...argumentNode,
1225
+ value: newValueNode
1226
+ };
1227
+ }
1228
+ }
1229
+ }
1230
+ return {
1231
+ ...field,
1232
+ arguments: Object.values(argumentNodeMap)
1233
+ };
1234
+ }
1235
+ }
1236
+ return void 0;
1237
+ }
1238
+ }
1239
+
1240
+ class TransformEnumValues {
1241
+ enumValueTransformer;
1242
+ transformer;
1243
+ transformedSchema;
1244
+ mapping;
1245
+ reverseMapping;
1246
+ noTransformation = true;
1247
+ constructor(enumValueTransformer, inputValueTransformer, outputValueTransformer) {
1248
+ this.enumValueTransformer = enumValueTransformer;
1249
+ this.mapping = /* @__PURE__ */ Object.create(null);
1250
+ this.reverseMapping = /* @__PURE__ */ Object.create(null);
1251
+ this.transformer = new MapLeafValues(
1252
+ generateValueTransformer(inputValueTransformer, this.reverseMapping),
1253
+ generateValueTransformer(outputValueTransformer, this.mapping)
1254
+ );
1255
+ }
1256
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1257
+ const mappingSchema = this.transformer.transformSchema(
1258
+ originalWrappingSchema,
1259
+ subschemaConfig
1260
+ );
1261
+ this.transformedSchema = mapSchema(mappingSchema, {
1262
+ [MapperKind.ENUM_VALUE]: (valueConfig, typeName, _schema, externalValue) => this.transformEnumValue(typeName, externalValue, valueConfig),
1263
+ [MapperKind.ARGUMENT]: (argConfig) => {
1264
+ if (argConfig.defaultValue != null) {
1265
+ const newValue = transformInputValue(
1266
+ argConfig.type,
1267
+ argConfig.defaultValue,
1268
+ (type, value) => {
1269
+ return this.mapping[type.name]?.[value] ?? value;
1270
+ }
1271
+ );
1272
+ return {
1273
+ ...argConfig,
1274
+ defaultValue: newValue
1275
+ };
1276
+ }
1277
+ return void 0;
1278
+ }
1279
+ });
1280
+ return this.transformedSchema;
1281
+ }
1282
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1283
+ if (this.noTransformation) {
1284
+ return originalRequest;
1285
+ }
1286
+ return this.transformer.transformRequest(
1287
+ originalRequest,
1288
+ delegationContext,
1289
+ transformationContext
1290
+ );
1291
+ }
1292
+ transformResult(originalResult, delegationContext, transformationContext) {
1293
+ if (this.noTransformation) {
1294
+ return originalResult;
1295
+ }
1296
+ return this.transformer.transformResult(
1297
+ originalResult,
1298
+ delegationContext,
1299
+ transformationContext
1300
+ );
1301
+ }
1302
+ transformEnumValue(typeName, externalValue, enumValueConfig) {
1303
+ const transformedEnumValue = this.enumValueTransformer(
1304
+ typeName,
1305
+ externalValue,
1306
+ enumValueConfig
1307
+ );
1308
+ if (Array.isArray(transformedEnumValue)) {
1309
+ const newExternalValue = transformedEnumValue[0];
1310
+ if (newExternalValue !== externalValue) {
1311
+ if (!this.mapping[typeName]) {
1312
+ this.mapping[typeName] = /* @__PURE__ */ Object.create(null);
1313
+ this.reverseMapping[typeName] = /* @__PURE__ */ Object.create(null);
1314
+ }
1315
+ this.mapping[typeName][externalValue] = newExternalValue;
1316
+ this.reverseMapping[typeName][newExternalValue] = externalValue;
1317
+ this.noTransformation = false;
1318
+ }
1319
+ return [
1320
+ newExternalValue,
1321
+ {
1322
+ ...transformedEnumValue[1],
1323
+ value: void 0
1324
+ }
1325
+ ];
1326
+ }
1327
+ return {
1328
+ ...transformedEnumValue,
1329
+ value: void 0
1330
+ };
1331
+ }
1332
+ }
1333
+ function mapEnumValues(typeName, value, mapping) {
1334
+ const newExternalValue = mapping[typeName]?.[value];
1335
+ return newExternalValue != null ? newExternalValue : value;
1336
+ }
1337
+ function generateValueTransformer(valueTransformer, mapping) {
1338
+ if (valueTransformer == null) {
1339
+ return (typeName, value) => mapEnumValues(typeName, value, mapping);
1340
+ } else {
1341
+ return (typeName, value) => mapEnumValues(typeName, valueTransformer(typeName, value), mapping);
1342
+ }
1343
+ }
1344
+
1345
+ class TransformQuery {
1346
+ path;
1347
+ queryTransformer;
1348
+ resultTransformer;
1349
+ errorPathTransformer;
1350
+ fragments;
1351
+ constructor({
1352
+ path,
1353
+ queryTransformer,
1354
+ resultTransformer = (result) => result,
1355
+ errorPathTransformer = (errorPath) => [...errorPath],
1356
+ fragments = {}
1357
+ }) {
1358
+ this.path = path;
1359
+ const pollutingKeys = this.path.filter(isPrototypePollutingKey);
1360
+ if (pollutingKeys.length > 0) {
1361
+ throw new TypeError(
1362
+ `Invalid path - cannot be a prototype polluting keys: ${pollutingKeys.join(".")}`
1363
+ );
1364
+ }
1365
+ this.queryTransformer = queryTransformer;
1366
+ this.resultTransformer = resultTransformer;
1367
+ this.errorPathTransformer = errorPathTransformer;
1368
+ this.fragments = fragments;
1369
+ }
1370
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1371
+ const pathLength = this.path.length;
1372
+ let index = 0;
1373
+ const operationAst = getOperationASTFromRequest(originalRequest);
1374
+ const document = {
1375
+ kind: Kind.DOCUMENT,
1376
+ definitions: originalRequest.document.definitions.map((def) => {
1377
+ if (def === operationAst) {
1378
+ return visit(def, {
1379
+ [Kind.FIELD]: {
1380
+ enter: (node) => {
1381
+ if (index === pathLength || node.name.value !== this.path[index]) {
1382
+ return false;
1383
+ }
1384
+ index++;
1385
+ if (index === pathLength) {
1386
+ const selectionSet = this.queryTransformer(
1387
+ node.selectionSet,
1388
+ this.fragments,
1389
+ delegationContext,
1390
+ transformationContext
1391
+ );
1392
+ return {
1393
+ ...node,
1394
+ selectionSet
1395
+ };
1396
+ }
1397
+ return void 0;
1398
+ },
1399
+ leave: () => {
1400
+ index--;
1401
+ }
1402
+ }
1403
+ });
1404
+ }
1405
+ return def;
1406
+ })
1407
+ };
1408
+ return {
1409
+ ...originalRequest,
1410
+ document
1411
+ };
1412
+ }
1413
+ transformResult(originalResult, delegationContext, transformationContext) {
1414
+ const data = this.transformData(
1415
+ originalResult.data,
1416
+ delegationContext,
1417
+ transformationContext
1418
+ );
1419
+ const errors = originalResult.errors;
1420
+ return {
1421
+ data,
1422
+ errors: errors != null ? this.transformErrors(errors) : void 0
1423
+ };
1424
+ }
1425
+ transformData(data, delegationContext, transformationContext) {
1426
+ const leafIndex = this.path.length - 1;
1427
+ let index = 0;
1428
+ let newData = data;
1429
+ if (newData) {
1430
+ let next = this.path[index];
1431
+ while (index < leafIndex) {
1432
+ if (data[next]) {
1433
+ newData = newData[next];
1434
+ } else {
1435
+ break;
1436
+ }
1437
+ index++;
1438
+ next = this.path[index];
1439
+ }
1440
+ newData[next] = this.resultTransformer(
1441
+ newData[next],
1442
+ delegationContext,
1443
+ transformationContext
1444
+ );
1445
+ }
1446
+ return data;
1447
+ }
1448
+ transformErrors(errors) {
1449
+ return errors.map((error) => {
1450
+ const path = error.path;
1451
+ if (path == null) {
1452
+ return error;
1453
+ }
1454
+ let match = true;
1455
+ let index = 0;
1456
+ while (index < this.path.length) {
1457
+ if (path[index] !== this.path[index]) {
1458
+ match = false;
1459
+ break;
1460
+ }
1461
+ index++;
1462
+ }
1463
+ const newPath = match ? path.slice(0, index).concat(this.errorPathTransformer(path.slice(index))) : path;
1464
+ return relocatedError(error, newPath);
1465
+ });
1466
+ }
1467
+ }
1468
+
1469
+ class FilterObjectFieldDirectives {
1470
+ filter;
1471
+ constructor(filter) {
1472
+ this.filter = filter;
1473
+ }
1474
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1475
+ const transformer = new TransformObjectFields(
1476
+ (_typeName, _fieldName, fieldConfig) => {
1477
+ const keepDirectives = fieldConfig.astNode?.directives?.filter((dir) => {
1478
+ const directiveDef = originalWrappingSchema.getDirective(
1479
+ dir.name.value
1480
+ );
1481
+ const directiveValue = directiveDef ? getArgumentValues(directiveDef, dir) : void 0;
1482
+ return this.filter(dir.name.value, directiveValue);
1483
+ }) ?? [];
1484
+ if (fieldConfig.astNode?.directives != null && keepDirectives.length !== fieldConfig.astNode.directives.length) {
1485
+ fieldConfig = {
1486
+ ...fieldConfig,
1487
+ astNode: {
1488
+ ...fieldConfig.astNode,
1489
+ directives: keepDirectives
1490
+ }
1491
+ };
1492
+ return fieldConfig;
1493
+ }
1494
+ return void 0;
1495
+ }
1496
+ );
1497
+ return transformer.transformSchema(originalWrappingSchema, subschemaConfig);
1498
+ }
1499
+ }
1500
+
1501
+ class RemoveObjectFieldDirectives {
1502
+ transformer;
1503
+ constructor(directiveName, args = {}) {
1504
+ this.transformer = new FilterObjectFieldDirectives(
1505
+ (dirName, dirValue) => {
1506
+ return !(valueMatchesCriteria(dirName, directiveName) && valueMatchesCriteria(dirValue, args));
1507
+ }
1508
+ );
1509
+ }
1510
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1511
+ return this.transformer.transformSchema(
1512
+ originalWrappingSchema,
1513
+ subschemaConfig
1514
+ );
1515
+ }
1516
+ }
1517
+
1518
+ class RemoveObjectFieldsWithDirective {
1519
+ directiveName;
1520
+ args;
1521
+ constructor(directiveName, args = {}) {
1522
+ this.directiveName = directiveName;
1523
+ this.args = args;
1524
+ }
1525
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1526
+ const transformer = new FilterObjectFields(
1527
+ (_typeName, _fieldName, fieldConfig) => {
1528
+ const directives = getDirectives(originalWrappingSchema, fieldConfig);
1529
+ return !directives.some(
1530
+ (directive) => valueMatchesCriteria(directive.name, this.directiveName) && valueMatchesCriteria(directive.args, this.args)
1531
+ );
1532
+ }
1533
+ );
1534
+ return transformer.transformSchema(originalWrappingSchema, subschemaConfig);
1535
+ }
1536
+ }
1537
+
1538
+ class RemoveObjectFieldDeprecations {
1539
+ removeDirectives;
1540
+ removeDeprecations;
1541
+ constructor(reason) {
1542
+ const args = { reason };
1543
+ this.removeDirectives = new FilterObjectFieldDirectives(
1544
+ (dirName, dirValue) => {
1545
+ return !(dirName === "deprecated" && valueMatchesCriteria(dirValue, args));
1546
+ }
1547
+ );
1548
+ this.removeDeprecations = new TransformObjectFields(
1549
+ (_typeName, _fieldName, fieldConfig) => {
1550
+ if (fieldConfig.deprecationReason && valueMatchesCriteria(fieldConfig.deprecationReason, reason)) {
1551
+ fieldConfig = { ...fieldConfig };
1552
+ delete fieldConfig.deprecationReason;
1553
+ }
1554
+ return fieldConfig;
1555
+ }
1556
+ );
1557
+ }
1558
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1559
+ return this.removeDeprecations.transformSchema(
1560
+ this.removeDirectives.transformSchema(
1561
+ originalWrappingSchema,
1562
+ subschemaConfig
1563
+ ),
1564
+ subschemaConfig
1565
+ );
1566
+ }
1567
+ }
1568
+
1569
+ class RemoveObjectFieldsWithDeprecation {
1570
+ transformer;
1571
+ constructor(reason) {
1572
+ this.transformer = new FilterObjectFields(
1573
+ (_typeName, _fieldName, fieldConfig) => {
1574
+ if (fieldConfig.deprecationReason) {
1575
+ return !valueMatchesCriteria(fieldConfig.deprecationReason, reason);
1576
+ }
1577
+ return true;
1578
+ }
1579
+ );
1580
+ }
1581
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1582
+ return this.transformer.transformSchema(
1583
+ originalWrappingSchema,
1584
+ subschemaConfig
1585
+ );
1586
+ }
1587
+ }
1588
+
1589
+ class PruneTypes {
1590
+ options;
1591
+ constructor(options = {}) {
1592
+ this.options = options;
1593
+ }
1594
+ transformSchema(originalWrappingSchema, _subschemaConfig) {
1595
+ return pruneSchema(originalWrappingSchema, this.options);
1596
+ }
1597
+ }
1598
+
1599
+ class MapFields {
1600
+ fieldNodeTransformerMap;
1601
+ objectValueTransformerMap;
1602
+ errorsTransformer;
1603
+ transformer;
1604
+ constructor(fieldNodeTransformerMap, objectValueTransformerMap, errorsTransformer) {
1605
+ this.fieldNodeTransformerMap = fieldNodeTransformerMap;
1606
+ this.objectValueTransformerMap = objectValueTransformerMap;
1607
+ this.errorsTransformer = errorsTransformer;
1608
+ }
1609
+ _getTransformer() {
1610
+ const transformer = this.transformer;
1611
+ if (transformer === void 0) {
1612
+ throw new Error(
1613
+ `The MapFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
1614
+ );
1615
+ }
1616
+ return transformer;
1617
+ }
1618
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1619
+ const subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name;
1620
+ const objectValueTransformerMap = this.objectValueTransformerMap;
1621
+ this.transformer = new TransformCompositeFields(
1622
+ () => void 0,
1623
+ (typeName, fieldName, fieldNode, fragments, transformationContext) => {
1624
+ const typeTransformers = this.fieldNodeTransformerMap[typeName];
1625
+ if (typeTransformers == null) {
1626
+ return void 0;
1627
+ }
1628
+ const fieldNodeTransformer = typeTransformers[fieldName];
1629
+ if (fieldNodeTransformer == null) {
1630
+ return void 0;
1631
+ }
1632
+ return fieldNodeTransformer(
1633
+ fieldNode,
1634
+ fragments,
1635
+ transformationContext
1636
+ );
1637
+ },
1638
+ objectValueTransformerMap != null ? (data, transformationContext) => {
1639
+ if (data == null) {
1640
+ return data;
1641
+ }
1642
+ let typeName = data.__typename;
1643
+ if (typeName == null) {
1644
+ typeName = subscriptionTypeName;
1645
+ if (typeName == null) {
1646
+ return data;
1647
+ }
1648
+ }
1649
+ const transformer = objectValueTransformerMap[typeName];
1650
+ if (transformer == null) {
1651
+ return data;
1652
+ }
1653
+ return transformer(data, transformationContext);
1654
+ } : void 0,
1655
+ this.errorsTransformer != null ? this.errorsTransformer : void 0
1656
+ );
1657
+ return this.transformer.transformSchema(
1658
+ originalWrappingSchema,
1659
+ subschemaConfig
1660
+ );
1661
+ }
1662
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1663
+ return this._getTransformer().transformRequest(
1664
+ originalRequest,
1665
+ delegationContext,
1666
+ transformationContext
1667
+ );
1668
+ }
1669
+ transformResult(originalResult, delegationContext, transformationContext) {
1670
+ return this._getTransformer().transformResult(
1671
+ originalResult,
1672
+ delegationContext,
1673
+ transformationContext
1674
+ );
1675
+ }
1676
+ }
1677
+
1678
+ class WrapFields {
1679
+ outerTypeName;
1680
+ wrappingFieldNames;
1681
+ wrappingTypeNames;
1682
+ numWraps;
1683
+ fieldNames;
1684
+ transformer;
1685
+ config;
1686
+ constructor(outerTypeName, wrappingFieldNames, wrappingTypeNames, fieldNames, prefix = "gqtld", config = { isNullable: false }) {
1687
+ this.outerTypeName = outerTypeName;
1688
+ this.wrappingFieldNames = wrappingFieldNames;
1689
+ this.wrappingTypeNames = wrappingTypeNames;
1690
+ this.numWraps = wrappingFieldNames.length;
1691
+ this.fieldNames = fieldNames;
1692
+ this.config = config;
1693
+ const remainingWrappingFieldNames = this.wrappingFieldNames.slice();
1694
+ const outerMostWrappingFieldName = remainingWrappingFieldNames.shift();
1695
+ if (outerMostWrappingFieldName == null) {
1696
+ throw new Error(`Cannot wrap fields, no wrapping field name provided.`);
1697
+ }
1698
+ this.transformer = new MapFields(
1699
+ {
1700
+ [outerTypeName]: {
1701
+ [outerMostWrappingFieldName]: (fieldNode, fragments, transformationContext) => hoistFieldNodes({
1702
+ fieldNode,
1703
+ path: remainingWrappingFieldNames,
1704
+ fieldNames,
1705
+ fragments,
1706
+ transformationContext,
1707
+ prefix
1708
+ })
1709
+ }
1710
+ },
1711
+ {
1712
+ [outerTypeName]: (value, context) => dehoistValue(value, context)
1713
+ },
1714
+ (errors, context) => dehoistErrors(errors, context)
1715
+ );
1716
+ }
1717
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1718
+ const fieldNames = this.fieldNames;
1719
+ const targetFieldConfigMap = selectObjectFields(
1720
+ originalWrappingSchema,
1721
+ this.outerTypeName,
1722
+ !fieldNames ? () => true : (fieldName) => fieldNames.includes(fieldName)
1723
+ );
1724
+ const newTargetFieldConfigMap = /* @__PURE__ */ Object.create(null);
1725
+ for (const fieldName in targetFieldConfigMap) {
1726
+ const field = targetFieldConfigMap[fieldName];
1727
+ const newField = {
1728
+ ...field,
1729
+ resolve: defaultMergedResolver
1730
+ };
1731
+ newTargetFieldConfigMap[fieldName] = newField;
1732
+ }
1733
+ let wrapIndex = this.numWraps - 1;
1734
+ let wrappingTypeName = this.wrappingTypeNames[wrapIndex];
1735
+ let wrappingFieldName = this.wrappingFieldNames[wrapIndex];
1736
+ let newSchema = appendObjectFields(
1737
+ originalWrappingSchema,
1738
+ wrappingTypeName,
1739
+ newTargetFieldConfigMap
1740
+ );
1741
+ for (wrapIndex--; wrapIndex > -1; wrapIndex--) {
1742
+ const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex];
1743
+ newSchema = appendObjectFields(newSchema, nextWrappingTypeName, {
1744
+ [wrappingFieldName]: {
1745
+ type: new GraphQLNonNull(
1746
+ newSchema.getType(wrappingTypeName)
1747
+ ),
1748
+ resolve: defaultMergedResolver
1749
+ }
1750
+ });
1751
+ wrappingTypeName = nextWrappingTypeName;
1752
+ wrappingFieldName = this.wrappingFieldNames[wrapIndex];
1753
+ }
1754
+ const targetSchema = subschemaConfig.schema;
1755
+ let wrappingOperation;
1756
+ switch (this.outerTypeName) {
1757
+ case targetSchema.getQueryType()?.name:
1758
+ wrappingOperation = "query";
1759
+ break;
1760
+ case targetSchema.getMutationType()?.name:
1761
+ wrappingOperation = "mutation";
1762
+ break;
1763
+ case targetSchema.getSubscriptionType()?.name:
1764
+ wrappingOperation = "subscription";
1765
+ break;
1766
+ }
1767
+ let resolve;
1768
+ if (wrappingOperation) {
1769
+ const createProxyingResolver = subschemaConfig.createProxyingResolver ?? defaultCreateProxyingResolver;
1770
+ resolve = createProxyingResolver({
1771
+ subschemaConfig,
1772
+ operation: wrappingOperation,
1773
+ fieldName: wrappingFieldName
1774
+ });
1775
+ } else {
1776
+ resolve = defaultMergedResolver;
1777
+ }
1778
+ const baseType = newSchema.getType(wrappingTypeName);
1779
+ const wrappingType = this.config.isNullable ? baseType : new GraphQLNonNull(baseType);
1780
+ const newFieldConfig = wrappingOperation === "subscription" ? {
1781
+ type: wrappingType,
1782
+ subscribe: resolve,
1783
+ resolve: (payload) => payload
1784
+ } : {
1785
+ type: wrappingType,
1786
+ resolve
1787
+ };
1788
+ [newSchema] = modifyObjectFields(
1789
+ newSchema,
1790
+ this.outerTypeName,
1791
+ (fieldName) => !!newTargetFieldConfigMap[fieldName],
1792
+ {
1793
+ [wrappingFieldName]: newFieldConfig
1794
+ }
1795
+ );
1796
+ return this.transformer.transformSchema(newSchema, subschemaConfig);
1797
+ }
1798
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1799
+ transformationContext.nextIndex = 0;
1800
+ transformationContext.paths = /* @__PURE__ */ Object.create(null);
1801
+ return this.transformer.transformRequest(
1802
+ originalRequest,
1803
+ delegationContext,
1804
+ transformationContext
1805
+ );
1806
+ }
1807
+ transformResult(originalResult, delegationContext, transformationContext) {
1808
+ return this.transformer.transformResult(
1809
+ originalResult,
1810
+ delegationContext,
1811
+ transformationContext
1812
+ );
1813
+ }
1814
+ }
1815
+ function collectFields(selectionSet, fragments, fields = [], visitedFragmentNames = {}) {
1816
+ if (selectionSet != null) {
1817
+ for (const selection of selectionSet.selections) {
1818
+ switch (selection.kind) {
1819
+ case Kind.FIELD:
1820
+ fields.push(selection);
1821
+ break;
1822
+ case Kind.INLINE_FRAGMENT:
1823
+ collectFields(
1824
+ selection.selectionSet,
1825
+ fragments,
1826
+ fields,
1827
+ visitedFragmentNames
1828
+ );
1829
+ break;
1830
+ case Kind.FRAGMENT_SPREAD: {
1831
+ const fragmentName = selection.name.value;
1832
+ if (!visitedFragmentNames[fragmentName]) {
1833
+ visitedFragmentNames[fragmentName] = true;
1834
+ collectFields(
1835
+ fragments[fragmentName].selectionSet,
1836
+ fragments,
1837
+ fields,
1838
+ visitedFragmentNames
1839
+ );
1840
+ }
1841
+ break;
1842
+ }
1843
+ }
1844
+ }
1845
+ }
1846
+ return fields;
1847
+ }
1848
+ function aliasFieldNode(fieldNode, str) {
1849
+ return {
1850
+ ...fieldNode,
1851
+ alias: {
1852
+ kind: Kind.NAME,
1853
+ value: str
1854
+ }
1855
+ };
1856
+ }
1857
+ function hoistFieldNodes({
1858
+ fieldNode,
1859
+ fieldNames,
1860
+ path,
1861
+ fragments,
1862
+ transformationContext,
1863
+ prefix,
1864
+ index = 0,
1865
+ wrappingPath = []
1866
+ }) {
1867
+ const alias = fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value;
1868
+ let newFieldNodes = [];
1869
+ if (index < path.length) {
1870
+ const pathSegment = path[index];
1871
+ for (const possibleFieldNode of collectFields(
1872
+ fieldNode.selectionSet,
1873
+ fragments
1874
+ )) {
1875
+ if (possibleFieldNode.name.value === pathSegment) {
1876
+ const newWrappingPath = wrappingPath.concat([alias]);
1877
+ newFieldNodes = newFieldNodes.concat(
1878
+ hoistFieldNodes({
1879
+ fieldNode: possibleFieldNode,
1880
+ fieldNames,
1881
+ path,
1882
+ fragments,
1883
+ transformationContext,
1884
+ prefix,
1885
+ index: index + 1,
1886
+ wrappingPath: newWrappingPath
1887
+ })
1888
+ );
1889
+ }
1890
+ }
1891
+ } else {
1892
+ for (const possibleFieldNode of collectFields(
1893
+ fieldNode.selectionSet,
1894
+ fragments
1895
+ )) {
1896
+ if (!fieldNames || fieldNames.includes(possibleFieldNode.name.value)) {
1897
+ const nextIndex = transformationContext.nextIndex;
1898
+ transformationContext.nextIndex++;
1899
+ const indexingAlias = `__${prefix}${nextIndex}__`;
1900
+ transformationContext.paths[indexingAlias] = {
1901
+ pathToField: wrappingPath.concat([alias]),
1902
+ alias: possibleFieldNode.alias != null ? possibleFieldNode.alias.value : possibleFieldNode.name.value
1903
+ };
1904
+ newFieldNodes.push(aliasFieldNode(possibleFieldNode, indexingAlias));
1905
+ }
1906
+ }
1907
+ }
1908
+ return newFieldNodes;
1909
+ }
1910
+ function dehoistValue(originalValue, context) {
1911
+ if (originalValue == null) {
1912
+ return originalValue;
1913
+ }
1914
+ const newValue = /* @__PURE__ */ Object.create(null);
1915
+ for (const alias in originalValue) {
1916
+ let obj = newValue;
1917
+ const path = context.paths[alias];
1918
+ if (path == null) {
1919
+ newValue[alias] = originalValue[alias];
1920
+ continue;
1921
+ }
1922
+ const pathToField = path.pathToField;
1923
+ const fieldAlias = path.alias;
1924
+ for (const key of pathToField) {
1925
+ obj = obj[key] = obj[key] || /* @__PURE__ */ Object.create(null);
1926
+ }
1927
+ obj[fieldAlias] = originalValue[alias];
1928
+ }
1929
+ return newValue;
1930
+ }
1931
+ function dehoistErrors(errors, context) {
1932
+ if (errors === void 0) {
1933
+ return void 0;
1934
+ }
1935
+ return errors.map((error) => {
1936
+ const originalPath = error.path;
1937
+ if (originalPath == null) {
1938
+ return error;
1939
+ }
1940
+ let newPath = [];
1941
+ for (const pathSegment of originalPath) {
1942
+ if (typeof pathSegment !== "string") {
1943
+ newPath.push(pathSegment);
1944
+ continue;
1945
+ }
1946
+ const path = context.paths[pathSegment];
1947
+ if (path == null) {
1948
+ newPath.push(pathSegment);
1949
+ continue;
1950
+ }
1951
+ newPath = newPath.concat(path.pathToField, [path.alias]);
1952
+ }
1953
+ return relocatedError(error, newPath);
1954
+ });
1955
+ }
1956
+
1957
+ class WrapType {
1958
+ transformer;
1959
+ constructor(outerTypeName, innerTypeName, fieldName) {
1960
+ this.transformer = new WrapFields(
1961
+ outerTypeName,
1962
+ [fieldName],
1963
+ [innerTypeName]
1964
+ );
1965
+ }
1966
+ transformSchema(originalWrappingSchema, subschemaConfig) {
1967
+ return this.transformer.transformSchema(
1968
+ originalWrappingSchema,
1969
+ subschemaConfig
1970
+ );
1971
+ }
1972
+ transformRequest(originalRequest, delegationContext, transformationContext) {
1973
+ return this.transformer.transformRequest(
1974
+ originalRequest,
1975
+ delegationContext,
1976
+ transformationContext
1977
+ );
1978
+ }
1979
+ transformResult(originalResult, delegationContext, transformationContext) {
1980
+ return this.transformer.transformResult(
1981
+ originalResult,
1982
+ delegationContext,
1983
+ transformationContext
1984
+ );
1985
+ }
1986
+ }
1987
+
1988
+ class HoistField {
1989
+ typeName;
1990
+ newFieldName;
1991
+ pathToField;
1992
+ oldFieldName;
1993
+ argFilters;
1994
+ argLevels;
1995
+ transformer;
1996
+ constructor(typeName, pathConfig, newFieldName, alias = "__gqtlw__") {
1997
+ this.typeName = typeName;
1998
+ this.newFieldName = newFieldName;
1999
+ const path = pathConfig.map(
2000
+ (segment) => typeof segment === "string" ? segment : segment.fieldName
2001
+ );
2002
+ this.argFilters = pathConfig.map((segment, index) => {
2003
+ if (typeof segment === "string" || segment.argFilter == null) {
2004
+ return index === pathConfig.length - 1 ? () => true : () => false;
2005
+ }
2006
+ return segment.argFilter;
2007
+ });
2008
+ const pathToField = path.slice();
2009
+ const oldFieldName = pathToField.pop();
2010
+ if (oldFieldName == null) {
2011
+ throw new Error(
2012
+ `Cannot hoist field to ${newFieldName} on type ${typeName}, no path provided.`
2013
+ );
2014
+ }
2015
+ this.oldFieldName = oldFieldName;
2016
+ this.pathToField = pathToField;
2017
+ const argLevels = /* @__PURE__ */ Object.create(null);
2018
+ this.transformer = new MapFields(
2019
+ {
2020
+ [typeName]: {
2021
+ [newFieldName]: (fieldNode) => wrapFieldNode(
2022
+ renameFieldNode(fieldNode, oldFieldName),
2023
+ pathToField,
2024
+ alias,
2025
+ argLevels
2026
+ )
2027
+ }
2028
+ },
2029
+ {
2030
+ [typeName]: (value) => unwrapValue(value, alias)
2031
+ },
2032
+ (errors) => errors != null ? unwrapErrors(errors, alias) : void 0
2033
+ );
2034
+ this.argLevels = argLevels;
2035
+ }
2036
+ transformSchema(originalWrappingSchema, subschemaConfig) {
2037
+ const argsMap = /* @__PURE__ */ Object.create(null);
2038
+ let isList = false;
2039
+ const innerType = this.pathToField.reduce(
2040
+ (acc, pathSegment, index) => {
2041
+ const field = acc.getFields()[pathSegment];
2042
+ for (const arg of field.args) {
2043
+ if (this.argFilters[index](arg)) {
2044
+ argsMap[arg.name] = arg;
2045
+ this.argLevels[arg.name] = index;
2046
+ }
2047
+ }
2048
+ const nullableType = getNullableType(field?.type);
2049
+ if (isListType(nullableType)) {
2050
+ isList = true;
2051
+ return getNamedType(nullableType);
2052
+ }
2053
+ return nullableType;
2054
+ },
2055
+ originalWrappingSchema.getType(this.typeName)
2056
+ );
2057
+ let [newSchema, targetFieldConfigMap] = removeObjectFields(
2058
+ originalWrappingSchema,
2059
+ innerType.name,
2060
+ (fieldName) => fieldName === this.oldFieldName
2061
+ );
2062
+ const targetField = targetFieldConfigMap[this.oldFieldName];
2063
+ let resolve;
2064
+ const hoistingToRootField = this.typeName === originalWrappingSchema.getQueryType()?.name || this.typeName === originalWrappingSchema.getMutationType()?.name;
2065
+ if (hoistingToRootField) {
2066
+ const targetSchema = subschemaConfig.schema;
2067
+ const operation = this.typeName === targetSchema.getQueryType()?.name ? "query" : "mutation";
2068
+ const createProxyingResolver = subschemaConfig.createProxyingResolver ?? defaultCreateProxyingResolver;
2069
+ resolve = createProxyingResolver({
2070
+ subschemaConfig,
2071
+ operation,
2072
+ fieldName: this.newFieldName
2073
+ });
2074
+ } else {
2075
+ resolve = defaultMergedResolver;
2076
+ }
2077
+ const newTargetField = {
2078
+ ...targetField,
2079
+ resolve
2080
+ };
2081
+ const level = this.pathToField.length;
2082
+ const args = targetField?.args;
2083
+ if (args != null) {
2084
+ for (const argName in args) {
2085
+ const argConfig = args[argName];
2086
+ if (argConfig == null) {
2087
+ continue;
2088
+ }
2089
+ const arg = {
2090
+ ...argConfig,
2091
+ name: argName,
2092
+ description: argConfig.description,
2093
+ defaultValue: argConfig.defaultValue,
2094
+ extensions: argConfig.extensions,
2095
+ astNode: argConfig.astNode
2096
+ };
2097
+ if (this.argFilters[level]?.(arg)) {
2098
+ argsMap[argName] = arg;
2099
+ this.argLevels[arg.name] = level;
2100
+ }
2101
+ }
2102
+ }
2103
+ newTargetField.args = argsMap;
2104
+ if (isList) {
2105
+ newTargetField.type = new GraphQLList(newTargetField.type);
2106
+ const resolver = newTargetField.resolve;
2107
+ newTargetField.resolve = (parent, args2, context, info) => Promise.all(
2108
+ Object.keys(parent).filter((key) => !isNaN(parseInt(key, 10))).map((key) => resolver(parent[key], args2, context, info))
2109
+ );
2110
+ }
2111
+ newSchema = appendObjectFields(newSchema, this.typeName, {
2112
+ [this.newFieldName]: newTargetField
2113
+ });
2114
+ return this.transformer.transformSchema(newSchema, subschemaConfig);
2115
+ }
2116
+ transformRequest(originalRequest, delegationContext, transformationContext) {
2117
+ return this.transformer.transformRequest(
2118
+ originalRequest,
2119
+ delegationContext,
2120
+ transformationContext
2121
+ );
2122
+ }
2123
+ transformResult(originalResult, delegationContext, transformationContext) {
2124
+ return this.transformer.transformResult(
2125
+ originalResult,
2126
+ delegationContext,
2127
+ transformationContext
2128
+ );
2129
+ }
2130
+ }
2131
+ function wrapFieldNode(fieldNode, path, alias, argLevels) {
2132
+ return path.reduceRight(
2133
+ (acc, fieldName, index) => ({
2134
+ kind: Kind.FIELD,
2135
+ alias: {
2136
+ kind: Kind.NAME,
2137
+ value: alias
2138
+ },
2139
+ name: {
2140
+ kind: Kind.NAME,
2141
+ value: fieldName
2142
+ },
2143
+ selectionSet: {
2144
+ kind: Kind.SELECTION_SET,
2145
+ selections: [acc]
2146
+ },
2147
+ arguments: fieldNode.arguments != null ? fieldNode.arguments.filter(
2148
+ (arg) => argLevels[arg.name.value] === index
2149
+ ) : void 0
2150
+ }),
2151
+ {
2152
+ ...fieldNode,
2153
+ arguments: fieldNode.arguments != null ? fieldNode.arguments.filter(
2154
+ (arg) => argLevels[arg.name.value] === path.length
2155
+ ) : void 0
2156
+ }
2157
+ );
2158
+ }
2159
+ function renameFieldNode(fieldNode, name) {
2160
+ return {
2161
+ ...fieldNode,
2162
+ alias: {
2163
+ kind: Kind.NAME,
2164
+ value: fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value
2165
+ },
2166
+ name: {
2167
+ kind: Kind.NAME,
2168
+ value: name
2169
+ }
2170
+ };
2171
+ }
2172
+ function unwrapValue(originalValue, alias) {
2173
+ let newValue = originalValue;
2174
+ let object = newValue[alias];
2175
+ while (object != null) {
2176
+ newValue = object;
2177
+ object = newValue[alias];
2178
+ }
2179
+ delete originalValue[alias];
2180
+ Object.assign(originalValue, newValue);
2181
+ return originalValue;
2182
+ }
2183
+ function unwrapErrors(errors, alias) {
2184
+ return errors.map((error) => {
2185
+ const originalPath = error.path;
2186
+ if (originalPath == null) {
2187
+ return error;
2188
+ }
2189
+ const newPath = originalPath.filter((pathSegment) => pathSegment !== alias);
2190
+ return relocatedError(error, newPath);
2191
+ });
2192
+ }
2193
+
2194
+ class WrapQuery {
2195
+ constructor(path, wrapper, extractor) {
2196
+ this.path = path;
2197
+ this.wrapper = wrapper;
2198
+ this.extractor = extractor;
2199
+ const pollutingKeys = this.path.filter(isPrototypePollutingKey);
2200
+ if (pollutingKeys.length > 0) {
2201
+ throw new TypeError(
2202
+ `Invalid path - cannot be a prototype polluting keys: ${pollutingKeys.join(".")}`
2203
+ );
2204
+ }
2205
+ }
2206
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
2207
+ const fieldPath = [];
2208
+ const ourPath = JSON.stringify(this.path);
2209
+ const document = visit(originalRequest.document, {
2210
+ [Kind.FIELD]: {
2211
+ enter: (node) => {
2212
+ fieldPath.push(node.name.value);
2213
+ if (node.selectionSet != null && ourPath === JSON.stringify(fieldPath)) {
2214
+ const wrapResult = this.wrapper(node.selectionSet);
2215
+ const selectionSet = wrapResult != null && wrapResult.kind === Kind.SELECTION_SET ? wrapResult : {
2216
+ kind: Kind.SELECTION_SET,
2217
+ selections: [wrapResult]
2218
+ };
2219
+ return {
2220
+ ...node,
2221
+ selectionSet
2222
+ };
2223
+ }
2224
+ return void 0;
2225
+ },
2226
+ leave: () => {
2227
+ fieldPath.pop();
2228
+ }
2229
+ }
2230
+ });
2231
+ return {
2232
+ ...originalRequest,
2233
+ document
2234
+ };
2235
+ }
2236
+ transformResult(originalResult, _delegationContext, _transformationContext) {
2237
+ const rootData = originalResult.data;
2238
+ if (rootData != null) {
2239
+ let data = rootData;
2240
+ const path = [...this.path];
2241
+ while (path.length > 1) {
2242
+ const next = path.shift();
2243
+ if (data[next]) {
2244
+ data = data[next];
2245
+ }
2246
+ }
2247
+ const lastKey = path[0];
2248
+ data[lastKey] = this.extractor(data[lastKey]);
2249
+ }
2250
+ return {
2251
+ data: rootData,
2252
+ errors: originalResult.errors
2253
+ };
2254
+ }
2255
+ }
2256
+
2257
+ class ExtractField {
2258
+ from;
2259
+ to;
2260
+ constructor({ from, to }) {
2261
+ this.from = from;
2262
+ this.to = to;
2263
+ }
2264
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
2265
+ let fromSelection;
2266
+ const ourPathFrom = JSON.stringify(this.from);
2267
+ const ourPathTo = JSON.stringify(this.to);
2268
+ let fieldPath = [];
2269
+ visit(originalRequest.document, {
2270
+ [Kind.FIELD]: {
2271
+ enter: (node) => {
2272
+ fieldPath.push(node.name.value);
2273
+ if (ourPathFrom === JSON.stringify(fieldPath)) {
2274
+ fromSelection = node.selectionSet;
2275
+ return BREAK;
2276
+ }
2277
+ return void 0;
2278
+ },
2279
+ leave: () => {
2280
+ fieldPath.pop();
2281
+ }
2282
+ }
2283
+ });
2284
+ fieldPath = [];
2285
+ const document = visit(originalRequest.document, {
2286
+ [Kind.FIELD]: {
2287
+ enter: (node) => {
2288
+ fieldPath.push(node.name.value);
2289
+ if (ourPathTo === JSON.stringify(fieldPath) && fromSelection != null) {
2290
+ return {
2291
+ ...node,
2292
+ selectionSet: fromSelection
2293
+ };
2294
+ }
2295
+ return void 0;
2296
+ },
2297
+ leave: () => {
2298
+ fieldPath.pop();
2299
+ }
2300
+ }
2301
+ });
2302
+ return {
2303
+ ...originalRequest,
2304
+ document
2305
+ };
2306
+ }
2307
+ }
2308
+
2309
+ function getSchemaFromIntrospection(introspectionResult, options) {
2310
+ if (introspectionResult?.data?.__schema) {
2311
+ return buildClientSchema(introspectionResult.data, options);
2312
+ }
2313
+ if (introspectionResult?.errors) {
2314
+ const graphqlErrors = introspectionResult.errors.map(
2315
+ (error) => createGraphQLError(error.message, error)
2316
+ );
2317
+ if (introspectionResult.errors.length === 1) {
2318
+ throw graphqlErrors[0];
2319
+ } else {
2320
+ throw new AggregateError(
2321
+ graphqlErrors,
2322
+ "Could not obtain introspection result"
2323
+ );
2324
+ }
2325
+ }
2326
+ throw createGraphQLError(
2327
+ `Could not obtain introspection result, received the following as response;
2328
+ ${inspect(
2329
+ introspectionResult
2330
+ )}`
2331
+ );
2332
+ }
2333
+ function schemaFromExecutor(executor, context, options) {
2334
+ const parsedIntrospectionQuery = parse(
2335
+ getIntrospectionQuery(options),
2336
+ options
2337
+ );
2338
+ return handleMaybePromise(
2339
+ () => handleMaybePromise(
2340
+ () => executor({
2341
+ document: parsedIntrospectionQuery,
2342
+ context
2343
+ }),
2344
+ (introspection) => {
2345
+ if (isAsyncIterable(introspection)) {
2346
+ const iterator = introspection[Symbol.asyncIterator]();
2347
+ return iterator.next().then(({ value }) => value);
2348
+ }
2349
+ return introspection;
2350
+ }
2351
+ ),
2352
+ (introspection) => getSchemaFromIntrospection(introspection, options)
2353
+ );
2354
+ }
2355
+
2356
+ export { ExtractField, FilterInputObjectFields, FilterInterfaceFields, FilterObjectFieldDirectives, FilterObjectFields, FilterRootFields, FilterTypes, HoistField, MapFields, MapLeafValues, PruneTypes as PruneSchema, RemoveObjectFieldDeprecations, RemoveObjectFieldDirectives, RemoveObjectFieldsWithDeprecation, RemoveObjectFieldsWithDirective, RenameInputObjectFields, RenameInterfaceFields, RenameObjectFieldArguments, RenameObjectFields, RenameRootFields, RenameRootTypes, RenameTypes, TransformCompositeFields, TransformEnumValues, TransformInputObjectFields, TransformInterfaceFields, TransformObjectFields, TransformQuery, TransformRootFields, WrapFields, WrapQuery, WrapType, defaultCreateProxyingResolver, generateProxyingResolvers, schemaFromExecutor, wrapSchema };