1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | const utils = require('@graphql-tools/utils/es5');
|
8 | const tslib = require('tslib');
|
9 | const graphql = require('graphql');
|
10 | const AggregateError = _interopDefault(require('@ardatan/aggregate-error'));
|
11 | const valueOrPromise = require('value-or-promise');
|
12 | const batchExecute = require('@graphql-tools/batch-execute/es5');
|
13 |
|
14 | function applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
15 | var schemaTransforms = subschemaConfig.transforms;
|
16 | if (schemaTransforms == null) {
|
17 | return originalWrappingSchema;
|
18 | }
|
19 | return schemaTransforms.reduce(function (schema, transform) {
|
20 | return transform.transformSchema != null
|
21 | ? transform.transformSchema(utils.cloneSchema(schema), subschemaConfig, transformedSchema)
|
22 | : schema;
|
23 | }, originalWrappingSchema);
|
24 | }
|
25 |
|
26 | function isSubschema(value) {
|
27 | return Boolean(value.transformedSchema);
|
28 | }
|
29 | var Subschema = (function () {
|
30 | function Subschema(config) {
|
31 | var _a;
|
32 | this.schema = config.schema;
|
33 | this.rootValue = config.rootValue;
|
34 | this.executor = config.executor;
|
35 | this.subscriber = config.subscriber;
|
36 | this.batch = config.batch;
|
37 | this.batchingOptions = config.batchingOptions;
|
38 | this.createProxyingResolver = config.createProxyingResolver;
|
39 | this.transforms = (_a = config.transforms) !== null && _a !== void 0 ? _a : [];
|
40 | this.transformedSchema = applySchemaTransforms(this.schema, config);
|
41 | this.merge = config.merge;
|
42 | }
|
43 | return Subschema;
|
44 | }());
|
45 |
|
46 | function getDelegatingOperation(parentType, schema) {
|
47 | if (parentType === schema.getMutationType()) {
|
48 | return 'mutation';
|
49 | }
|
50 | else if (parentType === schema.getSubscriptionType()) {
|
51 | return 'subscription';
|
52 | }
|
53 | return 'query';
|
54 | }
|
55 | function createRequestFromInfo(_a) {
|
56 | var info = _a.info, operationName = _a.operationName, _b = _a.operation, operation = _b === void 0 ? getDelegatingOperation(info.parentType, info.schema) : _b, _c = _a.fieldName, fieldName = _c === void 0 ? info.fieldName : _c, selectionSet = _a.selectionSet, _d = _a.fieldNodes, fieldNodes = _d === void 0 ? info.fieldNodes : _d;
|
57 | return createRequest({
|
58 | sourceSchema: info.schema,
|
59 | sourceParentType: info.parentType,
|
60 | sourceFieldName: info.fieldName,
|
61 | fragments: info.fragments,
|
62 | variableDefinitions: info.operation.variableDefinitions,
|
63 | variableValues: info.variableValues,
|
64 | targetOperationName: operationName,
|
65 | targetOperation: operation,
|
66 | targetFieldName: fieldName,
|
67 | selectionSet: selectionSet,
|
68 | fieldNodes: fieldNodes,
|
69 | });
|
70 | }
|
71 | function createRequest(_a) {
|
72 | var _b;
|
73 | var sourceSchema = _a.sourceSchema, sourceParentType = _a.sourceParentType, sourceFieldName = _a.sourceFieldName, fragments = _a.fragments, variableDefinitions = _a.variableDefinitions, variableValues = _a.variableValues, targetOperationName = _a.targetOperationName, targetOperation = _a.targetOperation, targetFieldName = _a.targetFieldName, selectionSet = _a.selectionSet, fieldNodes = _a.fieldNodes;
|
74 | var newSelectionSet;
|
75 | var argumentNodeMap;
|
76 | if (selectionSet != null) {
|
77 | newSelectionSet = selectionSet;
|
78 | argumentNodeMap = Object.create(null);
|
79 | }
|
80 | else {
|
81 | var selections = fieldNodes.reduce(function (acc, fieldNode) { return (fieldNode.selectionSet != null ? acc.concat(fieldNode.selectionSet.selections) : acc); }, []);
|
82 | newSelectionSet = selections.length
|
83 | ? {
|
84 | kind: graphql.Kind.SELECTION_SET,
|
85 | selections: selections,
|
86 | }
|
87 | : undefined;
|
88 | argumentNodeMap = {};
|
89 | var args = (_b = fieldNodes[0]) === null || _b === void 0 ? void 0 : _b.arguments;
|
90 | if (args) {
|
91 | argumentNodeMap = args.reduce(function (prev, curr) {
|
92 | var _a;
|
93 | return (tslib.__assign(tslib.__assign({}, prev), (_a = {}, _a[curr.name.value] = curr, _a)));
|
94 | }, argumentNodeMap);
|
95 | }
|
96 | }
|
97 | var newVariables = Object.create(null);
|
98 | var variableDefinitionMap = Object.create(null);
|
99 | if (sourceSchema != null && variableDefinitions != null) {
|
100 | variableDefinitions.forEach(function (def) {
|
101 | var varName = def.variable.name.value;
|
102 | variableDefinitionMap[varName] = def;
|
103 | var varType = graphql.typeFromAST(sourceSchema, def.type);
|
104 | var serializedValue = utils.serializeInputValue(varType, variableValues[varName]);
|
105 | if (serializedValue !== undefined) {
|
106 | newVariables[varName] = serializedValue;
|
107 | }
|
108 | });
|
109 | }
|
110 | if (sourceParentType != null) {
|
111 | updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, newVariables);
|
112 | }
|
113 | var rootfieldNode = {
|
114 | kind: graphql.Kind.FIELD,
|
115 | arguments: Object.keys(argumentNodeMap).map(function (argName) { return argumentNodeMap[argName]; }),
|
116 | name: {
|
117 | kind: graphql.Kind.NAME,
|
118 | value: targetFieldName || fieldNodes[0].name.value,
|
119 | },
|
120 | selectionSet: newSelectionSet,
|
121 | };
|
122 | var operationName = targetOperationName
|
123 | ? {
|
124 | kind: graphql.Kind.NAME,
|
125 | value: targetOperationName,
|
126 | }
|
127 | : undefined;
|
128 | var operationDefinition = {
|
129 | kind: graphql.Kind.OPERATION_DEFINITION,
|
130 | name: operationName,
|
131 | operation: targetOperation,
|
132 | variableDefinitions: Object.keys(variableDefinitionMap).map(function (varName) { return variableDefinitionMap[varName]; }),
|
133 | selectionSet: {
|
134 | kind: graphql.Kind.SELECTION_SET,
|
135 | selections: [rootfieldNode],
|
136 | },
|
137 | };
|
138 | var definitions = [operationDefinition];
|
139 | if (fragments != null) {
|
140 | definitions = definitions.concat(Object.keys(fragments).map(function (fragmentName) { return fragments[fragmentName]; }));
|
141 | }
|
142 | var document = {
|
143 | kind: graphql.Kind.DOCUMENT,
|
144 | definitions: definitions,
|
145 | };
|
146 | return {
|
147 | document: document,
|
148 | variables: newVariables,
|
149 | };
|
150 | }
|
151 | function updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, variableValues) {
|
152 | var sourceField = sourceParentType.getFields()[sourceFieldName];
|
153 | sourceField.args.forEach(function (argument) {
|
154 | var argName = argument.name;
|
155 | var sourceArgType = argument.type;
|
156 | if (argumentNodeMap[argName] === undefined) {
|
157 | var defaultValue = argument.defaultValue;
|
158 | if (defaultValue !== undefined) {
|
159 | utils.updateArgument(argName, sourceArgType, argumentNodeMap, variableDefinitionMap, variableValues, utils.serializeInputValue(sourceArgType, defaultValue));
|
160 | }
|
161 | }
|
162 | });
|
163 | }
|
164 |
|
165 | var UNPATHED_ERRORS_SYMBOL = Symbol('subschemaErrors');
|
166 | var OBJECT_SUBSCHEMA_SYMBOL = Symbol('initialSubschema');
|
167 | var FIELD_SUBSCHEMA_MAP_SYMBOL = Symbol('subschemaMap');
|
168 |
|
169 | function isExternalObject(data) {
|
170 | return data[UNPATHED_ERRORS_SYMBOL] !== undefined;
|
171 | }
|
172 | function annotateExternalObject(object, errors, subschema) {
|
173 | var _a;
|
174 | Object.defineProperties(object, (_a = {},
|
175 | _a[OBJECT_SUBSCHEMA_SYMBOL] = { value: subschema },
|
176 | _a[FIELD_SUBSCHEMA_MAP_SYMBOL] = { value: Object.create(null) },
|
177 | _a[UNPATHED_ERRORS_SYMBOL] = { value: errors },
|
178 | _a));
|
179 | return object;
|
180 | }
|
181 | function getSubschema(object, responseKey) {
|
182 | var _a;
|
183 | return (_a = object[FIELD_SUBSCHEMA_MAP_SYMBOL][responseKey]) !== null && _a !== void 0 ? _a : object[OBJECT_SUBSCHEMA_SYMBOL];
|
184 | }
|
185 | function getUnpathedErrors(object) {
|
186 | return object[UNPATHED_ERRORS_SYMBOL];
|
187 | }
|
188 | function mergeExternalObjects(schema, path, typeName, target, sources, selectionSets) {
|
189 | var _a;
|
190 | var results = [];
|
191 | var errors = [];
|
192 | sources.forEach(function (source, index) {
|
193 | if (source instanceof Error || source === null) {
|
194 | var selectionSet = selectionSets[index];
|
195 | var fieldNodes_1 = utils.collectFields({
|
196 | schema: schema,
|
197 | variableValues: {},
|
198 | fragments: {},
|
199 | }, schema.getType(typeName), selectionSet, Object.create(null), Object.create(null));
|
200 | var nullResult_1 = {};
|
201 | Object.keys(fieldNodes_1).forEach(function (responseKey) {
|
202 | if (source instanceof graphql.GraphQLError) {
|
203 | nullResult_1[responseKey] = utils.relocatedError(source, path.concat([responseKey]));
|
204 | }
|
205 | else if (source instanceof Error) {
|
206 | nullResult_1[responseKey] = graphql.locatedError(source, fieldNodes_1[responseKey], path.concat([responseKey]));
|
207 | }
|
208 | else {
|
209 | nullResult_1[responseKey] = null;
|
210 | }
|
211 | });
|
212 | results.push(nullResult_1);
|
213 | }
|
214 | else {
|
215 | errors = errors.concat(source[UNPATHED_ERRORS_SYMBOL]);
|
216 | results.push(source);
|
217 | }
|
218 | });
|
219 | var combinedResult = results.reduce(utils.mergeDeep, target);
|
220 | var newFieldSubschemaMap = (_a = target[FIELD_SUBSCHEMA_MAP_SYMBOL]) !== null && _a !== void 0 ? _a : Object.create(null);
|
221 | results.forEach(function (source) {
|
222 | var objectSubschema = source[OBJECT_SUBSCHEMA_SYMBOL];
|
223 | var fieldSubschemaMap = source[FIELD_SUBSCHEMA_MAP_SYMBOL];
|
224 | if (fieldSubschemaMap === undefined) {
|
225 | Object.keys(source).forEach(function (responseKey) {
|
226 | newFieldSubschemaMap[responseKey] = objectSubschema;
|
227 | });
|
228 | }
|
229 | else {
|
230 | Object.keys(source).forEach(function (responseKey) {
|
231 | var _a;
|
232 | newFieldSubschemaMap[responseKey] = (_a = fieldSubschemaMap[responseKey]) !== null && _a !== void 0 ? _a : objectSubschema;
|
233 | });
|
234 | }
|
235 | });
|
236 | combinedResult[FIELD_SUBSCHEMA_MAP_SYMBOL] = newFieldSubschemaMap;
|
237 | combinedResult[OBJECT_SUBSCHEMA_SYMBOL] = target[OBJECT_SUBSCHEMA_SYMBOL];
|
238 | combinedResult[UNPATHED_ERRORS_SYMBOL] = target[UNPATHED_ERRORS_SYMBOL].concat(errors);
|
239 | return combinedResult;
|
240 | }
|
241 |
|
242 | function isSubschemaConfig(value) {
|
243 | return Boolean(value === null || value === void 0 ? void 0 : value.schema);
|
244 | }
|
245 | function cloneSubschemaConfig(subschemaConfig) {
|
246 | var newSubschemaConfig = tslib.__assign(tslib.__assign({}, subschemaConfig), { transforms: subschemaConfig.transforms != null ? tslib.__spreadArray([], tslib.__read(subschemaConfig.transforms)) : undefined });
|
247 | if (newSubschemaConfig.merge != null) {
|
248 | newSubschemaConfig.merge = tslib.__assign({}, subschemaConfig.merge);
|
249 | Object.keys(newSubschemaConfig.merge).forEach(function (typeName) {
|
250 | var mergedTypeConfig = (newSubschemaConfig.merge[typeName] = tslib.__assign({}, subschemaConfig.merge[typeName]));
|
251 | if (mergedTypeConfig.entryPoints != null) {
|
252 | mergedTypeConfig.entryPoints = mergedTypeConfig.entryPoints.map(function (entryPoint) { return (tslib.__assign({}, entryPoint)); });
|
253 | }
|
254 | if (mergedTypeConfig.fields != null) {
|
255 | var fields_1 = (mergedTypeConfig.fields = tslib.__assign({}, mergedTypeConfig.fields));
|
256 | Object.keys(fields_1).forEach(function (fieldName) {
|
257 | fields_1[fieldName] = tslib.__assign({}, fields_1[fieldName]);
|
258 | });
|
259 | }
|
260 | });
|
261 | }
|
262 | return newSubschemaConfig;
|
263 | }
|
264 |
|
265 | function memoizeInfoAnd2Objects(fn) {
|
266 | var cache1;
|
267 | function memoized(a1, a2, a3) {
|
268 | if (!cache1) {
|
269 | cache1 = new WeakMap();
|
270 | var cache2_1 = new WeakMap();
|
271 | cache1.set(a1.fieldNodes, cache2_1);
|
272 | var cache3_1 = new WeakMap();
|
273 | cache2_1.set(a2, cache3_1);
|
274 | var newValue = fn(a1, a2, a3);
|
275 | cache3_1.set(a3, newValue);
|
276 | return newValue;
|
277 | }
|
278 | var cache2 = cache1.get(a1.fieldNodes);
|
279 | if (!cache2) {
|
280 | cache2 = new WeakMap();
|
281 | cache1.set(a1.fieldNodes, cache2);
|
282 | var cache3_2 = new WeakMap();
|
283 | cache2.set(a2, cache3_2);
|
284 | var newValue = fn(a1, a2, a3);
|
285 | cache3_2.set(a3, newValue);
|
286 | return newValue;
|
287 | }
|
288 | var cache3 = cache2.get(a2);
|
289 | if (!cache3) {
|
290 | cache3 = new WeakMap();
|
291 | cache2.set(a2, cache3);
|
292 | var newValue = fn(a1, a2, a3);
|
293 | cache3.set(a3, newValue);
|
294 | return newValue;
|
295 | }
|
296 | var cachedValue = cache3.get(a3);
|
297 | if (cachedValue === undefined) {
|
298 | var newValue = fn(a1, a2, a3);
|
299 | cache3.set(a3, newValue);
|
300 | return newValue;
|
301 | }
|
302 | return cachedValue;
|
303 | }
|
304 | return memoized;
|
305 | }
|
306 | function memoize4(fn) {
|
307 | var cache1;
|
308 | function memoized(a1, a2, a3, a4) {
|
309 | if (!cache1) {
|
310 | cache1 = new WeakMap();
|
311 | var cache2_2 = new WeakMap();
|
312 | cache1.set(a1, cache2_2);
|
313 | var cache3_3 = new WeakMap();
|
314 | cache2_2.set(a2, cache3_3);
|
315 | var cache4_1 = new WeakMap();
|
316 | cache3_3.set(a3, cache4_1);
|
317 | var newValue = fn(a1, a2, a3, a4);
|
318 | cache4_1.set(a4, newValue);
|
319 | return newValue;
|
320 | }
|
321 | var cache2 = cache1.get(a1);
|
322 | if (!cache2) {
|
323 | cache2 = new WeakMap();
|
324 | cache1.set(a1, cache2);
|
325 | var cache3_4 = new WeakMap();
|
326 | cache2.set(a2, cache3_4);
|
327 | var cache4_2 = new WeakMap();
|
328 | cache3_4.set(a3, cache4_2);
|
329 | var newValue = fn(a1, a2, a3, a4);
|
330 | cache4_2.set(a4, newValue);
|
331 | return newValue;
|
332 | }
|
333 | var cache3 = cache2.get(a2);
|
334 | if (!cache3) {
|
335 | cache3 = new WeakMap();
|
336 | cache2.set(a2, cache3);
|
337 | var cache4_3 = new WeakMap();
|
338 | cache3.set(a3, cache4_3);
|
339 | var newValue = fn(a1, a2, a3, a4);
|
340 | cache4_3.set(a4, newValue);
|
341 | return newValue;
|
342 | }
|
343 | var cache4 = cache3.get(a3);
|
344 | if (!cache4) {
|
345 | var cache4_4 = new WeakMap();
|
346 | cache3.set(a3, cache4_4);
|
347 | var newValue = fn(a1, a2, a3, a4);
|
348 | cache4_4.set(a4, newValue);
|
349 | return newValue;
|
350 | }
|
351 | var cachedValue = cache4.get(a4);
|
352 | if (cachedValue === undefined) {
|
353 | var newValue = fn(a1, a2, a3, a4);
|
354 | cache4.set(a4, newValue);
|
355 | return newValue;
|
356 | }
|
357 | return cachedValue;
|
358 | }
|
359 | return memoized;
|
360 | }
|
361 | function memoize3(fn) {
|
362 | var cache1;
|
363 | function memoized(a1, a2, a3) {
|
364 | if (!cache1) {
|
365 | cache1 = new WeakMap();
|
366 | var cache2_3 = new WeakMap();
|
367 | cache1.set(a1, cache2_3);
|
368 | var cache3_5 = new WeakMap();
|
369 | cache2_3.set(a2, cache3_5);
|
370 | var newValue = fn(a1, a2, a3);
|
371 | cache3_5.set(a3, newValue);
|
372 | return newValue;
|
373 | }
|
374 | var cache2 = cache1.get(a1);
|
375 | if (!cache2) {
|
376 | cache2 = new WeakMap();
|
377 | cache1.set(a1, cache2);
|
378 | var cache3_6 = new WeakMap();
|
379 | cache2.set(a2, cache3_6);
|
380 | var newValue = fn(a1, a2, a3);
|
381 | cache3_6.set(a3, newValue);
|
382 | return newValue;
|
383 | }
|
384 | var cache3 = cache2.get(a2);
|
385 | if (!cache3) {
|
386 | cache3 = new WeakMap();
|
387 | cache2.set(a2, cache3);
|
388 | var newValue = fn(a1, a2, a3);
|
389 | cache3.set(a3, newValue);
|
390 | return newValue;
|
391 | }
|
392 | var cachedValue = cache3.get(a3);
|
393 | if (cachedValue === undefined) {
|
394 | var newValue = fn(a1, a2, a3);
|
395 | cache3.set(a3, newValue);
|
396 | return newValue;
|
397 | }
|
398 | return cachedValue;
|
399 | }
|
400 | return memoized;
|
401 | }
|
402 | function memoize2(fn) {
|
403 | var cache1;
|
404 | function memoized(a1, a2) {
|
405 | if (!cache1) {
|
406 | cache1 = new WeakMap();
|
407 | var cache2_4 = new WeakMap();
|
408 | cache1.set(a1, cache2_4);
|
409 | var newValue = fn(a1, a2);
|
410 | cache2_4.set(a2, newValue);
|
411 | return newValue;
|
412 | }
|
413 | var cache2 = cache1.get(a1);
|
414 | if (!cache2) {
|
415 | cache2 = new WeakMap();
|
416 | cache1.set(a1, cache2);
|
417 | var newValue = fn(a1, a2);
|
418 | cache2.set(a2, newValue);
|
419 | return newValue;
|
420 | }
|
421 | var cachedValue = cache2.get(a2);
|
422 | if (cachedValue === undefined) {
|
423 | var newValue = fn(a1, a2);
|
424 | cache2.set(a2, newValue);
|
425 | return newValue;
|
426 | }
|
427 | return cachedValue;
|
428 | }
|
429 | return memoized;
|
430 | }
|
431 |
|
432 | function collectSubFields(info, typeName) {
|
433 | var subFieldNodes = Object.create(null);
|
434 | var visitedFragmentNames = Object.create(null);
|
435 | var type = info.schema.getType(typeName);
|
436 | var partialExecutionContext = {
|
437 | schema: info.schema,
|
438 | variableValues: info.variableValues,
|
439 | fragments: info.fragments,
|
440 | };
|
441 | info.fieldNodes.forEach(function (fieldNode) {
|
442 | subFieldNodes = utils.collectFields(partialExecutionContext, type, fieldNode.selectionSet, subFieldNodes, visitedFragmentNames);
|
443 | });
|
444 | var stitchingInfo = info.schema.extensions.stitchingInfo;
|
445 | var selectionSetsByField = stitchingInfo.selectionSetsByField;
|
446 | Object.keys(subFieldNodes).forEach(function (responseName) {
|
447 | var _a;
|
448 | var fieldName = subFieldNodes[responseName][0].name.value;
|
449 | var fieldSelectionSet = (_a = selectionSetsByField === null || selectionSetsByField === void 0 ? void 0 : selectionSetsByField[typeName]) === null || _a === void 0 ? void 0 : _a[fieldName];
|
450 | if (fieldSelectionSet != null) {
|
451 | subFieldNodes = utils.collectFields(partialExecutionContext, type, fieldSelectionSet, subFieldNodes, visitedFragmentNames);
|
452 | }
|
453 | });
|
454 | return subFieldNodes;
|
455 | }
|
456 | var getFieldsNotInSubschema = memoizeInfoAnd2Objects(function (info, subschema, mergedTypeInfo) {
|
457 | var typeMap = isSubschemaConfig(subschema) ? mergedTypeInfo.typeMaps.get(subschema) : subschema.getTypeMap();
|
458 | var typeName = mergedTypeInfo.typeName;
|
459 | var fields = typeMap[typeName].getFields();
|
460 | var subFieldNodes = collectSubFields(info, typeName);
|
461 | var fieldsNotInSchema = [];
|
462 | Object.keys(subFieldNodes).forEach(function (responseName) {
|
463 | var fieldName = subFieldNodes[responseName][0].name.value;
|
464 | if (!(fieldName in fields)) {
|
465 | fieldsNotInSchema = fieldsNotInSchema.concat(subFieldNodes[responseName]);
|
466 | }
|
467 | });
|
468 | return fieldsNotInSchema;
|
469 | });
|
470 |
|
471 | var sortSubschemasByProxiability = memoize4(function (mergedTypeInfo, sourceSubschemaOrSourceSubschemas, targetSubschemas, fieldNodes) {
|
472 |
|
473 | var proxiableSubschemas = [];
|
474 | var nonProxiableSubschemas = [];
|
475 | targetSubschemas.forEach(function (t) {
|
476 | var selectionSet = mergedTypeInfo.selectionSets.get(t);
|
477 | var fieldSelectionSets = mergedTypeInfo.fieldSelectionSets.get(t);
|
478 | if (selectionSet != null &&
|
479 | !subschemaTypesContainSelectionSet(mergedTypeInfo, sourceSubschemaOrSourceSubschemas, selectionSet)) {
|
480 | nonProxiableSubschemas.push(t);
|
481 | }
|
482 | else {
|
483 | if (fieldSelectionSets == null ||
|
484 | fieldNodes.every(function (fieldNode) {
|
485 | var fieldName = fieldNode.name.value;
|
486 | var fieldSelectionSet = fieldSelectionSets[fieldName];
|
487 | return (fieldSelectionSet == null ||
|
488 | subschemaTypesContainSelectionSet(mergedTypeInfo, sourceSubschemaOrSourceSubschemas, fieldSelectionSet));
|
489 | })) {
|
490 | proxiableSubschemas.push(t);
|
491 | }
|
492 | else {
|
493 | nonProxiableSubschemas.push(t);
|
494 | }
|
495 | }
|
496 | });
|
497 | return {
|
498 | proxiableSubschemas: proxiableSubschemas,
|
499 | nonProxiableSubschemas: nonProxiableSubschemas,
|
500 | };
|
501 | });
|
502 | var buildDelegationPlan = memoize3(function (mergedTypeInfo, fieldNodes, proxiableSubschemas) {
|
503 | var uniqueFields = mergedTypeInfo.uniqueFields, nonUniqueFields = mergedTypeInfo.nonUniqueFields;
|
504 | var unproxiableFieldNodes = [];
|
505 |
|
506 | var delegationMap = new Map();
|
507 | fieldNodes.forEach(function (fieldNode) {
|
508 | if (fieldNode.name.value === '__typename') {
|
509 | return;
|
510 | }
|
511 |
|
512 | var uniqueSubschema = uniqueFields[fieldNode.name.value];
|
513 | if (uniqueSubschema != null) {
|
514 | if (!proxiableSubschemas.includes(uniqueSubschema)) {
|
515 | unproxiableFieldNodes.push(fieldNode);
|
516 | return;
|
517 | }
|
518 | var existingSubschema_1 = delegationMap.get(uniqueSubschema);
|
519 | if (existingSubschema_1 != null) {
|
520 | existingSubschema_1.push(fieldNode);
|
521 | }
|
522 | else {
|
523 | delegationMap.set(uniqueSubschema, [fieldNode]);
|
524 | }
|
525 | return;
|
526 | }
|
527 |
|
528 |
|
529 | var nonUniqueSubschemas = nonUniqueFields[fieldNode.name.value];
|
530 | if (nonUniqueSubschemas == null) {
|
531 | unproxiableFieldNodes.push(fieldNode);
|
532 | return;
|
533 | }
|
534 | nonUniqueSubschemas = nonUniqueSubschemas.filter(function (s) { return proxiableSubschemas.includes(s); });
|
535 | if (!nonUniqueSubschemas.length) {
|
536 | unproxiableFieldNodes.push(fieldNode);
|
537 | return;
|
538 | }
|
539 | var existingSubschema = nonUniqueSubschemas.find(function (s) { return delegationMap.has(s); });
|
540 | if (existingSubschema != null) {
|
541 | delegationMap.get(existingSubschema).push(fieldNode);
|
542 | }
|
543 | else {
|
544 | delegationMap.set(nonUniqueSubschemas[0], [fieldNode]);
|
545 | }
|
546 | });
|
547 | var finalDelegationMap = new Map();
|
548 | delegationMap.forEach(function (selections, subschema) {
|
549 | finalDelegationMap.set(subschema, {
|
550 | kind: graphql.Kind.SELECTION_SET,
|
551 | selections: selections,
|
552 | });
|
553 | });
|
554 | return {
|
555 | delegationMap: finalDelegationMap,
|
556 | unproxiableFieldNodes: unproxiableFieldNodes,
|
557 | };
|
558 | });
|
559 | var combineSubschemas = memoize2(function (subschemaOrSubschemas, additionalSubschemas) {
|
560 | return Array.isArray(subschemaOrSubschemas)
|
561 | ? subschemaOrSubschemas.concat(additionalSubschemas)
|
562 | : [subschemaOrSubschemas].concat(additionalSubschemas);
|
563 | });
|
564 | function mergeFields(mergedTypeInfo, typeName, object, fieldNodes, sourceSubschemaOrSourceSubschemas, targetSubschemas, context, info) {
|
565 | if (!fieldNodes.length) {
|
566 | return object;
|
567 | }
|
568 | var _a = sortSubschemasByProxiability(mergedTypeInfo, sourceSubschemaOrSourceSubschemas, targetSubschemas, fieldNodes), proxiableSubschemas = _a.proxiableSubschemas, nonProxiableSubschemas = _a.nonProxiableSubschemas;
|
569 | var _b = buildDelegationPlan(mergedTypeInfo, fieldNodes, proxiableSubschemas), delegationMap = _b.delegationMap, unproxiableFieldNodes = _b.unproxiableFieldNodes;
|
570 | if (!delegationMap.size) {
|
571 | return object;
|
572 | }
|
573 | var resultMap = new Map();
|
574 | delegationMap.forEach(function (selectionSet, s) {
|
575 | var resolver = mergedTypeInfo.resolvers.get(s);
|
576 | var valueOrPromise$1 = new valueOrPromise.ValueOrPromise(function () { return resolver(object, context, info, s, selectionSet); }).catch(function (error) { return error; });
|
577 | resultMap.set(valueOrPromise$1, selectionSet);
|
578 | });
|
579 | return valueOrPromise.ValueOrPromise.all(Array.from(resultMap.keys())).then(function (results) {
|
580 | return mergeFields(mergedTypeInfo, typeName, mergeExternalObjects(info.schema, graphql.responsePathAsArray(info.path), object.__typename, object, results, Array.from(resultMap.values())), unproxiableFieldNodes, combineSubschemas(sourceSubschemaOrSourceSubschemas, proxiableSubschemas), nonProxiableSubschemas, context, info);
|
581 | }).resolve();
|
582 | }
|
583 | var subschemaTypesContainSelectionSet = memoize3(function (mergedTypeInfo, sourceSubschemaOrSourceSubschemas, selectionSet) {
|
584 | if (Array.isArray(sourceSubschemaOrSourceSubschemas)) {
|
585 | return typesContainSelectionSet(sourceSubschemaOrSourceSubschemas.map(function (sourceSubschema) { return sourceSubschema.transformedSchema.getType(mergedTypeInfo.typeName); }), selectionSet);
|
586 | }
|
587 | return typesContainSelectionSet([sourceSubschemaOrSourceSubschemas.transformedSchema.getType(mergedTypeInfo.typeName)], selectionSet);
|
588 | });
|
589 | function typesContainSelectionSet(types, selectionSet) {
|
590 | var e_1, _a;
|
591 | var fieldMaps = types.map(function (type) { return type.getFields(); });
|
592 | var _loop_1 = function (selection) {
|
593 | if (selection.kind === graphql.Kind.FIELD) {
|
594 | var fields = fieldMaps.map(function (fieldMap) { return fieldMap[selection.name.value]; }).filter(function (field) { return field != null; });
|
595 | if (!fields.length) {
|
596 | return { value: false };
|
597 | }
|
598 | if (selection.selectionSet != null) {
|
599 | return { value: typesContainSelectionSet(fields.map(function (field) { return graphql.getNamedType(field.type); }), selection.selectionSet) };
|
600 | }
|
601 | }
|
602 | else if (selection.kind === graphql.Kind.INLINE_FRAGMENT && selection.typeCondition.name.value === types[0].name) {
|
603 | return { value: typesContainSelectionSet(types, selection.selectionSet) };
|
604 | }
|
605 | };
|
606 | try {
|
607 | for (var _b = tslib.__values(selectionSet.selections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
608 | var selection = _c.value;
|
609 | var state_1 = _loop_1(selection);
|
610 | if (typeof state_1 === "object")
|
611 | return state_1.value;
|
612 | }
|
613 | }
|
614 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
615 | finally {
|
616 | try {
|
617 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
618 | }
|
619 | finally { if (e_1) throw e_1.error; }
|
620 | }
|
621 | return true;
|
622 | }
|
623 |
|
624 | function resolveExternalValue(result, unpathedErrors, subschema, context, info, returnType, skipTypeMerging) {
|
625 | if (returnType === void 0) { returnType = info.returnType; }
|
626 | var type = graphql.getNullableType(returnType);
|
627 | if (result instanceof Error) {
|
628 | return result;
|
629 | }
|
630 | if (result == null) {
|
631 | return reportUnpathedErrorsViaNull(unpathedErrors);
|
632 | }
|
633 | if (graphql.isLeafType(type)) {
|
634 | return type.parseValue(result);
|
635 | }
|
636 | else if (graphql.isCompositeType(type)) {
|
637 | return resolveExternalObject(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
|
638 | }
|
639 | else if (graphql.isListType(type)) {
|
640 | return resolveExternalList(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
|
641 | }
|
642 | }
|
643 | function resolveExternalObject(type, object, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
644 | var _a;
|
645 |
|
646 |
|
647 | if (isExternalObject(object)) {
|
648 | return object;
|
649 | }
|
650 | annotateExternalObject(object, unpathedErrors, subschema);
|
651 | var stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo;
|
652 | if (skipTypeMerging || !stitchingInfo) {
|
653 | return object;
|
654 | }
|
655 | var typeName;
|
656 | if (graphql.isAbstractType(type)) {
|
657 | var resolvedType = info.schema.getTypeMap()[object.__typename];
|
658 | if (resolvedType == null) {
|
659 | throw new Error("Unable to resolve type '" + object.__typename + "'. Did you forget to include a transform that renames types? Did you delegate to the original subschema rather that the subschema config object containing the transform?");
|
660 | }
|
661 | typeName = resolvedType.name;
|
662 | }
|
663 | else {
|
664 | typeName = type.name;
|
665 | }
|
666 | var mergedTypeInfo = stitchingInfo.mergedTypes[typeName];
|
667 | var targetSubschemas;
|
668 |
|
669 |
|
670 | if (mergedTypeInfo != null) {
|
671 | targetSubschemas = mergedTypeInfo.targetSubschemas.get(subschema);
|
672 | }
|
673 |
|
674 | if (!targetSubschemas) {
|
675 | return object;
|
676 | }
|
677 | var fieldNodes = getFieldsNotInSubschema(info, subschema, mergedTypeInfo);
|
678 | return mergeFields(mergedTypeInfo, typeName, object, fieldNodes, subschema, targetSubschemas, context, info);
|
679 | }
|
680 | function resolveExternalList(type, list, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
681 | return list.map(function (listMember) {
|
682 | return resolveExternalListMember(graphql.getNullableType(type.ofType), listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
|
683 | });
|
684 | }
|
685 | function resolveExternalListMember(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
686 | if (listMember instanceof Error) {
|
687 | return listMember;
|
688 | }
|
689 | if (listMember == null) {
|
690 | return reportUnpathedErrorsViaNull(unpathedErrors);
|
691 | }
|
692 | if (graphql.isLeafType(type)) {
|
693 | return type.parseValue(listMember);
|
694 | }
|
695 | else if (graphql.isCompositeType(type)) {
|
696 | return resolveExternalObject(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
|
697 | }
|
698 | else if (graphql.isListType(type)) {
|
699 | return resolveExternalList(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
|
700 | }
|
701 | }
|
702 | var reportedErrors = new Map();
|
703 | function reportUnpathedErrorsViaNull(unpathedErrors) {
|
704 | if (unpathedErrors.length) {
|
705 | var unreportedErrors_1 = [];
|
706 | unpathedErrors.forEach(function (error) {
|
707 | if (!reportedErrors.has(error)) {
|
708 | unreportedErrors_1.push(error);
|
709 | reportedErrors.set(error, true);
|
710 | }
|
711 | });
|
712 | if (unreportedErrors_1.length) {
|
713 | if (unreportedErrors_1.length === 1) {
|
714 | return unreportedErrors_1[0];
|
715 | }
|
716 | var combinedError = new AggregateError(unreportedErrors_1);
|
717 | return graphql.locatedError(combinedError, undefined, unreportedErrors_1[0].path);
|
718 | }
|
719 | }
|
720 | return null;
|
721 | }
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 | function defaultMergedResolver(parent, args, context, info) {
|
730 | if (!parent) {
|
731 | return null;
|
732 | }
|
733 | var responseKey = utils.getResponseKeyFromInfo(info);
|
734 |
|
735 |
|
736 | if (!isExternalObject(parent)) {
|
737 | return graphql.defaultFieldResolver(parent, args, context, info);
|
738 | }
|
739 | var data = parent[responseKey];
|
740 | var unpathedErrors = getUnpathedErrors(parent);
|
741 | var subschema = getSubschema(parent, responseKey);
|
742 | return resolveExternalValue(data, unpathedErrors, subschema, context, info);
|
743 | }
|
744 |
|
745 | var VisitSelectionSets = (function () {
|
746 | function VisitSelectionSets(visitor) {
|
747 | this.visitor = visitor;
|
748 | }
|
749 | VisitSelectionSets.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
750 | var document = visitSelectionSets(originalRequest, delegationContext.info.schema, delegationContext.returnType, this.visitor);
|
751 | return tslib.__assign(tslib.__assign({}, originalRequest), { document: document });
|
752 | };
|
753 | return VisitSelectionSets;
|
754 | }());
|
755 | function visitSelectionSets(request, schema, initialType, visitor) {
|
756 | var document = request.document, variables = request.variables;
|
757 | var operations = [];
|
758 | var fragments = Object.create(null);
|
759 | document.definitions.forEach(function (def) {
|
760 | if (def.kind === graphql.Kind.OPERATION_DEFINITION) {
|
761 | operations.push(def);
|
762 | }
|
763 | else if (def.kind === graphql.Kind.FRAGMENT_DEFINITION) {
|
764 | fragments[def.name.value] = def;
|
765 | }
|
766 | });
|
767 | var partialExecutionContext = {
|
768 | schema: schema,
|
769 | variableValues: variables,
|
770 | fragments: fragments,
|
771 | };
|
772 | var typeInfo = new graphql.TypeInfo(schema, undefined, initialType);
|
773 | var newDefinitions = operations.map(function (operation) {
|
774 | var type = operation.operation === 'query'
|
775 | ? schema.getQueryType()
|
776 | : operation.operation === 'mutation'
|
777 | ? schema.getMutationType()
|
778 | : schema.getSubscriptionType();
|
779 | var fields = utils.collectFields(partialExecutionContext, type, operation.selectionSet, Object.create(null), Object.create(null));
|
780 | var newSelections = [];
|
781 | Object.keys(fields).forEach(function (responseKey) {
|
782 | var fieldNodes = fields[responseKey];
|
783 | fieldNodes.forEach(function (fieldNode) {
|
784 | var _a;
|
785 | var selectionSet = fieldNode.selectionSet;
|
786 | if (selectionSet == null) {
|
787 | newSelections.push(fieldNode);
|
788 | return;
|
789 | }
|
790 | var newSelectionSet = graphql.visit(selectionSet, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
791 | _a[graphql.Kind.SELECTION_SET] = function (node) { return visitor(node, typeInfo); },
|
792 | _a)));
|
793 | if (newSelectionSet === selectionSet) {
|
794 | newSelections.push(fieldNode);
|
795 | return;
|
796 | }
|
797 | newSelections.push(tslib.__assign(tslib.__assign({}, fieldNode), { selectionSet: newSelectionSet }));
|
798 | });
|
799 | });
|
800 | return tslib.__assign(tslib.__assign({}, operation), { selectionSet: {
|
801 | kind: graphql.Kind.SELECTION_SET,
|
802 | selections: newSelections,
|
803 | } });
|
804 | });
|
805 | Object.values(fragments).forEach(function (fragment) {
|
806 | var _a;
|
807 | newDefinitions.push(graphql.visit(fragment, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
808 | _a[graphql.Kind.SELECTION_SET] = function (node) { return visitor(node, typeInfo); },
|
809 | _a))));
|
810 | });
|
811 | return tslib.__assign(tslib.__assign({}, document), { definitions: newDefinitions });
|
812 | }
|
813 |
|
814 | var AddSelectionSets = (function () {
|
815 | function AddSelectionSets(selectionSetsByType, selectionSetsByField, dynamicSelectionSetsByField) {
|
816 | this.transformer = new VisitSelectionSets(function (node, typeInfo) {
|
817 | return visitSelectionSet(node, typeInfo, selectionSetsByType, selectionSetsByField, dynamicSelectionSetsByField);
|
818 | });
|
819 | }
|
820 | AddSelectionSets.prototype.transformRequest = function (originalRequest, delegationContext, transformationContext) {
|
821 | return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
822 | };
|
823 | return AddSelectionSets;
|
824 | }());
|
825 | function visitSelectionSet(node, typeInfo, selectionSetsByType, selectionSetsByField, dynamicSelectionSetsByField) {
|
826 | var parentType = typeInfo.getParentType();
|
827 | var newSelections = new Map();
|
828 | if (parentType != null) {
|
829 | var parentTypeName_1 = parentType.name;
|
830 | addSelectionsToMap(newSelections, node);
|
831 | if (parentTypeName_1 in selectionSetsByType) {
|
832 | var selectionSet = selectionSetsByType[parentTypeName_1];
|
833 | addSelectionsToMap(newSelections, selectionSet);
|
834 | }
|
835 | if (parentTypeName_1 in selectionSetsByField) {
|
836 | node.selections.forEach(function (selection) {
|
837 | if (selection.kind === graphql.Kind.FIELD) {
|
838 | var name_1 = selection.name.value;
|
839 | var selectionSet = selectionSetsByField[parentTypeName_1][name_1];
|
840 | if (selectionSet != null) {
|
841 | addSelectionsToMap(newSelections, selectionSet);
|
842 | }
|
843 | }
|
844 | });
|
845 | }
|
846 | if (parentTypeName_1 in dynamicSelectionSetsByField) {
|
847 | node.selections.forEach(function (selection) {
|
848 | if (selection.kind === graphql.Kind.FIELD) {
|
849 | var name_2 = selection.name.value;
|
850 | var dynamicSelectionSets = dynamicSelectionSetsByField[parentTypeName_1][name_2];
|
851 | if (dynamicSelectionSets != null) {
|
852 | dynamicSelectionSets.forEach(function (selectionSetFn) {
|
853 | var selectionSet = selectionSetFn(selection);
|
854 | if (selectionSet != null) {
|
855 | addSelectionsToMap(newSelections, selectionSet);
|
856 | }
|
857 | });
|
858 | }
|
859 | }
|
860 | });
|
861 | }
|
862 | return tslib.__assign(tslib.__assign({}, node), { selections: Array.from(newSelections.values()) });
|
863 | }
|
864 | }
|
865 | var addSelectionsToMap = memoize2(function (map, selectionSet) {
|
866 | selectionSet.selections.forEach(function (selection) {
|
867 | map.set(graphql.print(selection), selection);
|
868 | });
|
869 | });
|
870 |
|
871 | var ExpandAbstractTypes = (function () {
|
872 | function ExpandAbstractTypes() {
|
873 | }
|
874 | ExpandAbstractTypes.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
875 | var targetSchema = delegationContext.targetSchema;
|
876 | var _a = extractPossibleTypes(delegationContext.info.schema, targetSchema), possibleTypesMap = _a.possibleTypesMap, interfaceExtensionsMap = _a.interfaceExtensionsMap;
|
877 | var reversePossibleTypesMap = flipMapping(possibleTypesMap);
|
878 | var document = expandAbstractTypes(targetSchema, possibleTypesMap, reversePossibleTypesMap, interfaceExtensionsMap, originalRequest.document);
|
879 | return tslib.__assign(tslib.__assign({}, originalRequest), { document: document });
|
880 | };
|
881 | return ExpandAbstractTypes;
|
882 | }());
|
883 | function extractPossibleTypes(sourceSchema, targetSchema) {
|
884 | var typeMap = sourceSchema.getTypeMap();
|
885 | var possibleTypesMap = Object.create(null);
|
886 | var interfaceExtensionsMap = Object.create(null);
|
887 | Object.keys(typeMap).forEach(function (typeName) {
|
888 | var type = typeMap[typeName];
|
889 | if (graphql.isAbstractType(type)) {
|
890 | var targetType = targetSchema.getType(typeName);
|
891 | if (graphql.isInterfaceType(type) && graphql.isInterfaceType(targetType)) {
|
892 | var targetTypeFields_1 = targetType.getFields();
|
893 | var extensionFields_1 = Object.create(null);
|
894 | Object.keys(type.getFields()).forEach(function (fieldName) {
|
895 | if (!targetTypeFields_1[fieldName]) {
|
896 | extensionFields_1[fieldName] = true;
|
897 | }
|
898 | });
|
899 | if (Object.keys(extensionFields_1).length) {
|
900 | interfaceExtensionsMap[typeName] = extensionFields_1;
|
901 | }
|
902 | }
|
903 | if (!graphql.isAbstractType(targetType) || typeName in interfaceExtensionsMap) {
|
904 | var implementations = sourceSchema.getPossibleTypes(type);
|
905 | possibleTypesMap[typeName] = implementations
|
906 | .filter(function (impl) { return targetSchema.getType(impl.name); })
|
907 | .map(function (impl) { return impl.name; });
|
908 | }
|
909 | }
|
910 | });
|
911 | return { possibleTypesMap: possibleTypesMap, interfaceExtensionsMap: interfaceExtensionsMap };
|
912 | }
|
913 | function flipMapping(mapping) {
|
914 | var result = Object.create(null);
|
915 | Object.keys(mapping).forEach(function (typeName) {
|
916 | var toTypeNames = mapping[typeName];
|
917 | toTypeNames.forEach(function (toTypeName) {
|
918 | if (!(toTypeName in result)) {
|
919 | result[toTypeName] = [];
|
920 | }
|
921 | result[toTypeName].push(typeName);
|
922 | });
|
923 | });
|
924 | return result;
|
925 | }
|
926 | function expandAbstractTypes(targetSchema, possibleTypesMap, reversePossibleTypesMap, interfaceExtensionsMap, document) {
|
927 | var _a;
|
928 | var operations = document.definitions.filter(function (def) { return def.kind === graphql.Kind.OPERATION_DEFINITION; });
|
929 | var fragments = document.definitions.filter(function (def) { return def.kind === graphql.Kind.FRAGMENT_DEFINITION; });
|
930 | var existingFragmentNames = fragments.map(function (fragment) { return fragment.name.value; });
|
931 | var fragmentCounter = 0;
|
932 | var generateFragmentName = function (typeName) {
|
933 | var fragmentName;
|
934 | do {
|
935 | fragmentName = "_" + typeName + "_Fragment" + fragmentCounter.toString();
|
936 | fragmentCounter++;
|
937 | } while (existingFragmentNames.indexOf(fragmentName) !== -1);
|
938 | return fragmentName;
|
939 | };
|
940 | var generateInlineFragment = function (typeName, selectionSet) {
|
941 | return {
|
942 | kind: graphql.Kind.INLINE_FRAGMENT,
|
943 | typeCondition: {
|
944 | kind: graphql.Kind.NAMED_TYPE,
|
945 | name: {
|
946 | kind: graphql.Kind.NAME,
|
947 | value: typeName,
|
948 | },
|
949 | },
|
950 | selectionSet: selectionSet,
|
951 | };
|
952 | };
|
953 | var newFragments = [];
|
954 | var fragmentReplacements = Object.create(null);
|
955 | fragments.forEach(function (fragment) {
|
956 | newFragments.push(fragment);
|
957 | var possibleTypes = possibleTypesMap[fragment.typeCondition.name.value];
|
958 | if (possibleTypes != null) {
|
959 | fragmentReplacements[fragment.name.value] = [];
|
960 | possibleTypes.forEach(function (possibleTypeName) {
|
961 | var name = generateFragmentName(possibleTypeName);
|
962 | existingFragmentNames.push(name);
|
963 | var newFragment = {
|
964 | kind: graphql.Kind.FRAGMENT_DEFINITION,
|
965 | name: {
|
966 | kind: graphql.Kind.NAME,
|
967 | value: name,
|
968 | },
|
969 | typeCondition: {
|
970 | kind: graphql.Kind.NAMED_TYPE,
|
971 | name: {
|
972 | kind: graphql.Kind.NAME,
|
973 | value: possibleTypeName,
|
974 | },
|
975 | },
|
976 | selectionSet: fragment.selectionSet,
|
977 | };
|
978 | newFragments.push(newFragment);
|
979 | fragmentReplacements[fragment.name.value].push({
|
980 | fragmentName: name,
|
981 | typeName: possibleTypeName,
|
982 | });
|
983 | });
|
984 | }
|
985 | });
|
986 | var newDocument = tslib.__assign(tslib.__assign({}, document), { definitions: tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(operations)), tslib.__read(newFragments)) });
|
987 | var typeInfo = new graphql.TypeInfo(targetSchema);
|
988 | return graphql.visit(newDocument, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
989 | _a[graphql.Kind.SELECTION_SET] = function (node) {
|
990 | var newSelections = node.selections;
|
991 | var addedSelections = [];
|
992 | var maybeType = typeInfo.getParentType();
|
993 | if (maybeType != null) {
|
994 | var parentType_1 = graphql.getNamedType(maybeType);
|
995 | var interfaceExtension_1 = interfaceExtensionsMap[parentType_1.name];
|
996 | var interfaceExtensionFields_1 = [];
|
997 | node.selections.forEach(function (selection) {
|
998 | if (selection.kind === graphql.Kind.INLINE_FRAGMENT) {
|
999 | if (selection.typeCondition != null) {
|
1000 | var possibleTypes = possibleTypesMap[selection.typeCondition.name.value];
|
1001 | if (possibleTypes != null) {
|
1002 | possibleTypes.forEach(function (possibleType) {
|
1003 | var maybePossibleType = targetSchema.getType(possibleType);
|
1004 | if (maybePossibleType != null &&
|
1005 | utils.implementsAbstractType(targetSchema, parentType_1, maybePossibleType)) {
|
1006 | addedSelections.push(generateInlineFragment(possibleType, selection.selectionSet));
|
1007 | }
|
1008 | });
|
1009 | }
|
1010 | }
|
1011 | }
|
1012 | else if (selection.kind === graphql.Kind.FRAGMENT_SPREAD) {
|
1013 | var fragmentName = selection.name.value;
|
1014 | if (fragmentName in fragmentReplacements) {
|
1015 | fragmentReplacements[fragmentName].forEach(function (replacement) {
|
1016 | var typeName = replacement.typeName;
|
1017 | var maybeReplacementType = targetSchema.getType(typeName);
|
1018 | if (maybeReplacementType != null && utils.implementsAbstractType(targetSchema, parentType_1, maybeType)) {
|
1019 | addedSelections.push({
|
1020 | kind: graphql.Kind.FRAGMENT_SPREAD,
|
1021 | name: {
|
1022 | kind: graphql.Kind.NAME,
|
1023 | value: replacement.fragmentName,
|
1024 | },
|
1025 | });
|
1026 | }
|
1027 | });
|
1028 | }
|
1029 | }
|
1030 | else if (interfaceExtension_1 != null &&
|
1031 | interfaceExtension_1[selection.name.value] &&
|
1032 | selection.kind === graphql.Kind.FIELD) {
|
1033 | interfaceExtensionFields_1.push(selection);
|
1034 | }
|
1035 | });
|
1036 | if (parentType_1.name in reversePossibleTypesMap) {
|
1037 | addedSelections.push({
|
1038 | kind: graphql.Kind.FIELD,
|
1039 | name: {
|
1040 | kind: graphql.Kind.NAME,
|
1041 | value: '__typename',
|
1042 | },
|
1043 | });
|
1044 | }
|
1045 | if (interfaceExtensionFields_1.length) {
|
1046 | var possibleTypes = possibleTypesMap[parentType_1.name];
|
1047 | if (possibleTypes != null) {
|
1048 | possibleTypes.forEach(function (possibleType) {
|
1049 | addedSelections.push(generateInlineFragment(possibleType, {
|
1050 | kind: graphql.Kind.SELECTION_SET,
|
1051 | selections: interfaceExtensionFields_1,
|
1052 | }));
|
1053 | });
|
1054 | newSelections = newSelections.filter(function (selection) {
|
1055 | return !(selection.kind === graphql.Kind.FIELD && interfaceExtension_1[selection.name.value]);
|
1056 | });
|
1057 | }
|
1058 | }
|
1059 | }
|
1060 | if (addedSelections.length) {
|
1061 | return tslib.__assign(tslib.__assign({}, node), { selections: newSelections.concat(addedSelections) });
|
1062 | }
|
1063 | },
|
1064 | _a)));
|
1065 | }
|
1066 |
|
1067 |
|
1068 | var WrapConcreteTypes = (function () {
|
1069 | function WrapConcreteTypes() {
|
1070 | }
|
1071 | WrapConcreteTypes.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
1072 | var document = wrapConcreteTypes(delegationContext.returnType, delegationContext.targetSchema, originalRequest.document);
|
1073 | return tslib.__assign(tslib.__assign({}, originalRequest), { document: document });
|
1074 | };
|
1075 | return WrapConcreteTypes;
|
1076 | }());
|
1077 | function wrapConcreteTypes(returnType, targetSchema, document) {
|
1078 | var _a;
|
1079 | var _b, _c, _d;
|
1080 | var namedType = graphql.getNamedType(returnType);
|
1081 | if (!graphql.isObjectType(namedType)) {
|
1082 | return document;
|
1083 | }
|
1084 | var queryTypeName = (_b = targetSchema.getQueryType()) === null || _b === void 0 ? void 0 : _b.name;
|
1085 | var mutationTypeName = (_c = targetSchema.getMutationType()) === null || _c === void 0 ? void 0 : _c.name;
|
1086 | var subscriptionTypeName = (_d = targetSchema.getSubscriptionType()) === null || _d === void 0 ? void 0 : _d.name;
|
1087 | var typeInfo = new graphql.TypeInfo(targetSchema);
|
1088 | var newDocument = graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
1089 | _a[graphql.Kind.FRAGMENT_DEFINITION] = function (node) {
|
1090 | var typeName = node.typeCondition.name.value;
|
1091 | if (typeName !== queryTypeName && typeName !== mutationTypeName && typeName !== subscriptionTypeName) {
|
1092 | return false;
|
1093 | }
|
1094 | },
|
1095 | _a[graphql.Kind.FIELD] = function (node) {
|
1096 | if (graphql.isAbstractType(graphql.getNamedType(typeInfo.getType()))) {
|
1097 | return tslib.__assign(tslib.__assign({}, node), { selectionSet: {
|
1098 | kind: graphql.Kind.SELECTION_SET,
|
1099 | selections: [
|
1100 | {
|
1101 | kind: graphql.Kind.INLINE_FRAGMENT,
|
1102 | typeCondition: {
|
1103 | kind: graphql.Kind.NAMED_TYPE,
|
1104 | name: {
|
1105 | kind: graphql.Kind.NAME,
|
1106 | value: namedType.name,
|
1107 | },
|
1108 | },
|
1109 | selectionSet: node.selectionSet,
|
1110 | },
|
1111 | ],
|
1112 | } });
|
1113 | }
|
1114 | },
|
1115 | _a)),
|
1116 |
|
1117 |
|
1118 |
|
1119 | {
|
1120 | Name: [],
|
1121 | Document: ['definitions'],
|
1122 | OperationDefinition: ['selectionSet'],
|
1123 | VariableDefinition: [],
|
1124 | Variable: [],
|
1125 | SelectionSet: ['selections'],
|
1126 | Field: [],
|
1127 | Argument: [],
|
1128 | FragmentSpread: [],
|
1129 | InlineFragment: ['selectionSet'],
|
1130 | FragmentDefinition: ['selectionSet'],
|
1131 | IntValue: [],
|
1132 | FloatValue: [],
|
1133 | StringValue: [],
|
1134 | BooleanValue: [],
|
1135 | NullValue: [],
|
1136 | EnumValue: [],
|
1137 | ListValue: [],
|
1138 | ObjectValue: [],
|
1139 | ObjectField: [],
|
1140 | Directive: [],
|
1141 | NamedType: [],
|
1142 | ListType: [],
|
1143 | NonNullType: [],
|
1144 | SchemaDefinition: [],
|
1145 | OperationTypeDefinition: [],
|
1146 | ScalarTypeDefinition: [],
|
1147 | ObjectTypeDefinition: [],
|
1148 | FieldDefinition: [],
|
1149 | InputValueDefinition: [],
|
1150 | InterfaceTypeDefinition: [],
|
1151 | UnionTypeDefinition: [],
|
1152 | EnumTypeDefinition: [],
|
1153 | EnumValueDefinition: [],
|
1154 | InputObjectTypeDefinition: [],
|
1155 | DirectiveDefinition: [],
|
1156 | SchemaExtension: [],
|
1157 | ScalarTypeExtension: [],
|
1158 | ObjectTypeExtension: [],
|
1159 | InterfaceTypeExtension: [],
|
1160 | UnionTypeExtension: [],
|
1161 | EnumTypeExtension: [],
|
1162 | InputObjectTypeExtension: [],
|
1163 | });
|
1164 | return newDocument;
|
1165 | }
|
1166 |
|
1167 | var FilterToSchema = (function () {
|
1168 | function FilterToSchema() {
|
1169 | }
|
1170 | FilterToSchema.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
1171 | return tslib.__assign(tslib.__assign({}, originalRequest), filterToSchema(delegationContext.targetSchema, originalRequest.document, originalRequest.variables));
|
1172 | };
|
1173 | return FilterToSchema;
|
1174 | }());
|
1175 | function filterToSchema(targetSchema, document, variables) {
|
1176 | var operations = document.definitions.filter(function (def) { return def.kind === graphql.Kind.OPERATION_DEFINITION; });
|
1177 | var fragments = document.definitions.filter(function (def) { return def.kind === graphql.Kind.FRAGMENT_DEFINITION; });
|
1178 | var usedVariables = [];
|
1179 | var usedFragments = [];
|
1180 | var newOperations = [];
|
1181 | var newFragments = [];
|
1182 | var validFragments = fragments.filter(function (fragment) {
|
1183 | var typeName = fragment.typeCondition.name.value;
|
1184 | return Boolean(targetSchema.getType(typeName));
|
1185 | });
|
1186 | var validFragmentsWithType = validFragments.reduce(function (prev, fragment) {
|
1187 | var _a;
|
1188 | return (tslib.__assign(tslib.__assign({}, prev), (_a = {}, _a[fragment.name.value] = targetSchema.getType(fragment.typeCondition.name.value), _a)));
|
1189 | }, {});
|
1190 | var fragmentSet = Object.create(null);
|
1191 | operations.forEach(function (operation) {
|
1192 | var type;
|
1193 | if (operation.operation === 'subscription') {
|
1194 | type = targetSchema.getSubscriptionType();
|
1195 | }
|
1196 | else if (operation.operation === 'mutation') {
|
1197 | type = targetSchema.getMutationType();
|
1198 | }
|
1199 | else {
|
1200 | type = targetSchema.getQueryType();
|
1201 | }
|
1202 | var _a = filterSelectionSet(targetSchema, type, validFragmentsWithType, operation.selectionSet), selectionSet = _a.selectionSet, operationUsedFragments = _a.usedFragments, operationUsedVariables = _a.usedVariables;
|
1203 | usedFragments = union(usedFragments, operationUsedFragments);
|
1204 | var _b = collectFragmentVariables(targetSchema, fragmentSet, validFragments, validFragmentsWithType, usedFragments), collectedUsedVariables = _b.usedVariables, collectedNewFragments = _b.newFragments, collectedFragmentSet = _b.fragmentSet;
|
1205 | var operationOrFragmentVariables = union(operationUsedVariables, collectedUsedVariables);
|
1206 | usedVariables = union(usedVariables, operationOrFragmentVariables);
|
1207 | newFragments = collectedNewFragments;
|
1208 | fragmentSet = collectedFragmentSet;
|
1209 | var variableDefinitions = operation.variableDefinitions.filter(function (variable) { return operationOrFragmentVariables.indexOf(variable.variable.name.value) !== -1; });
|
1210 | newOperations.push({
|
1211 | kind: graphql.Kind.OPERATION_DEFINITION,
|
1212 | operation: operation.operation,
|
1213 | name: operation.name,
|
1214 | directives: operation.directives,
|
1215 | variableDefinitions: variableDefinitions,
|
1216 | selectionSet: selectionSet,
|
1217 | });
|
1218 | });
|
1219 | var newVariables = usedVariables.reduce(function (acc, variableName) {
|
1220 | var variableValue = variables[variableName];
|
1221 | if (variableValue !== undefined) {
|
1222 | acc[variableName] = variableValue;
|
1223 | }
|
1224 | return acc;
|
1225 | }, {});
|
1226 | return {
|
1227 | document: {
|
1228 | kind: graphql.Kind.DOCUMENT,
|
1229 | definitions: tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(newOperations)), tslib.__read(newFragments)),
|
1230 | },
|
1231 | variables: newVariables,
|
1232 | };
|
1233 | }
|
1234 | function collectFragmentVariables(targetSchema, fragmentSet, validFragments, validFragmentsWithType, usedFragments) {
|
1235 | var remainingFragments = usedFragments.slice();
|
1236 | var usedVariables = [];
|
1237 | var newFragments = [];
|
1238 | var _loop_1 = function () {
|
1239 | var nextFragmentName = remainingFragments.pop();
|
1240 | var fragment = validFragments.find(function (fr) { return fr.name.value === nextFragmentName; });
|
1241 | if (fragment != null) {
|
1242 | var name_1 = nextFragmentName;
|
1243 | var typeName = fragment.typeCondition.name.value;
|
1244 | var type = targetSchema.getType(typeName);
|
1245 | var _a = filterSelectionSet(targetSchema, type, validFragmentsWithType, fragment.selectionSet), selectionSet = _a.selectionSet, fragmentUsedFragments = _a.usedFragments, fragmentUsedVariables = _a.usedVariables;
|
1246 | remainingFragments = union(remainingFragments, fragmentUsedFragments);
|
1247 | usedVariables = union(usedVariables, fragmentUsedVariables);
|
1248 | if (!(name_1 in fragmentSet)) {
|
1249 | fragmentSet[name_1] = true;
|
1250 | newFragments.push({
|
1251 | kind: graphql.Kind.FRAGMENT_DEFINITION,
|
1252 | name: {
|
1253 | kind: graphql.Kind.NAME,
|
1254 | value: name_1,
|
1255 | },
|
1256 | typeCondition: fragment.typeCondition,
|
1257 | selectionSet: selectionSet,
|
1258 | });
|
1259 | }
|
1260 | }
|
1261 | };
|
1262 | while (remainingFragments.length !== 0) {
|
1263 | _loop_1();
|
1264 | }
|
1265 | return {
|
1266 | usedVariables: usedVariables,
|
1267 | newFragments: newFragments,
|
1268 | fragmentSet: fragmentSet,
|
1269 | };
|
1270 | }
|
1271 | function filterSelectionSet(schema, type, validFragments, selectionSet) {
|
1272 | var _a;
|
1273 | var usedFragments = [];
|
1274 | var usedVariables = [];
|
1275 | var typeInfo = new graphql.TypeInfo(schema, undefined, type);
|
1276 | var filteredSelectionSet = graphql.visit(selectionSet, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
1277 | _a[graphql.Kind.FIELD] = {
|
1278 | enter: function (node) {
|
1279 | var parentType = typeInfo.getParentType();
|
1280 | if (graphql.isObjectType(parentType) || graphql.isInterfaceType(parentType)) {
|
1281 | var fields = parentType.getFields();
|
1282 | var field = node.name.value === '__typename' ? graphql.TypeNameMetaFieldDef : fields[node.name.value];
|
1283 | if (!field) {
|
1284 | return null;
|
1285 | }
|
1286 | var argNames_1 = (field.args != null ? field.args : []).map(function (arg) { return arg.name; });
|
1287 | if (node.arguments != null) {
|
1288 | var args = node.arguments.filter(function (arg) { return argNames_1.indexOf(arg.name.value) !== -1; });
|
1289 | if (args.length !== node.arguments.length) {
|
1290 | return tslib.__assign(tslib.__assign({}, node), { arguments: args });
|
1291 | }
|
1292 | }
|
1293 | }
|
1294 | },
|
1295 | leave: function (node) {
|
1296 | var _a;
|
1297 | var resolvedType = graphql.getNamedType(typeInfo.getType());
|
1298 | if (graphql.isObjectType(resolvedType) || graphql.isInterfaceType(resolvedType)) {
|
1299 | var selections = node.selectionSet != null ? node.selectionSet.selections : null;
|
1300 | if (selections == null || selections.length === 0) {
|
1301 |
|
1302 | graphql.visit(node, (_a = {},
|
1303 | _a[graphql.Kind.VARIABLE] = function (variableNode) {
|
1304 | var index = usedVariables.indexOf(variableNode.name.value);
|
1305 | if (index !== -1) {
|
1306 | usedVariables.splice(index, 1);
|
1307 | }
|
1308 | },
|
1309 | _a));
|
1310 | return null;
|
1311 | }
|
1312 | }
|
1313 | },
|
1314 | },
|
1315 | _a[graphql.Kind.FRAGMENT_SPREAD] = function (node) {
|
1316 | if (node.name.value in validFragments) {
|
1317 | var parentType = typeInfo.getParentType();
|
1318 | var innerType = validFragments[node.name.value];
|
1319 | if (!utils.implementsAbstractType(schema, parentType, innerType)) {
|
1320 | return null;
|
1321 | }
|
1322 | usedFragments.push(node.name.value);
|
1323 | return;
|
1324 | }
|
1325 | return null;
|
1326 | },
|
1327 | _a[graphql.Kind.INLINE_FRAGMENT] = {
|
1328 | enter: function (node) {
|
1329 | if (node.typeCondition != null) {
|
1330 | var parentType = typeInfo.getParentType();
|
1331 | var innerType = schema.getType(node.typeCondition.name.value);
|
1332 | if (!utils.implementsAbstractType(schema, parentType, innerType)) {
|
1333 | return null;
|
1334 | }
|
1335 | }
|
1336 | },
|
1337 | },
|
1338 | _a[graphql.Kind.VARIABLE] = function (node) {
|
1339 | usedVariables.push(node.name.value);
|
1340 | },
|
1341 | _a)));
|
1342 | return {
|
1343 | selectionSet: filteredSelectionSet,
|
1344 | usedFragments: usedFragments,
|
1345 | usedVariables: usedVariables,
|
1346 | };
|
1347 | }
|
1348 | function union() {
|
1349 | var arrays = [];
|
1350 | for (var _i = 0; _i < arguments.length; _i++) {
|
1351 | arrays[_i] = arguments[_i];
|
1352 | }
|
1353 | var cache = Object.create(null);
|
1354 | var result = [];
|
1355 | arrays.forEach(function (array) {
|
1356 | array.forEach(function (item) {
|
1357 | if (!(item in cache)) {
|
1358 | cache[item] = true;
|
1359 | result.push(item);
|
1360 | }
|
1361 | });
|
1362 | });
|
1363 | return result;
|
1364 | }
|
1365 |
|
1366 | var AddTypenameToAbstract = (function () {
|
1367 | function AddTypenameToAbstract() {
|
1368 | }
|
1369 | AddTypenameToAbstract.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
1370 | var document = addTypenameToAbstract(delegationContext.targetSchema, originalRequest.document);
|
1371 | return tslib.__assign(tslib.__assign({}, originalRequest), { document: document });
|
1372 | };
|
1373 | return AddTypenameToAbstract;
|
1374 | }());
|
1375 | function addTypenameToAbstract(targetSchema, document) {
|
1376 | var _a;
|
1377 | var typeInfo = new graphql.TypeInfo(targetSchema);
|
1378 | return graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, (_a = {},
|
1379 | _a[graphql.Kind.SELECTION_SET] = function (node) {
|
1380 | var parentType = typeInfo.getParentType();
|
1381 | var selections = node.selections;
|
1382 | if (parentType != null && graphql.isAbstractType(parentType)) {
|
1383 | selections = selections.concat({
|
1384 | kind: graphql.Kind.FIELD,
|
1385 | name: {
|
1386 | kind: graphql.Kind.NAME,
|
1387 | value: '__typename',
|
1388 | },
|
1389 | });
|
1390 | }
|
1391 | if (selections !== node.selections) {
|
1392 | return tslib.__assign(tslib.__assign({}, node), { selections: selections });
|
1393 | }
|
1394 | },
|
1395 | _a)));
|
1396 | }
|
1397 |
|
1398 | var CheckResultAndHandleErrors = (function () {
|
1399 | function CheckResultAndHandleErrors() {
|
1400 | }
|
1401 | CheckResultAndHandleErrors.prototype.transformResult = function (originalResult, delegationContext, _transformationContext) {
|
1402 | return checkResultAndHandleErrors(originalResult, delegationContext.context != null ? delegationContext.context : {}, delegationContext.info, delegationContext.fieldName, delegationContext.subschema, delegationContext.returnType, delegationContext.skipTypeMerging, delegationContext.onLocatedError);
|
1403 | };
|
1404 | return CheckResultAndHandleErrors;
|
1405 | }());
|
1406 | function checkResultAndHandleErrors(result, context, info, responseKey, subschema, returnType, skipTypeMerging, onLocatedError) {
|
1407 | if (responseKey === void 0) { responseKey = utils.getResponseKeyFromInfo(info); }
|
1408 | if (returnType === void 0) { returnType = info.returnType; }
|
1409 | var _a = mergeDataAndErrors(result.data == null ? undefined : result.data[responseKey], result.errors == null ? [] : result.errors, info ? graphql.responsePathAsArray(info.path) : undefined, onLocatedError), data = _a.data, unpathedErrors = _a.unpathedErrors;
|
1410 | return resolveExternalValue(data, unpathedErrors, subschema, context, info, returnType, skipTypeMerging);
|
1411 | }
|
1412 | function mergeDataAndErrors(data, errors, path, onLocatedError, index) {
|
1413 | if (index === void 0) { index = 1; }
|
1414 | if (data == null) {
|
1415 | if (!errors.length) {
|
1416 | return { data: null, unpathedErrors: [] };
|
1417 | }
|
1418 | if (errors.length === 1) {
|
1419 | var error = onLocatedError ? onLocatedError(errors[0]) : errors[0];
|
1420 | var newPath = path === undefined ? error.path : error.path === undefined ? path : path.concat(error.path.slice(1));
|
1421 | return { data: utils.relocatedError(errors[0], newPath), unpathedErrors: [] };
|
1422 | }
|
1423 | var newError = graphql.locatedError(new AggregateError(errors), undefined, path);
|
1424 | return { data: newError, unpathedErrors: [] };
|
1425 | }
|
1426 | if (!errors.length) {
|
1427 | return { data: data, unpathedErrors: [] };
|
1428 | }
|
1429 | var unpathedErrors = [];
|
1430 | var errorMap = Object.create(null);
|
1431 | errors.forEach(function (error) {
|
1432 | var _a;
|
1433 | var pathSegment = (_a = error.path) === null || _a === void 0 ? void 0 : _a[index];
|
1434 | if (pathSegment != null) {
|
1435 | var pathSegmentErrors = errorMap[pathSegment];
|
1436 | if (pathSegmentErrors === undefined) {
|
1437 | errorMap[pathSegment] = [error];
|
1438 | }
|
1439 | else {
|
1440 | pathSegmentErrors.push(error);
|
1441 | }
|
1442 | }
|
1443 | else {
|
1444 | unpathedErrors.push(error);
|
1445 | }
|
1446 | });
|
1447 | Object.keys(errorMap).forEach(function (pathSegment) {
|
1448 | if (data[pathSegment] !== undefined) {
|
1449 | var _a = mergeDataAndErrors(data[pathSegment], errorMap[pathSegment], path, onLocatedError, index + 1), newData = _a.data, newErrors = _a.unpathedErrors;
|
1450 | data[pathSegment] = newData;
|
1451 | unpathedErrors = unpathedErrors.concat(newErrors);
|
1452 | }
|
1453 | else {
|
1454 | unpathedErrors = unpathedErrors.concat(errorMap[pathSegment]);
|
1455 | }
|
1456 | });
|
1457 | return { data: data, unpathedErrors: unpathedErrors };
|
1458 | }
|
1459 |
|
1460 | var AddArgumentsAsVariables = (function () {
|
1461 | function AddArgumentsAsVariables(args) {
|
1462 | this.args = Object.entries(args).reduce(function (prev, _a) {
|
1463 | var _b;
|
1464 | var _c = tslib.__read(_a, 2), key = _c[0], val = _c[1];
|
1465 | return (tslib.__assign(tslib.__assign({}, prev), (_b = {}, _b[key] = val, _b)));
|
1466 | }, {});
|
1467 | }
|
1468 | AddArgumentsAsVariables.prototype.transformRequest = function (originalRequest, delegationContext, _transformationContext) {
|
1469 | var _a = addVariablesToRootField(delegationContext.targetSchema, originalRequest, this.args), document = _a.document, variables = _a.variables;
|
1470 | return tslib.__assign(tslib.__assign({}, originalRequest), { document: document,
|
1471 | variables: variables });
|
1472 | };
|
1473 | return AddArgumentsAsVariables;
|
1474 | }());
|
1475 | function addVariablesToRootField(targetSchema, originalRequest, args) {
|
1476 | var document = originalRequest.document;
|
1477 | var variableValues = originalRequest.variables;
|
1478 | var operations = document.definitions.filter(function (def) { return def.kind === graphql.Kind.OPERATION_DEFINITION; });
|
1479 | var fragments = document.definitions.filter(function (def) { return def.kind === graphql.Kind.FRAGMENT_DEFINITION; });
|
1480 | var newOperations = operations.map(function (operation) {
|
1481 | var variableDefinitionMap = operation.variableDefinitions.reduce(function (prev, def) {
|
1482 | var _a;
|
1483 | return (tslib.__assign(tslib.__assign({}, prev), (_a = {}, _a[def.variable.name.value] = def, _a)));
|
1484 | }, {});
|
1485 | var type;
|
1486 | if (operation.operation === 'subscription') {
|
1487 | type = targetSchema.getSubscriptionType();
|
1488 | }
|
1489 | else if (operation.operation === 'mutation') {
|
1490 | type = targetSchema.getMutationType();
|
1491 | }
|
1492 | else {
|
1493 | type = targetSchema.getQueryType();
|
1494 | }
|
1495 | var newSelectionSet = [];
|
1496 | operation.selectionSet.selections.forEach(function (selection) {
|
1497 | var _a;
|
1498 | if (selection.kind === graphql.Kind.FIELD) {
|
1499 | var argumentNodes = (_a = selection.arguments) !== null && _a !== void 0 ? _a : [];
|
1500 | var argumentNodeMap_1 = argumentNodes.reduce(function (prev, argument) {
|
1501 | var _a;
|
1502 | return (tslib.__assign(tslib.__assign({}, prev), (_a = {}, _a[argument.name.value] = argument, _a)));
|
1503 | }, {});
|
1504 | var targetField = type.getFields()[selection.name.value];
|
1505 |
|
1506 | if (targetField != null) {
|
1507 | updateArguments(targetField, argumentNodeMap_1, variableDefinitionMap, variableValues, args);
|
1508 | }
|
1509 | newSelectionSet.push(tslib.__assign(tslib.__assign({}, selection), { arguments: Object.keys(argumentNodeMap_1).map(function (argName) { return argumentNodeMap_1[argName]; }) }));
|
1510 | }
|
1511 | else {
|
1512 | newSelectionSet.push(selection);
|
1513 | }
|
1514 | });
|
1515 | return tslib.__assign(tslib.__assign({}, operation), { variableDefinitions: Object.keys(variableDefinitionMap).map(function (varName) { return variableDefinitionMap[varName]; }), selectionSet: {
|
1516 | kind: graphql.Kind.SELECTION_SET,
|
1517 | selections: newSelectionSet,
|
1518 | } });
|
1519 | });
|
1520 | return {
|
1521 | document: tslib.__assign(tslib.__assign({}, document), { definitions: tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(newOperations)), tslib.__read(fragments)) }),
|
1522 | variables: variableValues,
|
1523 | };
|
1524 | }
|
1525 | function updateArguments(targetField, argumentNodeMap, variableDefinitionMap, variableValues, newArgs) {
|
1526 | targetField.args.forEach(function (argument) {
|
1527 | var argName = argument.name;
|
1528 | var argType = argument.type;
|
1529 | if (argName in newArgs) {
|
1530 | utils.updateArgument(argName, argType, argumentNodeMap, variableDefinitionMap, variableValues, utils.serializeInputValue(argType, newArgs[argName]));
|
1531 | }
|
1532 | });
|
1533 | }
|
1534 |
|
1535 | function defaultDelegationBinding(delegationContext) {
|
1536 | var _a;
|
1537 | var delegationTransforms = [new CheckResultAndHandleErrors()];
|
1538 | var info = delegationContext.info;
|
1539 | var stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo;
|
1540 | if (stitchingInfo != null) {
|
1541 | delegationTransforms = delegationTransforms.concat([
|
1542 | new ExpandAbstractTypes(),
|
1543 | new AddSelectionSets(stitchingInfo.selectionSetsByType, stitchingInfo.selectionSetsByField, stitchingInfo.dynamicSelectionSetsByField),
|
1544 | new WrapConcreteTypes(),
|
1545 | ]);
|
1546 | }
|
1547 | else if (info != null) {
|
1548 | delegationTransforms = delegationTransforms.concat([new WrapConcreteTypes(), new ExpandAbstractTypes()]);
|
1549 | }
|
1550 | else {
|
1551 | delegationTransforms.push(new WrapConcreteTypes());
|
1552 | }
|
1553 | var transforms = delegationContext.transforms;
|
1554 | if (transforms != null) {
|
1555 | delegationTransforms = delegationTransforms.concat(transforms.slice().reverse());
|
1556 | }
|
1557 | var args = delegationContext.args;
|
1558 | if (args != null) {
|
1559 | delegationTransforms.push(new AddArgumentsAsVariables(args));
|
1560 | }
|
1561 | delegationTransforms = delegationTransforms.concat([new FilterToSchema(), new AddTypenameToAbstract()]);
|
1562 | return delegationTransforms;
|
1563 | }
|
1564 |
|
1565 | var Transformer = (function () {
|
1566 | function Transformer(context, binding) {
|
1567 | var _this = this;
|
1568 | if (binding === void 0) { binding = defaultDelegationBinding; }
|
1569 | this.transformations = [];
|
1570 | this.delegationContext = context;
|
1571 | var delegationTransforms = binding(this.delegationContext);
|
1572 | delegationTransforms.forEach(function (transform) { return _this.addTransform(transform, {}); });
|
1573 | }
|
1574 | Transformer.prototype.addTransform = function (transform, context) {
|
1575 | if (context === void 0) { context = {}; }
|
1576 | this.transformations.push({ transform: transform, context: context });
|
1577 | };
|
1578 | Transformer.prototype.transformRequest = function (originalRequest) {
|
1579 | var _this = this;
|
1580 | return this.transformations.reduce(function (request, transformation) {
|
1581 | return transformation.transform.transformRequest != null
|
1582 | ? transformation.transform.transformRequest(request, _this.delegationContext, transformation.context)
|
1583 | : request;
|
1584 | }, originalRequest);
|
1585 | };
|
1586 | Transformer.prototype.transformResult = function (originalResult) {
|
1587 | var _this = this;
|
1588 | return this.transformations.reduceRight(function (result, transformation) {
|
1589 | return transformation.transform.transformResult != null
|
1590 | ? transformation.transform.transformResult(result, _this.delegationContext, transformation.context)
|
1591 | : result;
|
1592 | }, originalResult);
|
1593 | };
|
1594 | return Transformer;
|
1595 | }());
|
1596 |
|
1597 | function delegateToSchema(options) {
|
1598 | var info = options.info, operationName = options.operationName, _a = options.operation, operation = _a === void 0 ? getDelegatingOperation(info.parentType, info.schema) : _a, _b = options.fieldName, fieldName = _b === void 0 ? info.fieldName : _b, _c = options.returnType, returnType = _c === void 0 ? info.returnType : _c, selectionSet = options.selectionSet, fieldNodes = options.fieldNodes;
|
1599 | var request = createRequestFromInfo({
|
1600 | info: info,
|
1601 | operation: operation,
|
1602 | fieldName: fieldName,
|
1603 | selectionSet: selectionSet,
|
1604 | fieldNodes: fieldNodes,
|
1605 | operationName: operationName,
|
1606 | });
|
1607 | return delegateRequest(tslib.__assign(tslib.__assign({}, options), { request: request,
|
1608 | operation: operation,
|
1609 | fieldName: fieldName,
|
1610 | returnType: returnType }));
|
1611 | }
|
1612 | function getDelegationReturnType(targetSchema, operation, fieldName) {
|
1613 | var rootType;
|
1614 | if (operation === 'query') {
|
1615 | rootType = targetSchema.getQueryType();
|
1616 | }
|
1617 | else if (operation === 'mutation') {
|
1618 | rootType = targetSchema.getMutationType();
|
1619 | }
|
1620 | else {
|
1621 | rootType = targetSchema.getSubscriptionType();
|
1622 | }
|
1623 | return rootType.getFields()[fieldName].type;
|
1624 | }
|
1625 | function delegateRequest(_a) {
|
1626 | var _b, _c, _d;
|
1627 | var request = _a.request, subschemaOrSubschemaConfig = _a.schema, rootValue = _a.rootValue, info = _a.info, operation = _a.operation, fieldName = _a.fieldName, args = _a.args, returnType = _a.returnType, onLocatedError = _a.onLocatedError, context = _a.context, _e = _a.transforms, transforms = _e === void 0 ? [] : _e, transformedSchema = _a.transformedSchema, skipValidation = _a.skipValidation, skipTypeMerging = _a.skipTypeMerging, binding = _a.binding;
|
1628 | var operationDefinition;
|
1629 | var targetOperation;
|
1630 | var targetFieldName;
|
1631 | if (operation == null) {
|
1632 | operationDefinition = graphql.getOperationAST(request.document, undefined);
|
1633 | targetOperation = operationDefinition.operation;
|
1634 | }
|
1635 | else {
|
1636 | targetOperation = operation;
|
1637 | }
|
1638 | if (fieldName == null) {
|
1639 | operationDefinition = operationDefinition !== null && operationDefinition !== void 0 ? operationDefinition : graphql.getOperationAST(request.document, undefined);
|
1640 | targetFieldName = operationDefinition.selectionSet.selections[0].name.value;
|
1641 | }
|
1642 | else {
|
1643 | targetFieldName = fieldName;
|
1644 | }
|
1645 | var _f = collectTargetParameters(subschemaOrSubschemaConfig, rootValue, info, transforms), targetSchema = _f.targetSchema, targetRootValue = _f.targetRootValue, subschemaConfig = _f.subschemaConfig, allTransforms = _f.allTransforms;
|
1646 | var delegationContext = {
|
1647 | subschema: subschemaOrSubschemaConfig,
|
1648 | targetSchema: targetSchema,
|
1649 | operation: targetOperation,
|
1650 | fieldName: targetFieldName,
|
1651 | args: args,
|
1652 | context: context,
|
1653 | info: info,
|
1654 | returnType: (_b = returnType !== null && returnType !== void 0 ? returnType : info === null || info === void 0 ? void 0 : info.returnType) !== null && _b !== void 0 ? _b : getDelegationReturnType(targetSchema, targetOperation, targetFieldName),
|
1655 | onLocatedError: onLocatedError,
|
1656 | transforms: allTransforms,
|
1657 | transformedSchema: (_d = transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : (_c = subschemaConfig) === null || _c === void 0 ? void 0 : _c.transformedSchema) !== null && _d !== void 0 ? _d : targetSchema,
|
1658 | skipTypeMerging: skipTypeMerging,
|
1659 | };
|
1660 | var transformer = new Transformer(delegationContext, binding);
|
1661 | var processedRequest = transformer.transformRequest(request);
|
1662 | if (!skipValidation) {
|
1663 | validateRequest(targetSchema, processedRequest.document);
|
1664 | }
|
1665 | if (targetOperation === 'query' || targetOperation === 'mutation') {
|
1666 | var executor_1 = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.executor) || createDefaultExecutor(targetSchema, (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) || targetRootValue);
|
1667 | if (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batch) {
|
1668 | var batchingOptions = subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batchingOptions;
|
1669 | executor_1 = batchExecute.getBatchingExecutor(context, executor_1, batchingOptions === null || batchingOptions === void 0 ? void 0 : batchingOptions.dataLoaderOptions, batchingOptions === null || batchingOptions === void 0 ? void 0 : batchingOptions.extensionsReducer);
|
1670 | }
|
1671 | return new valueOrPromise.ValueOrPromise(function () { return executor_1(tslib.__assign(tslib.__assign({}, processedRequest), { context: context,
|
1672 | info: info })); }).then(function (originalResult) { return transformer.transformResult(originalResult); }).resolve();
|
1673 | }
|
1674 | var subscriber = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.subscriber) || createDefaultSubscriber(targetSchema, (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) || targetRootValue);
|
1675 | return subscriber(tslib.__assign(tslib.__assign({}, processedRequest), { context: context,
|
1676 | info: info })).then(function (subscriptionResult) {
|
1677 | if (Symbol.asyncIterator in subscriptionResult) {
|
1678 |
|
1679 | return utils.mapAsyncIterator(subscriptionResult, function (originalResult) {
|
1680 | var _a;
|
1681 | return (_a = {},
|
1682 | _a[targetFieldName] = transformer.transformResult(originalResult),
|
1683 | _a);
|
1684 | });
|
1685 | }
|
1686 | return transformer.transformResult(subscriptionResult);
|
1687 | });
|
1688 | }
|
1689 | var emptyObject = {};
|
1690 | function collectTargetParameters(subschema, rootValue, info, transforms) {
|
1691 | var _a, _b, _c, _d, _e;
|
1692 | if (transforms === void 0) { transforms = []; }
|
1693 | var stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo;
|
1694 | var subschemaOrSubschemaConfig = (_b = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.subschemaMap.get(subschema)) !== null && _b !== void 0 ? _b : subschema;
|
1695 | if (isSubschemaConfig(subschemaOrSubschemaConfig)) {
|
1696 | return {
|
1697 | targetSchema: subschemaOrSubschemaConfig.schema,
|
1698 | targetRootValue: (_d = (_c = rootValue !== null && rootValue !== void 0 ? rootValue : subschemaOrSubschemaConfig === null || subschemaOrSubschemaConfig === void 0 ? void 0 : subschemaOrSubschemaConfig.rootValue) !== null && _c !== void 0 ? _c : info === null || info === void 0 ? void 0 : info.rootValue) !== null && _d !== void 0 ? _d : emptyObject,
|
1699 | subschemaConfig: subschemaOrSubschemaConfig,
|
1700 | allTransforms: subschemaOrSubschemaConfig.transforms != null
|
1701 | ? subschemaOrSubschemaConfig.transforms.concat(transforms)
|
1702 | : transforms,
|
1703 | };
|
1704 | }
|
1705 | return {
|
1706 | targetSchema: subschemaOrSubschemaConfig,
|
1707 | targetRootValue: (_e = rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue) !== null && _e !== void 0 ? _e : emptyObject,
|
1708 | allTransforms: transforms,
|
1709 | };
|
1710 | }
|
1711 | function validateRequest(targetSchema, document) {
|
1712 | var errors = graphql.validate(targetSchema, document);
|
1713 | if (errors.length > 0) {
|
1714 | if (errors.length > 1) {
|
1715 | var combinedError = new AggregateError(errors);
|
1716 | throw combinedError;
|
1717 | }
|
1718 | var error = errors[0];
|
1719 | throw error.originalError || error;
|
1720 | }
|
1721 | }
|
1722 | var createDefaultExecutor = memoize2(function (schema, rootValue) {
|
1723 | return (function (_a) {
|
1724 | var document = _a.document, context = _a.context, variables = _a.variables, info = _a.info;
|
1725 | return graphql.execute({
|
1726 | schema: schema,
|
1727 | document: document,
|
1728 | contextValue: context,
|
1729 | variableValues: variables,
|
1730 | rootValue: rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue,
|
1731 | });
|
1732 | });
|
1733 | });
|
1734 | function createDefaultSubscriber(schema, rootValue) {
|
1735 | return function (_a) {
|
1736 | var document = _a.document, context = _a.context, variables = _a.variables, info = _a.info;
|
1737 | return graphql.subscribe({
|
1738 | schema: schema,
|
1739 | document: document,
|
1740 | contextValue: context,
|
1741 | variableValues: variables,
|
1742 | rootValue: rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue,
|
1743 | });
|
1744 | };
|
1745 | }
|
1746 |
|
1747 | exports.AddArgumentsAsVariables = AddArgumentsAsVariables;
|
1748 | exports.AddSelectionSets = AddSelectionSets;
|
1749 | exports.AddTypenameToAbstract = AddTypenameToAbstract;
|
1750 | exports.CheckResultAndHandleErrors = CheckResultAndHandleErrors;
|
1751 | exports.ExpandAbstractTypes = ExpandAbstractTypes;
|
1752 | exports.FilterToSchema = FilterToSchema;
|
1753 | exports.Subschema = Subschema;
|
1754 | exports.VisitSelectionSets = VisitSelectionSets;
|
1755 | exports.annotateExternalObject = annotateExternalObject;
|
1756 | exports.applySchemaTransforms = applySchemaTransforms;
|
1757 | exports.checkResultAndHandleErrors = checkResultAndHandleErrors;
|
1758 | exports.cloneSubschemaConfig = cloneSubschemaConfig;
|
1759 | exports.createRequest = createRequest;
|
1760 | exports.createRequestFromInfo = createRequestFromInfo;
|
1761 | exports.defaultDelegationBinding = defaultDelegationBinding;
|
1762 | exports.defaultMergedResolver = defaultMergedResolver;
|
1763 | exports.delegateRequest = delegateRequest;
|
1764 | exports.delegateToSchema = delegateToSchema;
|
1765 | exports.getDelegatingOperation = getDelegatingOperation;
|
1766 | exports.getSubschema = getSubschema;
|
1767 | exports.getUnpathedErrors = getUnpathedErrors;
|
1768 | exports.isExternalObject = isExternalObject;
|
1769 | exports.isSubschema = isSubschema;
|
1770 | exports.isSubschemaConfig = isSubschemaConfig;
|
1771 | exports.mergeExternalObjects = mergeExternalObjects;
|
1772 | exports.resolveExternalValue = resolveExternalValue;
|
1773 |
|