@graphql-tools/delegate 9.0.0-alpha-e2ab84c8.0 → 9.0.0-alpha-25204a2f.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/README.md +0 -2
- package/cjs/Subschema.js +22 -0
- package/cjs/Transformer.js +45 -0
- package/cjs/applySchemaTransforms.js +11 -0
- package/cjs/checkResultAndHandleErrors.js +77 -0
- package/cjs/createRequest.js +125 -0
- package/cjs/defaultMergedResolver.js +29 -0
- package/cjs/delegateToSchema.js +148 -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 +7 -0
- package/esm/checkResultAndHandleErrors.js +72 -0
- package/esm/createRequest.js +120 -0
- package/esm/defaultMergedResolver.js +25 -0
- package/esm/delegateToSchema.js +143 -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 +40 -18
- package/{Subschema.d.ts → typings/Subschema.d.ts} +7 -3
- package/{Transformer.d.ts → typings/Transformer.d.ts} +3 -3
- package/{applySchemaTransforms.d.ts → typings/applySchemaTransforms.d.ts} +2 -2
- package/typings/checkResultAndHandleErrors.d.ts +8 -0
- package/{createRequest.d.ts → typings/createRequest.d.ts} +1 -2
- package/{defaultMergedResolver.d.ts → typings/defaultMergedResolver.d.ts} +2 -4
- package/typings/delegateToSchema.d.ts +6 -0
- package/typings/finalizeGatewayRequest.d.ts +3 -0
- package/{getDocumentMetadata.d.ts → typings/getDocumentMetadata.d.ts} +0 -0
- package/typings/index.d.ts +10 -0
- package/typings/mergeFields.d.ts +8 -0
- package/{prepareGatewayDocument.d.ts → typings/prepareGatewayDocument.d.ts} +0 -0
- package/typings/resolveExternalValue.d.ts +3 -0
- package/{subschemaConfig.d.ts → typings/subschemaConfig.d.ts} +1 -1
- package/typings/symbols.d.ts +3 -0
- package/{types.d.ts → typings/types.d.ts} +21 -40
- package/delegateToSchema.d.ts +0 -9
- package/externalObjects.d.ts +0 -11
- package/externalValues.d.ts +0 -4
- package/finalizeGatewayRequest.d.ts +0 -3
- package/getMergedParent.d.ts +0 -9
- package/index.d.ts +0 -11
- package/index.js +0 -1624
- package/index.mjs +0 -1590
- package/mergeDataAndErrors.d.ts +0 -5
- package/symbols.d.ts +0 -7
package/README.md
CHANGED
package/cjs/Subschema.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Subschema = exports.isSubschema = void 0;
|
|
4
|
+
const applySchemaTransforms_js_1 = require("./applySchemaTransforms.js");
|
|
5
|
+
function isSubschema(value) {
|
|
6
|
+
return Boolean(value.transformedSchema);
|
|
7
|
+
}
|
|
8
|
+
exports.isSubschema = isSubschema;
|
|
9
|
+
class Subschema {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
var _a;
|
|
12
|
+
this.schema = config.schema;
|
|
13
|
+
this.executor = config.executor;
|
|
14
|
+
this.batch = config.batch;
|
|
15
|
+
this.batchingOptions = config.batchingOptions;
|
|
16
|
+
this.createProxyingResolver = config.createProxyingResolver;
|
|
17
|
+
this.transforms = (_a = config.transforms) !== null && _a !== void 0 ? _a : [];
|
|
18
|
+
this.transformedSchema = (0, applySchemaTransforms_js_1.applySchemaTransforms)(this.schema, config);
|
|
19
|
+
this.merge = config.merge;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Subschema = Subschema;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transformer = void 0;
|
|
4
|
+
const prepareGatewayDocument_js_1 = require("./prepareGatewayDocument.js");
|
|
5
|
+
const finalizeGatewayRequest_js_1 = require("./finalizeGatewayRequest.js");
|
|
6
|
+
const checkResultAndHandleErrors_js_1 = require("./checkResultAndHandleErrors.js");
|
|
7
|
+
class Transformer {
|
|
8
|
+
constructor(context) {
|
|
9
|
+
this.transformations = [];
|
|
10
|
+
this.delegationContext = context;
|
|
11
|
+
const transforms = context.transforms;
|
|
12
|
+
const delegationTransforms = transforms.slice().reverse();
|
|
13
|
+
for (const transform of delegationTransforms) {
|
|
14
|
+
this.addTransform(transform, {});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
addTransform(transform, context = {}) {
|
|
18
|
+
this.transformations.push({ transform, context });
|
|
19
|
+
}
|
|
20
|
+
transformRequest(originalRequest) {
|
|
21
|
+
var _a;
|
|
22
|
+
let request = {
|
|
23
|
+
...originalRequest,
|
|
24
|
+
document: (0, prepareGatewayDocument_js_1.prepareGatewayDocument)(originalRequest.document, this.delegationContext.transformedSchema, this.delegationContext.returnType, (_a = this.delegationContext.info) === null || _a === void 0 ? void 0 : _a.schema),
|
|
25
|
+
};
|
|
26
|
+
for (const transformation of this.transformations) {
|
|
27
|
+
if (transformation.transform.transformRequest) {
|
|
28
|
+
request = transformation.transform.transformRequest(request, this.delegationContext, transformation.context);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return (0, finalizeGatewayRequest_js_1.finalizeGatewayRequest)(request, this.delegationContext);
|
|
32
|
+
}
|
|
33
|
+
transformResult(originalResult) {
|
|
34
|
+
let result = originalResult;
|
|
35
|
+
// from right to left
|
|
36
|
+
for (let i = this.transformations.length - 1; i >= 0; i--) {
|
|
37
|
+
const transformation = this.transformations[i];
|
|
38
|
+
if (transformation.transform.transformResult) {
|
|
39
|
+
result = transformation.transform.transformResult(result, this.delegationContext, transformation.context);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return (0, checkResultAndHandleErrors_js_1.checkResultAndHandleErrors)(result, this.delegationContext);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.Transformer = Transformer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applySchemaTransforms = void 0;
|
|
4
|
+
function applySchemaTransforms(originalWrappingSchema, subschemaConfig) {
|
|
5
|
+
const schemaTransforms = subschemaConfig.transforms;
|
|
6
|
+
if (schemaTransforms == null) {
|
|
7
|
+
return originalWrappingSchema;
|
|
8
|
+
}
|
|
9
|
+
return schemaTransforms.reduce((schema, transform) => transform.transformSchema != null ? transform.transformSchema(schema, subschemaConfig) : schema, originalWrappingSchema);
|
|
10
|
+
}
|
|
11
|
+
exports.applySchemaTransforms = applySchemaTransforms;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeDataAndErrors = exports.checkResultAndHandleErrors = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
const resolveExternalValue_js_1 = require("./resolveExternalValue.js");
|
|
7
|
+
function checkResultAndHandleErrors(result, delegationContext) {
|
|
8
|
+
const { context, info, fieldName: responseKey = getResponseKey(info), subschema, returnType = getReturnType(info), skipTypeMerging, onLocatedError, } = delegationContext;
|
|
9
|
+
const { data, unpathedErrors } = mergeDataAndErrors(result.data == null ? undefined : result.data[responseKey], result.errors == null ? [] : result.errors, info != null && info.path ? (0, graphql_1.responsePathAsArray)(info.path) : undefined, onLocatedError);
|
|
10
|
+
return (0, resolveExternalValue_js_1.resolveExternalValue)(data, unpathedErrors, subschema, context, info, returnType, skipTypeMerging);
|
|
11
|
+
}
|
|
12
|
+
exports.checkResultAndHandleErrors = checkResultAndHandleErrors;
|
|
13
|
+
function mergeDataAndErrors(data, errors, path, onLocatedError, index = 1) {
|
|
14
|
+
var _a;
|
|
15
|
+
if (data == null) {
|
|
16
|
+
if (!errors.length) {
|
|
17
|
+
return { data: null, unpathedErrors: [] };
|
|
18
|
+
}
|
|
19
|
+
if (errors.length === 1) {
|
|
20
|
+
const error = onLocatedError ? onLocatedError(errors[0]) : errors[0];
|
|
21
|
+
const newPath = path === undefined ? error.path : !error.path ? path : path.concat(error.path.slice(1));
|
|
22
|
+
return { data: (0, utils_1.relocatedError)(errors[0], newPath), unpathedErrors: [] };
|
|
23
|
+
}
|
|
24
|
+
// We cast path as any for GraphQL.js 14 compat
|
|
25
|
+
// locatedError path argument must be defined, but it is just forwarded to a constructor that allows a undefined value
|
|
26
|
+
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/locatedError.js#L25
|
|
27
|
+
// https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/GraphQLError.js#L19
|
|
28
|
+
const combinedError = new utils_1.AggregateError(errors, errors.map(error => error.message).join(', \n'));
|
|
29
|
+
const newError = (0, graphql_1.locatedError)(combinedError, undefined, path);
|
|
30
|
+
return { data: newError, unpathedErrors: [] };
|
|
31
|
+
}
|
|
32
|
+
if (!errors.length) {
|
|
33
|
+
return { data, unpathedErrors: [] };
|
|
34
|
+
}
|
|
35
|
+
const unpathedErrors = [];
|
|
36
|
+
const errorMap = new Map();
|
|
37
|
+
for (const error of errors) {
|
|
38
|
+
const pathSegment = (_a = error.path) === null || _a === void 0 ? void 0 : _a[index];
|
|
39
|
+
if (pathSegment != null) {
|
|
40
|
+
let pathSegmentErrors = errorMap.get(pathSegment);
|
|
41
|
+
if (pathSegmentErrors === undefined) {
|
|
42
|
+
pathSegmentErrors = [error];
|
|
43
|
+
errorMap.set(pathSegment, pathSegmentErrors);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
pathSegmentErrors.push(error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
unpathedErrors.push(error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const [pathSegment, pathSegmentErrors] of errorMap) {
|
|
54
|
+
if (data[pathSegment] !== undefined) {
|
|
55
|
+
const { data: newData, unpathedErrors: newErrors } = mergeDataAndErrors(data[pathSegment], pathSegmentErrors, path, onLocatedError, index + 1);
|
|
56
|
+
data[pathSegment] = newData;
|
|
57
|
+
unpathedErrors.push(...newErrors);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
unpathedErrors.push(...pathSegmentErrors);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { data, unpathedErrors };
|
|
64
|
+
}
|
|
65
|
+
exports.mergeDataAndErrors = mergeDataAndErrors;
|
|
66
|
+
function getResponseKey(info) {
|
|
67
|
+
if (info == null) {
|
|
68
|
+
throw new Error(`Data cannot be extracted from result without an explicit key or source schema.`);
|
|
69
|
+
}
|
|
70
|
+
return (0, utils_1.getResponseKeyFromInfo)(info);
|
|
71
|
+
}
|
|
72
|
+
function getReturnType(info) {
|
|
73
|
+
if (info == null) {
|
|
74
|
+
throw new Error(`Return type cannot be inferred without a source schema.`);
|
|
75
|
+
}
|
|
76
|
+
return info.returnType;
|
|
77
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRequest = exports.getDelegatingOperation = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
function getDelegatingOperation(parentType, schema) {
|
|
7
|
+
if (parentType === schema.getMutationType()) {
|
|
8
|
+
return 'mutation';
|
|
9
|
+
}
|
|
10
|
+
else if (parentType === schema.getSubscriptionType()) {
|
|
11
|
+
return 'subscription';
|
|
12
|
+
}
|
|
13
|
+
return 'query';
|
|
14
|
+
}
|
|
15
|
+
exports.getDelegatingOperation = getDelegatingOperation;
|
|
16
|
+
function createRequest({ sourceSchema, sourceParentType, sourceFieldName, fragments, variableDefinitions, variableValues, targetRootValue, targetOperationName, targetOperation, targetFieldName, selectionSet, fieldNodes, context, info, }) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
let newSelectionSet;
|
|
19
|
+
const argumentNodeMap = Object.create(null);
|
|
20
|
+
if (selectionSet != null) {
|
|
21
|
+
newSelectionSet = selectionSet;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const selections = [];
|
|
25
|
+
for (const fieldNode of fieldNodes || []) {
|
|
26
|
+
if (fieldNode.selectionSet) {
|
|
27
|
+
for (const selection of fieldNode.selectionSet.selections) {
|
|
28
|
+
selections.push(selection);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
newSelectionSet = selections.length
|
|
33
|
+
? {
|
|
34
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
35
|
+
selections,
|
|
36
|
+
}
|
|
37
|
+
: undefined;
|
|
38
|
+
const args = (_a = fieldNodes === null || fieldNodes === void 0 ? void 0 : fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments;
|
|
39
|
+
if (args) {
|
|
40
|
+
for (const argNode of args) {
|
|
41
|
+
argumentNodeMap[argNode.name.value] = argNode;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const newVariables = Object.create(null);
|
|
46
|
+
const variableDefinitionMap = Object.create(null);
|
|
47
|
+
if (sourceSchema != null && variableDefinitions != null) {
|
|
48
|
+
for (const def of variableDefinitions) {
|
|
49
|
+
const varName = def.variable.name.value;
|
|
50
|
+
variableDefinitionMap[varName] = def;
|
|
51
|
+
const varType = (0, graphql_1.typeFromAST)(sourceSchema, def.type);
|
|
52
|
+
const serializedValue = (0, utils_1.serializeInputValue)(varType, variableValues === null || variableValues === void 0 ? void 0 : variableValues[varName]);
|
|
53
|
+
if (serializedValue !== undefined) {
|
|
54
|
+
newVariables[varName] = serializedValue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (sourceParentType != null && sourceFieldName != null) {
|
|
59
|
+
updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, newVariables);
|
|
60
|
+
}
|
|
61
|
+
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;
|
|
62
|
+
if (rootFieldName === undefined) {
|
|
63
|
+
throw new Error(`Either "targetFieldName" or a non empty "fieldNodes" array must be provided.`);
|
|
64
|
+
}
|
|
65
|
+
const rootfieldNode = {
|
|
66
|
+
kind: graphql_1.Kind.FIELD,
|
|
67
|
+
arguments: Object.values(argumentNodeMap),
|
|
68
|
+
name: {
|
|
69
|
+
kind: graphql_1.Kind.NAME,
|
|
70
|
+
value: rootFieldName,
|
|
71
|
+
},
|
|
72
|
+
selectionSet: newSelectionSet,
|
|
73
|
+
};
|
|
74
|
+
const operationName = targetOperationName
|
|
75
|
+
? {
|
|
76
|
+
kind: graphql_1.Kind.NAME,
|
|
77
|
+
value: targetOperationName,
|
|
78
|
+
}
|
|
79
|
+
: undefined;
|
|
80
|
+
const operationDefinition = {
|
|
81
|
+
kind: graphql_1.Kind.OPERATION_DEFINITION,
|
|
82
|
+
name: operationName,
|
|
83
|
+
operation: targetOperation,
|
|
84
|
+
variableDefinitions: Object.values(variableDefinitionMap),
|
|
85
|
+
selectionSet: {
|
|
86
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
87
|
+
selections: [rootfieldNode],
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
const definitions = [operationDefinition];
|
|
91
|
+
if (fragments != null) {
|
|
92
|
+
for (const fragmentName in fragments) {
|
|
93
|
+
const fragment = fragments[fragmentName];
|
|
94
|
+
definitions.push(fragment);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const document = {
|
|
98
|
+
kind: graphql_1.Kind.DOCUMENT,
|
|
99
|
+
definitions,
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
document,
|
|
103
|
+
variables: newVariables,
|
|
104
|
+
rootValue: targetRootValue,
|
|
105
|
+
operationName: targetOperationName,
|
|
106
|
+
context,
|
|
107
|
+
info,
|
|
108
|
+
operationType: targetOperation,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
exports.createRequest = createRequest;
|
|
112
|
+
function updateArgumentsWithDefaults(sourceParentType, sourceFieldName, argumentNodeMap, variableDefinitionMap, variableValues) {
|
|
113
|
+
const generateVariableName = (0, utils_1.createVariableNameGenerator)(variableDefinitionMap);
|
|
114
|
+
const sourceField = sourceParentType.getFields()[sourceFieldName];
|
|
115
|
+
for (const argument of sourceField.args) {
|
|
116
|
+
const argName = argument.name;
|
|
117
|
+
const sourceArgType = argument.type;
|
|
118
|
+
if (argumentNodeMap[argName] === undefined) {
|
|
119
|
+
const defaultValue = argument.defaultValue;
|
|
120
|
+
if (defaultValue !== undefined) {
|
|
121
|
+
(0, utils_1.updateArgument)(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), sourceArgType, (0, utils_1.serializeInputValue)(sourceArgType, defaultValue));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultMergedResolver = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
const resolveExternalValue_js_1 = require("./resolveExternalValue.js");
|
|
7
|
+
const mergeFields_js_1 = require("./mergeFields.js");
|
|
8
|
+
/**
|
|
9
|
+
* Resolver that knows how to:
|
|
10
|
+
* a) handle aliases for proxied schemas
|
|
11
|
+
* b) handle errors from proxied schemas
|
|
12
|
+
* c) handle external to internal enum conversion
|
|
13
|
+
*/
|
|
14
|
+
function defaultMergedResolver(parent, args, context, info) {
|
|
15
|
+
if (!parent) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const responseKey = (0, utils_1.getResponseKeyFromInfo)(info);
|
|
19
|
+
// check to see if parent is not a proxied result, i.e. if parent resolver was manually overwritten
|
|
20
|
+
// See https://github.com/ardatan/graphql-tools/issues/967
|
|
21
|
+
if (!(0, mergeFields_js_1.isExternalObject)(parent)) {
|
|
22
|
+
return (0, graphql_1.defaultFieldResolver)(parent, args, context, info);
|
|
23
|
+
}
|
|
24
|
+
const data = parent[responseKey];
|
|
25
|
+
const unpathedErrors = (0, mergeFields_js_1.getUnpathedErrors)(parent);
|
|
26
|
+
const subschema = (0, mergeFields_js_1.getSubschema)(parent, responseKey);
|
|
27
|
+
return (0, resolveExternalValue_js_1.resolveExternalValue)(data, unpathedErrors, subschema, context, info);
|
|
28
|
+
}
|
|
29
|
+
exports.defaultMergedResolver = defaultMergedResolver;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDefaultExecutor = exports.delegateRequest = exports.delegateToSchema = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const value_or_promise_1 = require("value-or-promise");
|
|
6
|
+
const batch_execute_1 = require("@graphql-tools/batch-execute");
|
|
7
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
8
|
+
const subschemaConfig_js_1 = require("./subschemaConfig.js");
|
|
9
|
+
const Subschema_js_1 = require("./Subschema.js");
|
|
10
|
+
const createRequest_js_1 = require("./createRequest.js");
|
|
11
|
+
const Transformer_js_1 = require("./Transformer.js");
|
|
12
|
+
const applySchemaTransforms_js_1 = require("./applySchemaTransforms.js");
|
|
13
|
+
function delegateToSchema(options) {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
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 = (0, createRequest_js_1.getDelegatingOperation)(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes = info.fieldNodes, context, } = options;
|
|
16
|
+
const request = (0, createRequest_js_1.createRequest)({
|
|
17
|
+
sourceSchema: info.schema,
|
|
18
|
+
sourceParentType: info.parentType,
|
|
19
|
+
sourceFieldName: info.fieldName,
|
|
20
|
+
fragments: info.fragments,
|
|
21
|
+
variableDefinitions: info.operation.variableDefinitions,
|
|
22
|
+
variableValues: info.variableValues,
|
|
23
|
+
targetRootValue: rootValue,
|
|
24
|
+
targetOperationName: operationName,
|
|
25
|
+
targetOperation: operation,
|
|
26
|
+
targetFieldName: fieldName,
|
|
27
|
+
selectionSet,
|
|
28
|
+
fieldNodes,
|
|
29
|
+
context,
|
|
30
|
+
info,
|
|
31
|
+
});
|
|
32
|
+
return delegateRequest({
|
|
33
|
+
...options,
|
|
34
|
+
request,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
exports.delegateToSchema = delegateToSchema;
|
|
38
|
+
function getDelegationReturnType(targetSchema, operation, fieldName) {
|
|
39
|
+
const rootType = (0, utils_1.getDefinedRootType)(targetSchema, operation);
|
|
40
|
+
return rootType.getFields()[fieldName].type;
|
|
41
|
+
}
|
|
42
|
+
function delegateRequest(options) {
|
|
43
|
+
const delegationContext = getDelegationContext(options);
|
|
44
|
+
const transformer = new Transformer_js_1.Transformer(delegationContext);
|
|
45
|
+
const processedRequest = transformer.transformRequest(options.request);
|
|
46
|
+
if (options.validateRequest) {
|
|
47
|
+
validateRequest(delegationContext, processedRequest.document);
|
|
48
|
+
}
|
|
49
|
+
const executor = getExecutor(delegationContext);
|
|
50
|
+
return new value_or_promise_1.ValueOrPromise(() => executor(processedRequest))
|
|
51
|
+
.then(originalResult => {
|
|
52
|
+
if ((0, utils_1.isAsyncIterable)(originalResult)) {
|
|
53
|
+
const iterator = originalResult[Symbol.asyncIterator]();
|
|
54
|
+
// "subscribe" to the subscription result and map the result through the transforms
|
|
55
|
+
return (0, utils_1.mapAsyncIterator)(iterator, result => transformer.transformResult(result));
|
|
56
|
+
}
|
|
57
|
+
return transformer.transformResult(originalResult);
|
|
58
|
+
})
|
|
59
|
+
.resolve();
|
|
60
|
+
}
|
|
61
|
+
exports.delegateRequest = delegateRequest;
|
|
62
|
+
function getDelegationContext({ request, schema, fieldName, returnType, args, info, transforms = [], transformedSchema, skipTypeMerging = false, }) {
|
|
63
|
+
var _a, _b, _c, _d;
|
|
64
|
+
const operationDefinition = (0, utils_1.getOperationASTFromRequest)(request);
|
|
65
|
+
let targetFieldName;
|
|
66
|
+
if (fieldName == null) {
|
|
67
|
+
targetFieldName = operationDefinition.selectionSet.selections[0].name.value;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
targetFieldName = fieldName;
|
|
71
|
+
}
|
|
72
|
+
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a['stitchingInfo'];
|
|
73
|
+
const subschemaOrSubschemaConfig = (_b = stitchingInfo === null || stitchingInfo === void 0 ? void 0 : stitchingInfo.subschemaMap.get(schema)) !== null && _b !== void 0 ? _b : schema;
|
|
74
|
+
const operation = operationDefinition.operation;
|
|
75
|
+
if ((0, subschemaConfig_js_1.isSubschemaConfig)(subschemaOrSubschemaConfig)) {
|
|
76
|
+
const targetSchema = subschemaOrSubschemaConfig.schema;
|
|
77
|
+
return {
|
|
78
|
+
subschema: schema,
|
|
79
|
+
subschemaConfig: subschemaOrSubschemaConfig,
|
|
80
|
+
targetSchema,
|
|
81
|
+
operation,
|
|
82
|
+
fieldName: targetFieldName,
|
|
83
|
+
args,
|
|
84
|
+
context: request.context,
|
|
85
|
+
info,
|
|
86
|
+
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),
|
|
87
|
+
transforms: subschemaOrSubschemaConfig.transforms != null
|
|
88
|
+
? subschemaOrSubschemaConfig.transforms.concat(transforms)
|
|
89
|
+
: transforms,
|
|
90
|
+
transformedSchema: transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : (subschemaOrSubschemaConfig instanceof Subschema_js_1.Subschema
|
|
91
|
+
? subschemaOrSubschemaConfig.transformedSchema
|
|
92
|
+
: (0, applySchemaTransforms_js_1.applySchemaTransforms)(targetSchema, subschemaOrSubschemaConfig)),
|
|
93
|
+
skipTypeMerging,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
subschema: schema,
|
|
98
|
+
subschemaConfig: undefined,
|
|
99
|
+
targetSchema: subschemaOrSubschemaConfig,
|
|
100
|
+
operation,
|
|
101
|
+
fieldName: targetFieldName,
|
|
102
|
+
args,
|
|
103
|
+
context: request.context,
|
|
104
|
+
info,
|
|
105
|
+
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),
|
|
106
|
+
transforms,
|
|
107
|
+
transformedSchema: transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : subschemaOrSubschemaConfig,
|
|
108
|
+
skipTypeMerging,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function validateRequest(delegationContext, document) {
|
|
112
|
+
const errors = (0, graphql_1.validate)(delegationContext.targetSchema, document);
|
|
113
|
+
if (errors.length > 0) {
|
|
114
|
+
if (errors.length > 1) {
|
|
115
|
+
const combinedError = new utils_1.AggregateError(errors, errors.map(error => error.message).join(', \n'));
|
|
116
|
+
throw combinedError;
|
|
117
|
+
}
|
|
118
|
+
const error = errors[0];
|
|
119
|
+
throw error.originalError || error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const GLOBAL_CONTEXT = {};
|
|
123
|
+
function getExecutor(delegationContext) {
|
|
124
|
+
const { subschemaConfig, targetSchema, context } = delegationContext;
|
|
125
|
+
let executor = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.executor) || (0, exports.createDefaultExecutor)(targetSchema);
|
|
126
|
+
if (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batch) {
|
|
127
|
+
const batchingOptions = subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.batchingOptions;
|
|
128
|
+
executor = (0, batch_execute_1.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);
|
|
129
|
+
}
|
|
130
|
+
return executor;
|
|
131
|
+
}
|
|
132
|
+
exports.createDefaultExecutor = (0, utils_1.memoize1)(function createDefaultExecutor(schema) {
|
|
133
|
+
return function defaultExecutor(request) {
|
|
134
|
+
const executionArgs = {
|
|
135
|
+
schema,
|
|
136
|
+
document: request.document,
|
|
137
|
+
rootValue: request.rootValue,
|
|
138
|
+
contextValue: request.context,
|
|
139
|
+
variableValues: request.variables,
|
|
140
|
+
operationName: request.operationName,
|
|
141
|
+
};
|
|
142
|
+
const operationType = request.operationType || (0, utils_1.getOperationASTFromRequest)(request).operation;
|
|
143
|
+
if (operationType === 'subscription') {
|
|
144
|
+
return (0, graphql_1.subscribe)(executionArgs);
|
|
145
|
+
}
|
|
146
|
+
return (0, graphql_1.execute)(executionArgs);
|
|
147
|
+
};
|
|
148
|
+
});
|