@graphql-tools/delegate 8.7.12 → 8.8.0-alpha-b76ec274.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Subschema.js +22 -0
- package/cjs/Transformer.js +45 -0
- package/cjs/applySchemaTransforms.js +13 -0
- package/cjs/checkResultAndHandleErrors.js +77 -0
- package/cjs/createRequest.js +125 -0
- package/cjs/defaultMergedResolver.js +29 -0
- package/cjs/delegateToSchema.js +145 -0
- package/cjs/finalizeGatewayRequest.js +317 -0
- package/cjs/getDocumentMetadata.js +25 -0
- package/cjs/index.js +13 -0
- package/cjs/mergeFields.js +87 -0
- package/cjs/package.json +1 -0
- package/cjs/prepareGatewayDocument.js +311 -0
- package/cjs/resolveExternalValue.js +103 -0
- package/cjs/subschemaConfig.js +31 -0
- package/cjs/symbols.js +6 -0
- package/cjs/types.js +3 -0
- package/esm/Subschema.js +17 -0
- package/esm/Transformer.js +41 -0
- package/esm/applySchemaTransforms.js +9 -0
- package/esm/checkResultAndHandleErrors.js +72 -0
- package/esm/createRequest.js +120 -0
- package/esm/defaultMergedResolver.js +25 -0
- package/esm/delegateToSchema.js +140 -0
- package/esm/finalizeGatewayRequest.js +313 -0
- package/esm/getDocumentMetadata.js +21 -0
- package/esm/index.js +10 -0
- package/esm/mergeFields.js +79 -0
- package/esm/prepareGatewayDocument.js +307 -0
- package/esm/resolveExternalValue.js +99 -0
- package/esm/subschemaConfig.js +26 -0
- package/esm/symbols.js +3 -0
- package/esm/types.js +1 -0
- package/package.json +34 -13
- package/{Subschema.d.ts → typings/Subschema.d.ts} +1 -1
- package/{Transformer.d.ts → typings/Transformer.d.ts} +1 -1
- package/{applySchemaTransforms.d.ts → typings/applySchemaTransforms.d.ts} +1 -1
- package/{checkResultAndHandleErrors.d.ts → typings/checkResultAndHandleErrors.d.ts} +1 -1
- package/{createRequest.d.ts → typings/createRequest.d.ts} +1 -1
- package/{defaultMergedResolver.d.ts → typings/defaultMergedResolver.d.ts} +1 -1
- package/{delegateToSchema.d.ts → typings/delegateToSchema.d.ts} +1 -1
- package/{finalizeGatewayRequest.d.ts → typings/finalizeGatewayRequest.d.ts} +1 -1
- package/{getDocumentMetadata.d.ts → typings/getDocumentMetadata.d.ts} +0 -0
- package/typings/index.d.ts +10 -0
- package/{mergeFields.d.ts → typings/mergeFields.d.ts} +2 -2
- package/{prepareGatewayDocument.d.ts → typings/prepareGatewayDocument.d.ts} +0 -0
- package/{resolveExternalValue.d.ts → typings/resolveExternalValue.d.ts} +1 -1
- package/{subschemaConfig.d.ts → typings/subschemaConfig.d.ts} +1 -1
- package/{symbols.d.ts → typings/symbols.d.ts} +0 -0
- package/{types.d.ts → typings/types.d.ts} +2 -2
- package/README.md +0 -5
- package/index.d.ts +0 -10
- package/index.js +0 -1279
- package/index.mjs +0 -1258
package/index.mjs
DELETED
|
@@ -1,1258 +0,0 @@
|
|
|
1
|
-
import { Kind, isAbstractType, isInterfaceType, TypeInfo, visit, visitWithTypeInfo, getNamedType, isObjectType, versionInfo, TypeNameMetaFieldDef, responsePathAsArray, GraphQLError, locatedError, getNullableType, isCompositeType, isListType, typeFromAST, defaultFieldResolver, validate, subscribe, execute } from 'graphql';
|
|
2
|
-
import { memoize2, implementsAbstractType, getRootTypeNames, getDefinedRootType, createVariableNameGenerator, updateArgument, serializeInputValue, inspect, collectFields, relocatedError, AggregateError, getResponseKeyFromInfo, isAsyncIterable, mapAsyncIterator, getOperationASTFromRequest, memoize1 } from '@graphql-tools/utils';
|
|
3
|
-
import { ValueOrPromise } from 'value-or-promise';
|
|
4
|
-
import { getBatchingExecutor } from '@graphql-tools/batch-execute';
|
|
5
|
-
|
|
6
|
-
function applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
7
|
-
const schemaTransforms = subschemaConfig.transforms;
|
|
8
|
-
if (schemaTransforms == null) {
|
|
9
|
-
return originalWrappingSchema;
|
|
10
|
-
}
|
|
11
|
-
return schemaTransforms.reduce((schema, transform) => transform.transformSchema != null
|
|
12
|
-
? transform.transformSchema(schema, subschemaConfig, transformedSchema)
|
|
13
|
-
: schema, originalWrappingSchema);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function isSubschema(value) {
|
|
17
|
-
return Boolean(value.transformedSchema);
|
|
18
|
-
}
|
|
19
|
-
class Subschema {
|
|
20
|
-
constructor(config) {
|
|
21
|
-
var _a;
|
|
22
|
-
this.schema = config.schema;
|
|
23
|
-
this.executor = config.executor;
|
|
24
|
-
this.batch = config.batch;
|
|
25
|
-
this.batchingOptions = config.batchingOptions;
|
|
26
|
-
this.createProxyingResolver = config.createProxyingResolver;
|
|
27
|
-
this.transforms = (_a = config.transforms) !== null && _a !== void 0 ? _a : [];
|
|
28
|
-
this.transformedSchema = applySchemaTransforms(this.schema, config);
|
|
29
|
-
this.merge = config.merge;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function getDocumentMetadata(document) {
|
|
34
|
-
const operations = [];
|
|
35
|
-
const fragments = [];
|
|
36
|
-
const fragmentNames = new Set();
|
|
37
|
-
for (let i = 0; i < document.definitions.length; i++) {
|
|
38
|
-
const def = document.definitions[i];
|
|
39
|
-
if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
40
|
-
fragments.push(def);
|
|
41
|
-
fragmentNames.add(def.name.value);
|
|
42
|
-
}
|
|
43
|
-
else if (def.kind === Kind.OPERATION_DEFINITION) {
|
|
44
|
-
operations.push(def);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
operations,
|
|
49
|
-
fragments,
|
|
50
|
-
fragmentNames,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function prepareGatewayDocument(originalDocument, transformedSchema, returnType, infoSchema) {
|
|
55
|
-
const wrappedConcreteTypesDocument = wrapConcreteTypes(returnType, transformedSchema, originalDocument);
|
|
56
|
-
if (infoSchema == null) {
|
|
57
|
-
return wrappedConcreteTypesDocument;
|
|
58
|
-
}
|
|
59
|
-
const { possibleTypesMap, reversePossibleTypesMap, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField, } = getSchemaMetaData(infoSchema, transformedSchema);
|
|
60
|
-
const { operations, fragments, fragmentNames } = getDocumentMetadata(wrappedConcreteTypesDocument);
|
|
61
|
-
const { expandedFragments, fragmentReplacements } = getExpandedFragments(fragments, fragmentNames, possibleTypesMap);
|
|
62
|
-
const typeInfo = new TypeInfo(transformedSchema);
|
|
63
|
-
const expandedDocument = {
|
|
64
|
-
kind: Kind.DOCUMENT,
|
|
65
|
-
definitions: [...operations, ...fragments, ...expandedFragments],
|
|
66
|
-
};
|
|
67
|
-
const visitorKeyMap = {
|
|
68
|
-
Document: ['definitions'],
|
|
69
|
-
OperationDefinition: ['selectionSet'],
|
|
70
|
-
SelectionSet: ['selections'],
|
|
71
|
-
Field: ['selectionSet'],
|
|
72
|
-
InlineFragment: ['selectionSet'],
|
|
73
|
-
FragmentDefinition: ['selectionSet'],
|
|
74
|
-
};
|
|
75
|
-
return visit(expandedDocument, visitWithTypeInfo(typeInfo, {
|
|
76
|
-
[Kind.SELECTION_SET]: node => visitSelectionSet(node, fragmentReplacements, transformedSchema, typeInfo, possibleTypesMap, reversePossibleTypesMap, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField),
|
|
77
|
-
}),
|
|
78
|
-
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
|
|
79
|
-
// empty keys cannot be removed only because of typescript errors
|
|
80
|
-
// will hopefully be fixed in future version of graphql-js to be optional
|
|
81
|
-
visitorKeyMap);
|
|
82
|
-
}
|
|
83
|
-
function visitSelectionSet(node, fragmentReplacements, schema, typeInfo, possibleTypesMap, reversePossibleTypesMap, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField) {
|
|
84
|
-
var _a, _b;
|
|
85
|
-
const newSelections = new Set();
|
|
86
|
-
const maybeType = typeInfo.getParentType();
|
|
87
|
-
if (maybeType != null) {
|
|
88
|
-
const parentType = getNamedType(maybeType);
|
|
89
|
-
const parentTypeName = parentType.name;
|
|
90
|
-
const fieldNodes = fieldNodesByType[parentTypeName];
|
|
91
|
-
if (fieldNodes) {
|
|
92
|
-
for (const fieldNode of fieldNodes) {
|
|
93
|
-
newSelections.add(fieldNode);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
const interfaceExtensions = interfaceExtensionsMap[parentType.name];
|
|
97
|
-
const interfaceExtensionFields = [];
|
|
98
|
-
for (const selection of node.selections) {
|
|
99
|
-
if (selection.kind === Kind.INLINE_FRAGMENT) {
|
|
100
|
-
if (selection.typeCondition != null) {
|
|
101
|
-
const possibleTypes = possibleTypesMap[selection.typeCondition.name.value];
|
|
102
|
-
if (possibleTypes == null) {
|
|
103
|
-
newSelections.add(selection);
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
for (const possibleTypeName of possibleTypes) {
|
|
107
|
-
const maybePossibleType = schema.getType(possibleTypeName);
|
|
108
|
-
if (maybePossibleType != null && implementsAbstractType(schema, parentType, maybePossibleType)) {
|
|
109
|
-
newSelections.add(generateInlineFragment(possibleTypeName, selection.selectionSet));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
else if (selection.kind === Kind.FRAGMENT_SPREAD) {
|
|
115
|
-
const fragmentName = selection.name.value;
|
|
116
|
-
if (!fragmentReplacements[fragmentName]) {
|
|
117
|
-
newSelections.add(selection);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
for (const replacement of fragmentReplacements[fragmentName]) {
|
|
121
|
-
const typeName = replacement.typeName;
|
|
122
|
-
const maybeReplacementType = schema.getType(typeName);
|
|
123
|
-
if (maybeReplacementType != null && implementsAbstractType(schema, parentType, maybeType)) {
|
|
124
|
-
newSelections.add({
|
|
125
|
-
kind: Kind.FRAGMENT_SPREAD,
|
|
126
|
-
name: {
|
|
127
|
-
kind: Kind.NAME,
|
|
128
|
-
value: replacement.fragmentName,
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
const fieldName = selection.name.value;
|
|
136
|
-
const fieldNodes = (_a = fieldNodesByField[parentTypeName]) === null || _a === void 0 ? void 0 : _a[fieldName];
|
|
137
|
-
if (fieldNodes != null) {
|
|
138
|
-
for (const fieldNode of fieldNodes) {
|
|
139
|
-
newSelections.add(fieldNode);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
const dynamicSelectionSets = (_b = dynamicSelectionSetsByField[parentTypeName]) === null || _b === void 0 ? void 0 : _b[fieldName];
|
|
143
|
-
if (dynamicSelectionSets != null) {
|
|
144
|
-
for (const selectionSetFn of dynamicSelectionSets) {
|
|
145
|
-
const selectionSet = selectionSetFn(selection);
|
|
146
|
-
if (selectionSet != null) {
|
|
147
|
-
for (const selection of selectionSet.selections) {
|
|
148
|
-
newSelections.add(selection);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (interfaceExtensions === null || interfaceExtensions === void 0 ? void 0 : interfaceExtensions[fieldName]) {
|
|
154
|
-
interfaceExtensionFields.push(selection);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
newSelections.add(selection);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (reversePossibleTypesMap[parentType.name]) {
|
|
162
|
-
newSelections.add({
|
|
163
|
-
kind: Kind.FIELD,
|
|
164
|
-
name: {
|
|
165
|
-
kind: Kind.NAME,
|
|
166
|
-
value: '__typename',
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
if (interfaceExtensionFields.length) {
|
|
171
|
-
const possibleTypes = possibleTypesMap[parentType.name];
|
|
172
|
-
if (possibleTypes != null) {
|
|
173
|
-
for (const possibleType of possibleTypes) {
|
|
174
|
-
newSelections.add(generateInlineFragment(possibleType, {
|
|
175
|
-
kind: Kind.SELECTION_SET,
|
|
176
|
-
selections: interfaceExtensionFields,
|
|
177
|
-
}));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return {
|
|
182
|
-
...node,
|
|
183
|
-
selections: Array.from(newSelections),
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return node;
|
|
187
|
-
}
|
|
188
|
-
function generateInlineFragment(typeName, selectionSet) {
|
|
189
|
-
return {
|
|
190
|
-
kind: Kind.INLINE_FRAGMENT,
|
|
191
|
-
typeCondition: {
|
|
192
|
-
kind: Kind.NAMED_TYPE,
|
|
193
|
-
name: {
|
|
194
|
-
kind: Kind.NAME,
|
|
195
|
-
value: typeName,
|
|
196
|
-
},
|
|
197
|
-
},
|
|
198
|
-
selectionSet,
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
const getSchemaMetaData = memoize2((sourceSchema, targetSchema) => {
|
|
202
|
-
var _a, _b, _c, _d;
|
|
203
|
-
const typeMap = sourceSchema.getTypeMap();
|
|
204
|
-
const targetTypeMap = targetSchema.getTypeMap();
|
|
205
|
-
const possibleTypesMap = Object.create(null);
|
|
206
|
-
const interfaceExtensionsMap = Object.create(null);
|
|
207
|
-
for (const typeName in typeMap) {
|
|
208
|
-
const type = typeMap[typeName];
|
|
209
|
-
if (isAbstractType(type)) {
|
|
210
|
-
const targetType = targetTypeMap[typeName];
|
|
211
|
-
if (isInterfaceType(type) && isInterfaceType(targetType)) {
|
|
212
|
-
const targetTypeFields = targetType.getFields();
|
|
213
|
-
const sourceTypeFields = type.getFields();
|
|
214
|
-
const extensionFields = Object.create(null);
|
|
215
|
-
let isExtensionFieldsEmpty = true;
|
|
216
|
-
for (const fieldName in sourceTypeFields) {
|
|
217
|
-
if (!targetTypeFields[fieldName]) {
|
|
218
|
-
extensionFields[fieldName] = true;
|
|
219
|
-
isExtensionFieldsEmpty = false;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (!isExtensionFieldsEmpty) {
|
|
223
|
-
interfaceExtensionsMap[typeName] = extensionFields;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
if (interfaceExtensionsMap[typeName] || !isAbstractType(targetType)) {
|
|
227
|
-
const implementations = sourceSchema.getPossibleTypes(type);
|
|
228
|
-
possibleTypesMap[typeName] = [];
|
|
229
|
-
for (const impl of implementations) {
|
|
230
|
-
if (targetTypeMap[impl.name]) {
|
|
231
|
-
possibleTypesMap[typeName].push(impl.name);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
const stitchingInfo = (_a = sourceSchema.extensions) === null || _a === void 0 ? void 0 : _a['stitchingInfo'];
|
|
238
|
-
return {
|
|
239
|
-
possibleTypesMap,
|
|
240
|
-
reversePossibleTypesMap: reversePossibleTypesMap(possibleTypesMap),
|
|
241
|
-
interfaceExtensionsMap,
|
|
242
|
-
fieldNodesByType: (_b = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.fieldNodesByType) !== null && _b !== void 0 ? _b : {},
|
|
243
|
-
fieldNodesByField: (_c = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.fieldNodesByField) !== null && _c !== void 0 ? _c : {},
|
|
244
|
-
dynamicSelectionSetsByField: (_d = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.dynamicSelectionSetsByField) !== null && _d !== void 0 ? _d : {},
|
|
245
|
-
};
|
|
246
|
-
});
|
|
247
|
-
function reversePossibleTypesMap(possibleTypesMap) {
|
|
248
|
-
const result = Object.create(null);
|
|
249
|
-
for (const typeName in possibleTypesMap) {
|
|
250
|
-
const toTypeNames = possibleTypesMap[typeName];
|
|
251
|
-
for (const toTypeName of toTypeNames) {
|
|
252
|
-
if (!result[toTypeName]) {
|
|
253
|
-
result[toTypeName] = [];
|
|
254
|
-
}
|
|
255
|
-
result[toTypeName].push(typeName);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
function getExpandedFragments(fragments, fragmentNames, possibleTypesMap) {
|
|
261
|
-
let fragmentCounter = 0;
|
|
262
|
-
function generateFragmentName(typeName) {
|
|
263
|
-
let fragmentName;
|
|
264
|
-
do {
|
|
265
|
-
fragmentName = `_${typeName}_Fragment${fragmentCounter.toString()}`;
|
|
266
|
-
fragmentCounter++;
|
|
267
|
-
} while (fragmentNames.has(fragmentName));
|
|
268
|
-
return fragmentName;
|
|
269
|
-
}
|
|
270
|
-
const expandedFragments = [];
|
|
271
|
-
const fragmentReplacements = Object.create(null);
|
|
272
|
-
for (const fragment of fragments) {
|
|
273
|
-
const possibleTypes = possibleTypesMap[fragment.typeCondition.name.value];
|
|
274
|
-
if (possibleTypes != null) {
|
|
275
|
-
const fragmentName = fragment.name.value;
|
|
276
|
-
fragmentReplacements[fragmentName] = [];
|
|
277
|
-
for (const possibleTypeName of possibleTypes) {
|
|
278
|
-
const name = generateFragmentName(possibleTypeName);
|
|
279
|
-
fragmentNames.add(name);
|
|
280
|
-
expandedFragments.push({
|
|
281
|
-
kind: Kind.FRAGMENT_DEFINITION,
|
|
282
|
-
name: {
|
|
283
|
-
kind: Kind.NAME,
|
|
284
|
-
value: name,
|
|
285
|
-
},
|
|
286
|
-
typeCondition: {
|
|
287
|
-
kind: Kind.NAMED_TYPE,
|
|
288
|
-
name: {
|
|
289
|
-
kind: Kind.NAME,
|
|
290
|
-
value: possibleTypeName,
|
|
291
|
-
},
|
|
292
|
-
},
|
|
293
|
-
selectionSet: fragment.selectionSet,
|
|
294
|
-
});
|
|
295
|
-
fragmentReplacements[fragmentName].push({
|
|
296
|
-
fragmentName: name,
|
|
297
|
-
typeName: possibleTypeName,
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return {
|
|
303
|
-
expandedFragments,
|
|
304
|
-
fragmentReplacements,
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
function wrapConcreteTypes(returnType, targetSchema, document) {
|
|
308
|
-
const namedType = getNamedType(returnType);
|
|
309
|
-
if (!isObjectType(namedType)) {
|
|
310
|
-
return document;
|
|
311
|
-
}
|
|
312
|
-
const rootTypeNames = getRootTypeNames(targetSchema);
|
|
313
|
-
const typeInfo = new TypeInfo(targetSchema);
|
|
314
|
-
const visitorKeys = {
|
|
315
|
-
Document: ['definitions'],
|
|
316
|
-
OperationDefinition: ['selectionSet'],
|
|
317
|
-
SelectionSet: ['selections'],
|
|
318
|
-
InlineFragment: ['selectionSet'],
|
|
319
|
-
FragmentDefinition: ['selectionSet'],
|
|
320
|
-
};
|
|
321
|
-
return visit(document, visitWithTypeInfo(typeInfo, {
|
|
322
|
-
[Kind.FRAGMENT_DEFINITION]: (node) => {
|
|
323
|
-
const typeName = node.typeCondition.name.value;
|
|
324
|
-
if (!rootTypeNames.has(typeName)) {
|
|
325
|
-
return false;
|
|
326
|
-
}
|
|
327
|
-
},
|
|
328
|
-
[Kind.FIELD]: (node) => {
|
|
329
|
-
const type = typeInfo.getType();
|
|
330
|
-
if (type != null && isAbstractType(getNamedType(type))) {
|
|
331
|
-
return {
|
|
332
|
-
...node,
|
|
333
|
-
selectionSet: {
|
|
334
|
-
kind: Kind.SELECTION_SET,
|
|
335
|
-
selections: [
|
|
336
|
-
{
|
|
337
|
-
kind: Kind.INLINE_FRAGMENT,
|
|
338
|
-
typeCondition: {
|
|
339
|
-
kind: Kind.NAMED_TYPE,
|
|
340
|
-
name: {
|
|
341
|
-
kind: Kind.NAME,
|
|
342
|
-
value: namedType.name,
|
|
343
|
-
},
|
|
344
|
-
},
|
|
345
|
-
selectionSet: node.selectionSet,
|
|
346
|
-
},
|
|
347
|
-
],
|
|
348
|
-
},
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
},
|
|
352
|
-
}),
|
|
353
|
-
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
|
|
354
|
-
// empty keys cannot be removed only because of typescript errors
|
|
355
|
-
// will hopefully be fixed in future version of graphql-js to be optional
|
|
356
|
-
visitorKeys);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function finalizeGatewayDocument(targetSchema, fragments, operations) {
|
|
360
|
-
var _a;
|
|
361
|
-
let usedVariables = [];
|
|
362
|
-
let usedFragments = [];
|
|
363
|
-
const newOperations = [];
|
|
364
|
-
let newFragments = [];
|
|
365
|
-
const validFragments = [];
|
|
366
|
-
const validFragmentsWithType = Object.create(null);
|
|
367
|
-
for (const fragment of fragments) {
|
|
368
|
-
const typeName = fragment.typeCondition.name.value;
|
|
369
|
-
const type = targetSchema.getType(typeName);
|
|
370
|
-
if (type != null) {
|
|
371
|
-
validFragments.push(fragment);
|
|
372
|
-
validFragmentsWithType[fragment.name.value] = type;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
let fragmentSet = Object.create(null);
|
|
376
|
-
for (const operation of operations) {
|
|
377
|
-
const type = getDefinedRootType(targetSchema, operation.operation);
|
|
378
|
-
const { selectionSet, usedFragments: operationUsedFragments, usedVariables: operationUsedVariables, } = finalizeSelectionSet(targetSchema, type, validFragmentsWithType, operation.selectionSet);
|
|
379
|
-
usedFragments = union(usedFragments, operationUsedFragments);
|
|
380
|
-
const { usedVariables: collectedUsedVariables, newFragments: collectedNewFragments, fragmentSet: collectedFragmentSet, } = collectFragmentVariables(targetSchema, fragmentSet, validFragments, validFragmentsWithType, usedFragments);
|
|
381
|
-
const operationOrFragmentVariables = union(operationUsedVariables, collectedUsedVariables);
|
|
382
|
-
usedVariables = union(usedVariables, operationOrFragmentVariables);
|
|
383
|
-
newFragments = collectedNewFragments;
|
|
384
|
-
fragmentSet = collectedFragmentSet;
|
|
385
|
-
const variableDefinitions = ((_a = operation.variableDefinitions) !== null && _a !== void 0 ? _a : []).filter((variable) => operationOrFragmentVariables.indexOf(variable.variable.name.value) !== -1);
|
|
386
|
-
newOperations.push({
|
|
387
|
-
kind: Kind.OPERATION_DEFINITION,
|
|
388
|
-
operation: operation.operation,
|
|
389
|
-
name: operation.name,
|
|
390
|
-
directives: operation.directives,
|
|
391
|
-
variableDefinitions,
|
|
392
|
-
selectionSet,
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
const newDocument = {
|
|
396
|
-
kind: Kind.DOCUMENT,
|
|
397
|
-
definitions: [...newOperations, ...newFragments],
|
|
398
|
-
};
|
|
399
|
-
return {
|
|
400
|
-
usedVariables,
|
|
401
|
-
newDocument,
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
function finalizeGatewayRequest(originalRequest, delegationContext) {
|
|
405
|
-
let { document, variables } = originalRequest;
|
|
406
|
-
let { operations, fragments } = getDocumentMetadata(document);
|
|
407
|
-
const { targetSchema, args } = delegationContext;
|
|
408
|
-
if (args) {
|
|
409
|
-
const requestWithNewVariables = addVariablesToRootFields(targetSchema, operations, args);
|
|
410
|
-
operations = requestWithNewVariables.newOperations;
|
|
411
|
-
variables = Object.assign({}, variables !== null && variables !== void 0 ? variables : {}, requestWithNewVariables.newVariables);
|
|
412
|
-
}
|
|
413
|
-
const { usedVariables, newDocument } = finalizeGatewayDocument(targetSchema, fragments, operations);
|
|
414
|
-
const newVariables = {};
|
|
415
|
-
if (variables != null) {
|
|
416
|
-
for (const variableName of usedVariables) {
|
|
417
|
-
const variableValue = variables[variableName];
|
|
418
|
-
if (variableValue !== undefined) {
|
|
419
|
-
newVariables[variableName] = variableValue;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return {
|
|
424
|
-
...originalRequest,
|
|
425
|
-
document: newDocument,
|
|
426
|
-
variables: newVariables,
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
function addVariablesToRootFields(targetSchema, operations, args) {
|
|
430
|
-
const newVariables = Object.create(null);
|
|
431
|
-
const newOperations = operations.map((operation) => {
|
|
432
|
-
var _a, _b;
|
|
433
|
-
const variableDefinitionMap = ((_a = operation.variableDefinitions) !== null && _a !== void 0 ? _a : []).reduce((prev, def) => ({
|
|
434
|
-
...prev,
|
|
435
|
-
[def.variable.name.value]: def,
|
|
436
|
-
}), {});
|
|
437
|
-
const type = getDefinedRootType(targetSchema, operation.operation);
|
|
438
|
-
const newSelections = [];
|
|
439
|
-
for (const selection of operation.selectionSet.selections) {
|
|
440
|
-
if (selection.kind === Kind.FIELD) {
|
|
441
|
-
const argumentNodes = (_b = selection.arguments) !== null && _b !== void 0 ? _b : [];
|
|
442
|
-
const argumentNodeMap = argumentNodes.reduce((prev, argument) => ({
|
|
443
|
-
...prev,
|
|
444
|
-
[argument.name.value]: argument,
|
|
445
|
-
}), {});
|
|
446
|
-
const targetField = type.getFields()[selection.name.value];
|
|
447
|
-
// excludes __typename
|
|
448
|
-
if (targetField != null) {
|
|
449
|
-
updateArguments(targetField, argumentNodeMap, variableDefinitionMap, newVariables, args);
|
|
450
|
-
}
|
|
451
|
-
newSelections.push({
|
|
452
|
-
...selection,
|
|
453
|
-
arguments: Object.values(argumentNodeMap),
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
|
-
newSelections.push(selection);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
const newSelectionSet = {
|
|
461
|
-
kind: Kind.SELECTION_SET,
|
|
462
|
-
selections: newSelections,
|
|
463
|
-
};
|
|
464
|
-
return {
|
|
465
|
-
...operation,
|
|
466
|
-
variableDefinitions: Object.values(variableDefinitionMap),
|
|
467
|
-
selectionSet: newSelectionSet,
|
|
468
|
-
};
|
|
469
|
-
});
|
|
470
|
-
return {
|
|
471
|
-
newOperations,
|
|
472
|
-
newVariables,
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
function updateArguments(targetField, argumentNodeMap, variableDefinitionMap, variableValues, newArgs) {
|
|
476
|
-
const generateVariableName = createVariableNameGenerator(variableDefinitionMap);
|
|
477
|
-
for (const argument of targetField.args) {
|
|
478
|
-
const argName = argument.name;
|
|
479
|
-
const argType = argument.type;
|
|
480
|
-
if (argName in newArgs) {
|
|
481
|
-
updateArgument(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), argType, serializeInputValue(argType, newArgs[argName]));
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
function collectFragmentVariables(targetSchema, fragmentSet, validFragments, validFragmentsWithType, usedFragments) {
|
|
486
|
-
let remainingFragments = usedFragments.slice();
|
|
487
|
-
let usedVariables = [];
|
|
488
|
-
const newFragments = [];
|
|
489
|
-
while (remainingFragments.length !== 0) {
|
|
490
|
-
const nextFragmentName = remainingFragments.pop();
|
|
491
|
-
const fragment = validFragments.find(fr => fr.name.value === nextFragmentName);
|
|
492
|
-
if (fragment != null) {
|
|
493
|
-
const name = nextFragmentName;
|
|
494
|
-
const typeName = fragment.typeCondition.name.value;
|
|
495
|
-
const type = targetSchema.getType(typeName);
|
|
496
|
-
if (type == null) {
|
|
497
|
-
throw new Error(`Fragment reference type "${typeName}", but the type is not contained within the target schema.`);
|
|
498
|
-
}
|
|
499
|
-
const { selectionSet, usedFragments: fragmentUsedFragments, usedVariables: fragmentUsedVariables, } = finalizeSelectionSet(targetSchema, type, validFragmentsWithType, fragment.selectionSet);
|
|
500
|
-
remainingFragments = union(remainingFragments, fragmentUsedFragments);
|
|
501
|
-
usedVariables = union(usedVariables, fragmentUsedVariables);
|
|
502
|
-
if (name && !(name in fragmentSet)) {
|
|
503
|
-
fragmentSet[name] = true;
|
|
504
|
-
newFragments.push({
|
|
505
|
-
kind: Kind.FRAGMENT_DEFINITION,
|
|
506
|
-
name: {
|
|
507
|
-
kind: Kind.NAME,
|
|
508
|
-
value: name,
|
|
509
|
-
},
|
|
510
|
-
typeCondition: fragment.typeCondition,
|
|
511
|
-
selectionSet,
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return {
|
|
517
|
-
usedVariables,
|
|
518
|
-
newFragments,
|
|
519
|
-
fragmentSet,
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
const filteredSelectionSetVisitorKeys = {
|
|
523
|
-
SelectionSet: ['selections'],
|
|
524
|
-
Field: ['selectionSet'],
|
|
525
|
-
InlineFragment: ['selectionSet'],
|
|
526
|
-
FragmentDefinition: ['selectionSet'],
|
|
527
|
-
};
|
|
528
|
-
const variablesVisitorKeys = {
|
|
529
|
-
SelectionSet: ['selections'],
|
|
530
|
-
Field: ['arguments', 'directives', 'selectionSet'],
|
|
531
|
-
Argument: ['value'],
|
|
532
|
-
InlineFragment: ['directives', 'selectionSet'],
|
|
533
|
-
FragmentSpread: ['directives'],
|
|
534
|
-
FragmentDefinition: ['selectionSet'],
|
|
535
|
-
ObjectValue: ['fields'],
|
|
536
|
-
ObjectField: ['name', 'value'],
|
|
537
|
-
Directive: ['arguments'],
|
|
538
|
-
ListValue: ['values'],
|
|
539
|
-
};
|
|
540
|
-
function finalizeSelectionSet(schema, type, validFragments, selectionSet) {
|
|
541
|
-
const usedFragments = [];
|
|
542
|
-
const usedVariables = [];
|
|
543
|
-
const typeInfo = versionInfo.major < 16 ? new TypeInfo(schema, undefined, type) : new TypeInfo(schema, type);
|
|
544
|
-
const filteredSelectionSet = visit(selectionSet, visitWithTypeInfo(typeInfo, {
|
|
545
|
-
[Kind.FIELD]: {
|
|
546
|
-
enter: node => {
|
|
547
|
-
const parentType = typeInfo.getParentType();
|
|
548
|
-
if (isObjectType(parentType) || isInterfaceType(parentType)) {
|
|
549
|
-
const fields = parentType.getFields();
|
|
550
|
-
const field = node.name.value === '__typename' ? TypeNameMetaFieldDef : fields[node.name.value];
|
|
551
|
-
if (!field) {
|
|
552
|
-
return null;
|
|
553
|
-
}
|
|
554
|
-
const args = field.args != null ? field.args : [];
|
|
555
|
-
const argsMap = Object.create(null);
|
|
556
|
-
for (const arg of args) {
|
|
557
|
-
argsMap[arg.name] = arg;
|
|
558
|
-
}
|
|
559
|
-
if (node.arguments != null) {
|
|
560
|
-
const newArgs = [];
|
|
561
|
-
for (const arg of node.arguments) {
|
|
562
|
-
if (arg.name.value in argsMap) {
|
|
563
|
-
newArgs.push(arg);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
if (newArgs.length !== node.arguments.length) {
|
|
567
|
-
return {
|
|
568
|
-
...node,
|
|
569
|
-
arguments: newArgs,
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
},
|
|
575
|
-
leave: node => {
|
|
576
|
-
const type = typeInfo.getType();
|
|
577
|
-
if (type == null) {
|
|
578
|
-
throw new Error(`No type was found for field node ${inspect(node)}.`);
|
|
579
|
-
}
|
|
580
|
-
const namedType = getNamedType(type);
|
|
581
|
-
if (!schema.getType(namedType.name) == null) {
|
|
582
|
-
return null;
|
|
583
|
-
}
|
|
584
|
-
if (isObjectType(namedType) || isInterfaceType(namedType)) {
|
|
585
|
-
const selections = node.selectionSet != null ? node.selectionSet.selections : null;
|
|
586
|
-
if (selections == null || selections.length === 0) {
|
|
587
|
-
return null;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
},
|
|
591
|
-
},
|
|
592
|
-
[Kind.FRAGMENT_SPREAD]: {
|
|
593
|
-
enter: node => {
|
|
594
|
-
if (!(node.name.value in validFragments)) {
|
|
595
|
-
return null;
|
|
596
|
-
}
|
|
597
|
-
const parentType = typeInfo.getParentType();
|
|
598
|
-
const innerType = validFragments[node.name.value];
|
|
599
|
-
if (!implementsAbstractType(schema, parentType, innerType)) {
|
|
600
|
-
return null;
|
|
601
|
-
}
|
|
602
|
-
usedFragments.push(node.name.value);
|
|
603
|
-
},
|
|
604
|
-
},
|
|
605
|
-
[Kind.INLINE_FRAGMENT]: {
|
|
606
|
-
enter: node => {
|
|
607
|
-
if (node.typeCondition != null) {
|
|
608
|
-
const parentType = typeInfo.getParentType();
|
|
609
|
-
const innerType = schema.getType(node.typeCondition.name.value);
|
|
610
|
-
if (!implementsAbstractType(schema, parentType, innerType)) {
|
|
611
|
-
return null;
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
},
|
|
615
|
-
},
|
|
616
|
-
[Kind.SELECTION_SET]: {
|
|
617
|
-
leave: node => {
|
|
618
|
-
const parentType = typeInfo.getParentType();
|
|
619
|
-
if (parentType != null && isAbstractType(parentType)) {
|
|
620
|
-
const selections = node.selections.concat([
|
|
621
|
-
{
|
|
622
|
-
kind: Kind.FIELD,
|
|
623
|
-
name: {
|
|
624
|
-
kind: Kind.NAME,
|
|
625
|
-
value: '__typename',
|
|
626
|
-
},
|
|
627
|
-
},
|
|
628
|
-
]);
|
|
629
|
-
return {
|
|
630
|
-
...node,
|
|
631
|
-
selections,
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
},
|
|
635
|
-
},
|
|
636
|
-
}),
|
|
637
|
-
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
|
|
638
|
-
// empty keys cannot be removed only because of typescript errors
|
|
639
|
-
// will hopefully be fixed in future version of graphql-js to be optional
|
|
640
|
-
filteredSelectionSetVisitorKeys);
|
|
641
|
-
visit(filteredSelectionSet, {
|
|
642
|
-
[Kind.VARIABLE]: variableNode => {
|
|
643
|
-
usedVariables.push(variableNode.name.value);
|
|
644
|
-
},
|
|
645
|
-
},
|
|
646
|
-
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
|
|
647
|
-
// empty keys cannot be removed only because of typescript errors
|
|
648
|
-
// will hopefully be fixed in future version of graphql-js to be optional
|
|
649
|
-
variablesVisitorKeys);
|
|
650
|
-
return {
|
|
651
|
-
selectionSet: filteredSelectionSet,
|
|
652
|
-
usedFragments,
|
|
653
|
-
usedVariables,
|
|
654
|
-
};
|
|
655
|
-
}
|
|
656
|
-
function union(...arrays) {
|
|
657
|
-
const cache = Object.create(null);
|
|
658
|
-
const result = [];
|
|
659
|
-
for (const array of arrays) {
|
|
660
|
-
for (const item of array) {
|
|
661
|
-
if (!(item in cache)) {
|
|
662
|
-
cache[item] = true;
|
|
663
|
-
result.push(item);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
return result;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
const UNPATHED_ERRORS_SYMBOL = Symbol('subschemaErrors');
|
|
671
|
-
const OBJECT_SUBSCHEMA_SYMBOL = Symbol('initialSubschema');
|
|
672
|
-
const FIELD_SUBSCHEMA_MAP_SYMBOL = Symbol('subschemaMap');
|
|
673
|
-
|
|
674
|
-
function isExternalObject(data) {
|
|
675
|
-
return data[UNPATHED_ERRORS_SYMBOL] !== undefined;
|
|
676
|
-
}
|
|
677
|
-
function annotateExternalObject(object, errors, subschema, subschemaMap) {
|
|
678
|
-
Object.defineProperties(object, {
|
|
679
|
-
[OBJECT_SUBSCHEMA_SYMBOL]: { value: subschema },
|
|
680
|
-
[FIELD_SUBSCHEMA_MAP_SYMBOL]: { value: subschemaMap },
|
|
681
|
-
[UNPATHED_ERRORS_SYMBOL]: { value: errors },
|
|
682
|
-
});
|
|
683
|
-
return object;
|
|
684
|
-
}
|
|
685
|
-
function getSubschema(object, responseKey) {
|
|
686
|
-
var _a;
|
|
687
|
-
return (_a = object[FIELD_SUBSCHEMA_MAP_SYMBOL][responseKey]) !== null && _a !== void 0 ? _a : object[OBJECT_SUBSCHEMA_SYMBOL];
|
|
688
|
-
}
|
|
689
|
-
function getUnpathedErrors(object) {
|
|
690
|
-
return object[UNPATHED_ERRORS_SYMBOL];
|
|
691
|
-
}
|
|
692
|
-
const EMPTY_ARRAY = [];
|
|
693
|
-
const EMPTY_OBJECT = Object.create(null);
|
|
694
|
-
async function mergeFields(mergedTypeInfo, object, sourceSubschema, context, info) {
|
|
695
|
-
var _a;
|
|
696
|
-
const delegationMaps = mergedTypeInfo.delegationPlanBuilder(info.schema, sourceSubschema, info.variableValues != null && Object.keys(info.variableValues).length > 0 ? info.variableValues : EMPTY_OBJECT, info.fragments != null && Object.keys(info.fragments).length > 0 ? info.fragments : EMPTY_OBJECT, ((_a = info.fieldNodes) === null || _a === void 0 ? void 0 : _a.length) ? info.fieldNodes : EMPTY_ARRAY);
|
|
697
|
-
for (const delegationMap of delegationMaps) {
|
|
698
|
-
await executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info);
|
|
699
|
-
}
|
|
700
|
-
return object;
|
|
701
|
-
}
|
|
702
|
-
async function executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info) {
|
|
703
|
-
const combinedErrors = object[UNPATHED_ERRORS_SYMBOL];
|
|
704
|
-
const path = responsePathAsArray(info.path);
|
|
705
|
-
const combinedFieldSubschemaMap = object[FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
706
|
-
await Promise.all([...delegationMap.entries()].map(async ([subschema, selectionSet]) => {
|
|
707
|
-
var _a;
|
|
708
|
-
const schema = subschema.transformedSchema || info.schema;
|
|
709
|
-
const type = schema.getType(object.__typename);
|
|
710
|
-
const resolver = mergedTypeInfo.resolvers.get(subschema);
|
|
711
|
-
if (resolver) {
|
|
712
|
-
let source;
|
|
713
|
-
try {
|
|
714
|
-
source = await resolver(object, context, info, subschema, selectionSet, undefined, type);
|
|
715
|
-
}
|
|
716
|
-
catch (error) {
|
|
717
|
-
source = error;
|
|
718
|
-
}
|
|
719
|
-
if (source instanceof Error || source == null) {
|
|
720
|
-
const fieldNodeResponseKeyMap = collectFields(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet, new Map(), new Set());
|
|
721
|
-
const nullResult = {};
|
|
722
|
-
for (const [responseKey, fieldNodes] of fieldNodeResponseKeyMap) {
|
|
723
|
-
const combinedPath = [...path, responseKey];
|
|
724
|
-
if (source instanceof GraphQLError) {
|
|
725
|
-
nullResult[responseKey] = relocatedError(source, combinedPath);
|
|
726
|
-
}
|
|
727
|
-
else if (source instanceof Error) {
|
|
728
|
-
nullResult[responseKey] = locatedError(source, fieldNodes, combinedPath);
|
|
729
|
-
}
|
|
730
|
-
else {
|
|
731
|
-
nullResult[responseKey] = null;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
source = nullResult;
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
if (source[UNPATHED_ERRORS_SYMBOL]) {
|
|
738
|
-
combinedErrors.push(...source[UNPATHED_ERRORS_SYMBOL]);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
const objectSubschema = source[OBJECT_SUBSCHEMA_SYMBOL];
|
|
742
|
-
const fieldSubschemaMap = source[FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
743
|
-
for (const responseKey in source) {
|
|
744
|
-
object[responseKey] = source[responseKey];
|
|
745
|
-
combinedFieldSubschemaMap[responseKey] = (_a = fieldSubschemaMap === null || fieldSubschemaMap === void 0 ? void 0 : fieldSubschemaMap[responseKey]) !== null && _a !== void 0 ? _a : objectSubschema;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
}));
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
function resolveExternalValue(result, unpathedErrors, subschema, context, info, returnType = getReturnType(info), skipTypeMerging) {
|
|
752
|
-
const type = getNullableType(returnType);
|
|
753
|
-
if (result instanceof Error) {
|
|
754
|
-
return result;
|
|
755
|
-
}
|
|
756
|
-
if (result == null) {
|
|
757
|
-
return reportUnpathedErrorsViaNull(unpathedErrors);
|
|
758
|
-
}
|
|
759
|
-
if ('parseValue' in type) {
|
|
760
|
-
return type.parseValue(result);
|
|
761
|
-
}
|
|
762
|
-
else if (isCompositeType(type)) {
|
|
763
|
-
return resolveExternalObject(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
|
|
764
|
-
}
|
|
765
|
-
else if (isListType(type)) {
|
|
766
|
-
return resolveExternalList(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
function resolveExternalObject(type, object, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
|
770
|
-
var _a;
|
|
771
|
-
// if we have already resolved this object, for example, when the identical object appears twice
|
|
772
|
-
// in a list, see https://github.com/ardatan/graphql-tools/issues/2304
|
|
773
|
-
if (!isExternalObject(object)) {
|
|
774
|
-
annotateExternalObject(object, unpathedErrors, subschema, Object.create(null));
|
|
775
|
-
}
|
|
776
|
-
if (skipTypeMerging || info == null) {
|
|
777
|
-
return object;
|
|
778
|
-
}
|
|
779
|
-
const stitchingInfo = (_a = info.schema.extensions) === null || _a === void 0 ? void 0 : _a['stitchingInfo'];
|
|
780
|
-
if (stitchingInfo == null) {
|
|
781
|
-
return object;
|
|
782
|
-
}
|
|
783
|
-
const typeName = isAbstractType(type) ? object.__typename : type.name;
|
|
784
|
-
const mergedTypeInfo = stitchingInfo.mergedTypes[typeName];
|
|
785
|
-
let targetSubschemas;
|
|
786
|
-
// Within the stitching context, delegation to a stitched GraphQLSchema or SubschemaConfig
|
|
787
|
-
// will be redirected to the appropriate Subschema object, from which merge targets can be queried.
|
|
788
|
-
if (mergedTypeInfo != null) {
|
|
789
|
-
targetSubschemas = mergedTypeInfo.targetSubschemas.get(subschema);
|
|
790
|
-
}
|
|
791
|
-
// If there are no merge targets from the subschema, return.
|
|
792
|
-
if (!targetSubschemas || !targetSubschemas.length) {
|
|
793
|
-
return object;
|
|
794
|
-
}
|
|
795
|
-
return mergeFields(mergedTypeInfo, object, subschema, context, info);
|
|
796
|
-
}
|
|
797
|
-
function resolveExternalList(type, list, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
|
798
|
-
return list.map(listMember => resolveExternalListMember(getNullableType(type.ofType), listMember, unpathedErrors, subschema, context, info, skipTypeMerging));
|
|
799
|
-
}
|
|
800
|
-
function resolveExternalListMember(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
|
801
|
-
if (listMember instanceof Error) {
|
|
802
|
-
return listMember;
|
|
803
|
-
}
|
|
804
|
-
if (listMember == null) {
|
|
805
|
-
return reportUnpathedErrorsViaNull(unpathedErrors);
|
|
806
|
-
}
|
|
807
|
-
if ('parseValue' in type) {
|
|
808
|
-
return type.parseValue(listMember);
|
|
809
|
-
}
|
|
810
|
-
else if (isCompositeType(type)) {
|
|
811
|
-
return resolveExternalObject(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
|
|
812
|
-
}
|
|
813
|
-
else if (isListType(type)) {
|
|
814
|
-
return resolveExternalList(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
const reportedErrors = new WeakMap();
|
|
818
|
-
function reportUnpathedErrorsViaNull(unpathedErrors) {
|
|
819
|
-
if (unpathedErrors.length) {
|
|
820
|
-
const unreportedErrors = [];
|
|
821
|
-
for (const error of unpathedErrors) {
|
|
822
|
-
if (!reportedErrors.has(error)) {
|
|
823
|
-
unreportedErrors.push(error);
|
|
824
|
-
reportedErrors.set(error, true);
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
if (unreportedErrors.length) {
|
|
828
|
-
if (unreportedErrors.length === 1) {
|
|
829
|
-
return unreportedErrors[0];
|
|
830
|
-
}
|
|
831
|
-
const combinedError = new AggregateError(unreportedErrors, unreportedErrors.map(error => error.message).join(', \n'));
|
|
832
|
-
// We cast path as any for GraphQL.js 14 compat
|
|
833
|
-
// locatedError path argument must be defined, but it is just forwarded to a constructor that allows a undefined value
|
|
834
|
-
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/locatedError.js#L25
|
|
835
|
-
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/GraphQLError.js#L19
|
|
836
|
-
return locatedError(combinedError, undefined, unreportedErrors[0].path);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
return null;
|
|
840
|
-
}
|
|
841
|
-
function getReturnType(info) {
|
|
842
|
-
if (info == null) {
|
|
843
|
-
throw new Error(`Return type cannot be inferred without a source schema.`);
|
|
844
|
-
}
|
|
845
|
-
return info.returnType;
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
function checkResultAndHandleErrors(result, delegationContext) {
|
|
849
|
-
const { context, info, fieldName: responseKey = getResponseKey(info), subschema, returnType = getReturnType$1(info), skipTypeMerging, onLocatedError, } = delegationContext;
|
|
850
|
-
const { data, unpathedErrors } = mergeDataAndErrors(result.data == null ? undefined : result.data[responseKey], result.errors == null ? [] : result.errors, info != null && info.path ? responsePathAsArray(info.path) : undefined, onLocatedError);
|
|
851
|
-
return resolveExternalValue(data, unpathedErrors, subschema, context, info, returnType, skipTypeMerging);
|
|
852
|
-
}
|
|
853
|
-
function mergeDataAndErrors(data, errors, path, onLocatedError, index = 1) {
|
|
854
|
-
var _a;
|
|
855
|
-
if (data == null) {
|
|
856
|
-
if (!errors.length) {
|
|
857
|
-
return { data: null, unpathedErrors: [] };
|
|
858
|
-
}
|
|
859
|
-
if (errors.length === 1) {
|
|
860
|
-
const error = onLocatedError ? onLocatedError(errors[0]) : errors[0];
|
|
861
|
-
const newPath = path === undefined ? error.path : !error.path ? path : path.concat(error.path.slice(1));
|
|
862
|
-
return { data: relocatedError(errors[0], newPath), unpathedErrors: [] };
|
|
863
|
-
}
|
|
864
|
-
// We cast path as any for GraphQL.js 14 compat
|
|
865
|
-
// locatedError path argument must be defined, but it is just forwarded to a constructor that allows a undefined value
|
|
866
|
-
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/locatedError.js#L25
|
|
867
|
-
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/GraphQLError.js#L19
|
|
868
|
-
const combinedError = new AggregateError(errors, errors.map(error => error.message).join(', \n'));
|
|
869
|
-
const newError = locatedError(combinedError, undefined, path);
|
|
870
|
-
return { data: newError, unpathedErrors: [] };
|
|
871
|
-
}
|
|
872
|
-
if (!errors.length) {
|
|
873
|
-
return { data, unpathedErrors: [] };
|
|
874
|
-
}
|
|
875
|
-
const unpathedErrors = [];
|
|
876
|
-
const errorMap = new Map();
|
|
877
|
-
for (const error of errors) {
|
|
878
|
-
const pathSegment = (_a = error.path) === null || _a === void 0 ? void 0 : _a[index];
|
|
879
|
-
if (pathSegment != null) {
|
|
880
|
-
let pathSegmentErrors = errorMap.get(pathSegment);
|
|
881
|
-
if (pathSegmentErrors === undefined) {
|
|
882
|
-
pathSegmentErrors = [error];
|
|
883
|
-
errorMap.set(pathSegment, pathSegmentErrors);
|
|
884
|
-
}
|
|
885
|
-
else {
|
|
886
|
-
pathSegmentErrors.push(error);
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
else {
|
|
890
|
-
unpathedErrors.push(error);
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
for (const [pathSegment, pathSegmentErrors] of errorMap) {
|
|
894
|
-
if (data[pathSegment] !== undefined) {
|
|
895
|
-
const { data: newData, unpathedErrors: newErrors } = mergeDataAndErrors(data[pathSegment], pathSegmentErrors, path, onLocatedError, index + 1);
|
|
896
|
-
data[pathSegment] = newData;
|
|
897
|
-
unpathedErrors.push(...newErrors);
|
|
898
|
-
}
|
|
899
|
-
else {
|
|
900
|
-
unpathedErrors.push(...pathSegmentErrors);
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return { data, unpathedErrors };
|
|
904
|
-
}
|
|
905
|
-
function getResponseKey(info) {
|
|
906
|
-
if (info == null) {
|
|
907
|
-
throw new Error(`Data cannot be extracted from result without an explicit key or source schema.`);
|
|
908
|
-
}
|
|
909
|
-
return getResponseKeyFromInfo(info);
|
|
910
|
-
}
|
|
911
|
-
function getReturnType$1(info) {
|
|
912
|
-
if (info == null) {
|
|
913
|
-
throw new Error(`Return type cannot be inferred without a source schema.`);
|
|
914
|
-
}
|
|
915
|
-
return info.returnType;
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
class Transformer {
|
|
919
|
-
constructor(context) {
|
|
920
|
-
this.transformations = [];
|
|
921
|
-
this.delegationContext = context;
|
|
922
|
-
const transforms = context.transforms;
|
|
923
|
-
const delegationTransforms = transforms.slice().reverse();
|
|
924
|
-
for (const transform of delegationTransforms) {
|
|
925
|
-
this.addTransform(transform, {});
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
addTransform(transform, context = {}) {
|
|
929
|
-
this.transformations.push({ transform, context });
|
|
930
|
-
}
|
|
931
|
-
transformRequest(originalRequest) {
|
|
932
|
-
var _a;
|
|
933
|
-
let request = {
|
|
934
|
-
...originalRequest,
|
|
935
|
-
document: prepareGatewayDocument(originalRequest.document, this.delegationContext.transformedSchema, this.delegationContext.returnType, (_a = this.delegationContext.info) === null || _a === void 0 ? void 0 : _a.schema),
|
|
936
|
-
};
|
|
937
|
-
for (const transformation of this.transformations) {
|
|
938
|
-
if (transformation.transform.transformRequest) {
|
|
939
|
-
request = transformation.transform.transformRequest(request, this.delegationContext, transformation.context);
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
return finalizeGatewayRequest(request, this.delegationContext);
|
|
943
|
-
}
|
|
944
|
-
transformResult(originalResult) {
|
|
945
|
-
let result = originalResult;
|
|
946
|
-
// from right to left
|
|
947
|
-
for (let i = this.transformations.length - 1; i >= 0; i--) {
|
|
948
|
-
const transformation = this.transformations[i];
|
|
949
|
-
if (transformation.transform.transformResult) {
|
|
950
|
-
result = transformation.transform.transformResult(result, this.delegationContext, transformation.context);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
return checkResultAndHandleErrors(result, this.delegationContext);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
function getDelegatingOperation(parentType, schema) {
|
|
958
|
-
if (parentType === schema.getMutationType()) {
|
|
959
|
-
return 'mutation';
|
|
960
|
-
}
|
|
961
|
-
else if (parentType === schema.getSubscriptionType()) {
|
|
962
|
-
return 'subscription';
|
|
963
|
-
}
|
|
964
|
-
return 'query';
|
|
965
|
-
}
|
|
966
|
-
function createRequest({ sourceSchema, sourceParentType, sourceFieldName, fragments, variableDefinitions, variableValues, targetRootValue, targetOperationName, targetOperation, targetFieldName, selectionSet, fieldNodes, context, info, }) {
|
|
967
|
-
var _a, _b;
|
|
968
|
-
let newSelectionSet;
|
|
969
|
-
const argumentNodeMap = Object.create(null);
|
|
970
|
-
if (selectionSet != null) {
|
|
971
|
-
newSelectionSet = selectionSet;
|
|
972
|
-
}
|
|
973
|
-
else {
|
|
974
|
-
const selections = [];
|
|
975
|
-
for (const fieldNode of fieldNodes || []) {
|
|
976
|
-
if (fieldNode.selectionSet) {
|
|
977
|
-
for (const selection of fieldNode.selectionSet.selections) {
|
|
978
|
-
selections.push(selection);
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
newSelectionSet = selections.length
|
|
983
|
-
? {
|
|
984
|
-
kind: Kind.SELECTION_SET,
|
|
985
|
-
selections,
|
|
986
|
-
}
|
|
987
|
-
: undefined;
|
|
988
|
-
const args = (_a = fieldNodes === null || fieldNodes === void 0 ? void 0 : fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments;
|
|
989
|
-
if (args) {
|
|
990
|
-
for (const argNode of args) {
|
|
991
|
-
argumentNodeMap[argNode.name.value] = argNode;
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
const newVariables = Object.create(null);
|
|
996
|
-
const variableDefinitionMap = Object.create(null);
|
|
997
|
-
if (sourceSchema != null && variableDefinitions != null) {
|
|
998
|
-
for (const def of variableDefinitions) {
|
|
999
|
-
const varName = def.variable.name.value;
|
|
1000
|
-
variableDefinitionMap[varName] = def;
|
|
1001
|
-
const varType = typeFromAST(sourceSchema, def.type);
|
|
1002
|
-
const serializedValue = serializeInputValue(varType, variableValues === null || variableValues === void 0 ? void 0 : variableValues[varName]);
|
|
1003
|
-
if (serializedValue !== undefined) {
|
|
1004
|
-
newVariables[varName] = serializedValue;
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
if (sourceParentType != null && sourceFieldName != null) {
|
|
1009
|
-
updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, newVariables);
|
|
1010
|
-
}
|
|
1011
|
-
const rootFieldName = targetFieldName !== null && targetFieldName !== void 0 ? targetFieldName : (_b = fieldNodes === null || fieldNodes === void 0 ? void 0 : fieldNodes[0]) === null || _b === void 0 ? void 0 : _b.name.value;
|
|
1012
|
-
if (rootFieldName === undefined) {
|
|
1013
|
-
throw new Error(`Either "targetFieldName" or a non empty "fieldNodes" array must be provided.`);
|
|
1014
|
-
}
|
|
1015
|
-
const rootfieldNode = {
|
|
1016
|
-
kind: Kind.FIELD,
|
|
1017
|
-
arguments: Object.values(argumentNodeMap),
|
|
1018
|
-
name: {
|
|
1019
|
-
kind: Kind.NAME,
|
|
1020
|
-
value: rootFieldName,
|
|
1021
|
-
},
|
|
1022
|
-
selectionSet: newSelectionSet,
|
|
1023
|
-
};
|
|
1024
|
-
const operationName = targetOperationName
|
|
1025
|
-
? {
|
|
1026
|
-
kind: Kind.NAME,
|
|
1027
|
-
value: targetOperationName,
|
|
1028
|
-
}
|
|
1029
|
-
: undefined;
|
|
1030
|
-
const operationDefinition = {
|
|
1031
|
-
kind: Kind.OPERATION_DEFINITION,
|
|
1032
|
-
name: operationName,
|
|
1033
|
-
operation: targetOperation,
|
|
1034
|
-
variableDefinitions: Object.values(variableDefinitionMap),
|
|
1035
|
-
selectionSet: {
|
|
1036
|
-
kind: Kind.SELECTION_SET,
|
|
1037
|
-
selections: [rootfieldNode],
|
|
1038
|
-
},
|
|
1039
|
-
};
|
|
1040
|
-
const definitions = [operationDefinition];
|
|
1041
|
-
if (fragments != null) {
|
|
1042
|
-
for (const fragmentName in fragments) {
|
|
1043
|
-
const fragment = fragments[fragmentName];
|
|
1044
|
-
definitions.push(fragment);
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
const document = {
|
|
1048
|
-
kind: Kind.DOCUMENT,
|
|
1049
|
-
definitions,
|
|
1050
|
-
};
|
|
1051
|
-
return {
|
|
1052
|
-
document,
|
|
1053
|
-
variables: newVariables,
|
|
1054
|
-
rootValue: targetRootValue,
|
|
1055
|
-
operationName: targetOperationName,
|
|
1056
|
-
context,
|
|
1057
|
-
info,
|
|
1058
|
-
operationType: targetOperation,
|
|
1059
|
-
};
|
|
1060
|
-
}
|
|
1061
|
-
function updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, variableValues) {
|
|
1062
|
-
const generateVariableName = createVariableNameGenerator(variableDefinitionMap);
|
|
1063
|
-
const sourceField = sourceParentType.getFields()[sourceFieldName];
|
|
1064
|
-
for (const argument of sourceField.args) {
|
|
1065
|
-
const argName = argument.name;
|
|
1066
|
-
const sourceArgType = argument.type;
|
|
1067
|
-
if (argumentNodeMap[argName] === undefined) {
|
|
1068
|
-
const defaultValue = argument.defaultValue;
|
|
1069
|
-
if (defaultValue !== undefined) {
|
|
1070
|
-
updateArgument(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), sourceArgType, serializeInputValue(sourceArgType, defaultValue));
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
/**
|
|
1077
|
-
* Resolver that knows how to:
|
|
1078
|
-
* a) handle aliases for proxied schemas
|
|
1079
|
-
* b) handle errors from proxied schemas
|
|
1080
|
-
* c) handle external to internal enum conversion
|
|
1081
|
-
*/
|
|
1082
|
-
function defaultMergedResolver(parent, args, context, info) {
|
|
1083
|
-
if (!parent) {
|
|
1084
|
-
return null;
|
|
1085
|
-
}
|
|
1086
|
-
const responseKey = getResponseKeyFromInfo(info);
|
|
1087
|
-
// check to see if parent is not a proxied result, i.e. if parent resolver was manually overwritten
|
|
1088
|
-
// See https://github.com/ardatan/graphql-tools/issues/967
|
|
1089
|
-
if (!isExternalObject(parent)) {
|
|
1090
|
-
return defaultFieldResolver(parent, args, context, info);
|
|
1091
|
-
}
|
|
1092
|
-
const data = parent[responseKey];
|
|
1093
|
-
const unpathedErrors = getUnpathedErrors(parent);
|
|
1094
|
-
const subschema = getSubschema(parent, responseKey);
|
|
1095
|
-
return resolveExternalValue(data, unpathedErrors, subschema, context, info);
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
function isSubschemaConfig(value) {
|
|
1099
|
-
return Boolean(value === null || value === void 0 ? void 0 : value.schema);
|
|
1100
|
-
}
|
|
1101
|
-
function cloneSubschemaConfig(subschemaConfig) {
|
|
1102
|
-
var _a, _b;
|
|
1103
|
-
const newSubschemaConfig = {
|
|
1104
|
-
...subschemaConfig,
|
|
1105
|
-
transforms: subschemaConfig.transforms != null ? [...subschemaConfig.transforms] : undefined,
|
|
1106
|
-
};
|
|
1107
|
-
if (newSubschemaConfig.merge != null) {
|
|
1108
|
-
newSubschemaConfig.merge = { ...subschemaConfig.merge };
|
|
1109
|
-
for (const typeName in newSubschemaConfig.merge) {
|
|
1110
|
-
const mergedTypeConfig = (newSubschemaConfig.merge[typeName] = { ...((_b = (_a = subschemaConfig.merge) === null || _a === void 0 ? void 0 : _a[typeName]) !== null && _b !== void 0 ? _b : {}) });
|
|
1111
|
-
if (mergedTypeConfig.entryPoints != null) {
|
|
1112
|
-
mergedTypeConfig.entryPoints = mergedTypeConfig.entryPoints.map(entryPoint => ({ ...entryPoint }));
|
|
1113
|
-
}
|
|
1114
|
-
if (mergedTypeConfig.fields != null) {
|
|
1115
|
-
const fields = (mergedTypeConfig.fields = { ...mergedTypeConfig.fields });
|
|
1116
|
-
for (const fieldName in fields) {
|
|
1117
|
-
fields[fieldName] = { ...fields[fieldName] };
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
return newSubschemaConfig;
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
function delegateToSchema(options) {
|
|
1126
|
-
var _a, _b;
|
|
1127
|
-
const { info, schema, rootValue = (_a = schema.rootValue) !== null && _a !== void 0 ? _a : info.rootValue, operationName = (_b = info.operation.name) === null || _b === void 0 ? void 0 : _b.value, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes = info.fieldNodes, context, } = options;
|
|
1128
|
-
const request = createRequest({
|
|
1129
|
-
sourceSchema: info.schema,
|
|
1130
|
-
sourceParentType: info.parentType,
|
|
1131
|
-
sourceFieldName: info.fieldName,
|
|
1132
|
-
fragments: info.fragments,
|
|
1133
|
-
variableDefinitions: info.operation.variableDefinitions,
|
|
1134
|
-
variableValues: info.variableValues,
|
|
1135
|
-
targetRootValue: rootValue,
|
|
1136
|
-
targetOperationName: operationName,
|
|
1137
|
-
targetOperation: operation,
|
|
1138
|
-
targetFieldName: fieldName,
|
|
1139
|
-
selectionSet,
|
|
1140
|
-
fieldNodes,
|
|
1141
|
-
context,
|
|
1142
|
-
info,
|
|
1143
|
-
});
|
|
1144
|
-
return delegateRequest({
|
|
1145
|
-
...options,
|
|
1146
|
-
request,
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
function getDelegationReturnType(targetSchema, operation, fieldName) {
|
|
1150
|
-
const rootType = getDefinedRootType(targetSchema, operation);
|
|
1151
|
-
return rootType.getFields()[fieldName].type;
|
|
1152
|
-
}
|
|
1153
|
-
function delegateRequest(options) {
|
|
1154
|
-
const delegationContext = getDelegationContext(options);
|
|
1155
|
-
const transformer = new Transformer(delegationContext);
|
|
1156
|
-
const processedRequest = transformer.transformRequest(options.request);
|
|
1157
|
-
if (options.validateRequest) {
|
|
1158
|
-
validateRequest(delegationContext, processedRequest.document);
|
|
1159
|
-
}
|
|
1160
|
-
const executor = getExecutor(delegationContext);
|
|
1161
|
-
return new ValueOrPromise(() => executor(processedRequest))
|
|
1162
|
-
.then(originalResult => {
|
|
1163
|
-
if (isAsyncIterable(originalResult)) {
|
|
1164
|
-
const iterator = originalResult[Symbol.asyncIterator]();
|
|
1165
|
-
// "subscribe" to the subscription result and map the result through the transforms
|
|
1166
|
-
return mapAsyncIterator(iterator, result => transformer.transformResult(result));
|
|
1167
|
-
}
|
|
1168
|
-
return transformer.transformResult(originalResult);
|
|
1169
|
-
})
|
|
1170
|
-
.resolve();
|
|
1171
|
-
}
|
|
1172
|
-
function getDelegationContext({ request, schema, fieldName, returnType, args, info, transforms = [], transformedSchema, skipTypeMerging = false, }) {
|
|
1173
|
-
var _a, _b, _c, _d;
|
|
1174
|
-
const operationDefinition = getOperationASTFromRequest(request);
|
|
1175
|
-
let targetFieldName;
|
|
1176
|
-
if (fieldName == null) {
|
|
1177
|
-
targetFieldName = operationDefinition.selectionSet.selections[0].name.value;
|
|
1178
|
-
}
|
|
1179
|
-
else {
|
|
1180
|
-
targetFieldName = fieldName;
|
|
1181
|
-
}
|
|
1182
|
-
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a['stitchingInfo'];
|
|
1183
|
-
const subschemaOrSubschemaConfig = (_b = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.subschemaMap.get(schema)) !== null && _b !== void 0 ? _b : schema;
|
|
1184
|
-
const operation = operationDefinition.operation;
|
|
1185
|
-
if (isSubschemaConfig(subschemaOrSubschemaConfig)) {
|
|
1186
|
-
const targetSchema = subschemaOrSubschemaConfig.schema;
|
|
1187
|
-
return {
|
|
1188
|
-
subschema: schema,
|
|
1189
|
-
subschemaConfig: subschemaOrSubschemaConfig,
|
|
1190
|
-
targetSchema,
|
|
1191
|
-
operation,
|
|
1192
|
-
fieldName: targetFieldName,
|
|
1193
|
-
args,
|
|
1194
|
-
context: request.context,
|
|
1195
|
-
info,
|
|
1196
|
-
returnType: (_c = returnType !== null && returnType !== void 0 ? returnType : info === null || info === void 0 ? void 0 : info.returnType) !== null && _c !== void 0 ? _c : getDelegationReturnType(targetSchema, operation, targetFieldName),
|
|
1197
|
-
transforms: subschemaOrSubschemaConfig.transforms != null
|
|
1198
|
-
? subschemaOrSubschemaConfig.transforms.concat(transforms)
|
|
1199
|
-
: transforms,
|
|
1200
|
-
transformedSchema: transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : (subschemaOrSubschemaConfig instanceof Subschema ? subschemaOrSubschemaConfig.transformedSchema : targetSchema),
|
|
1201
|
-
skipTypeMerging,
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
return {
|
|
1205
|
-
subschema: schema,
|
|
1206
|
-
subschemaConfig: undefined,
|
|
1207
|
-
targetSchema: subschemaOrSubschemaConfig,
|
|
1208
|
-
operation,
|
|
1209
|
-
fieldName: targetFieldName,
|
|
1210
|
-
args,
|
|
1211
|
-
context: request.context,
|
|
1212
|
-
info,
|
|
1213
|
-
returnType: (_d = returnType !== null && returnType !== void 0 ? returnType : info === null || info === void 0 ? void 0 : info.returnType) !== null && _d !== void 0 ? _d : getDelegationReturnType(subschemaOrSubschemaConfig, operation, targetFieldName),
|
|
1214
|
-
transforms,
|
|
1215
|
-
transformedSchema: transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : subschemaOrSubschemaConfig,
|
|
1216
|
-
skipTypeMerging,
|
|
1217
|
-
};
|
|
1218
|
-
}
|
|
1219
|
-
function validateRequest(delegationContext, document) {
|
|
1220
|
-
const errors = validate(delegationContext.targetSchema, document);
|
|
1221
|
-
if (errors.length > 0) {
|
|
1222
|
-
if (errors.length > 1) {
|
|
1223
|
-
const combinedError = new AggregateError(errors, errors.map(error => error.message).join(', \n'));
|
|
1224
|
-
throw combinedError;
|
|
1225
|
-
}
|
|
1226
|
-
const error = errors[0];
|
|
1227
|
-
throw error.originalError || error;
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
const GLOBAL_CONTEXT = {};
|
|
1231
|
-
function getExecutor(delegationContext) {
|
|
1232
|
-
const { subschemaConfig, targetSchema, context } = delegationContext;
|
|
1233
|
-
let executor = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.executor) || createDefaultExecutor(targetSchema);
|
|
1234
|
-
if (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batch) {
|
|
1235
|
-
const batchingOptions = subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batchingOptions;
|
|
1236
|
-
executor = getBatchingExecutor(context !== null && context !== void 0 ? context : GLOBAL_CONTEXT, executor, batchingOptions === null || batchingOptions === void 0 ? void 0 : batchingOptions.dataLoaderOptions, batchingOptions === null || batchingOptions === void 0 ? void 0 : batchingOptions.extensionsReducer);
|
|
1237
|
-
}
|
|
1238
|
-
return executor;
|
|
1239
|
-
}
|
|
1240
|
-
const createDefaultExecutor = memoize1(function createDefaultExecutor(schema) {
|
|
1241
|
-
return function defaultExecutor(request) {
|
|
1242
|
-
const executionArgs = {
|
|
1243
|
-
schema,
|
|
1244
|
-
document: request.document,
|
|
1245
|
-
rootValue: request.rootValue,
|
|
1246
|
-
contextValue: request.context,
|
|
1247
|
-
variableValues: request.variables,
|
|
1248
|
-
operationName: request.operationName,
|
|
1249
|
-
};
|
|
1250
|
-
const operationType = request.operationType || getOperationASTFromRequest(request).operation;
|
|
1251
|
-
if (operationType === 'subscription') {
|
|
1252
|
-
return subscribe(executionArgs);
|
|
1253
|
-
}
|
|
1254
|
-
return execute(executionArgs);
|
|
1255
|
-
};
|
|
1256
|
-
});
|
|
1257
|
-
|
|
1258
|
-
export { Subschema, Transformer, annotateExternalObject, applySchemaTransforms, cloneSubschemaConfig, createDefaultExecutor, createRequest, defaultMergedResolver, delegateRequest, delegateToSchema, getDelegatingOperation, getSubschema, getUnpathedErrors, isExternalObject, isSubschema, isSubschemaConfig, mergeFields, resolveExternalValue };
|