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