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