@graphql-tools/delegate 9.0.33 → 9.0.34
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/mergeFields.js +48 -43
- package/cjs/resolveExternalValue.js +1 -1
- package/esm/mergeFields.js +48 -43
- package/esm/resolveExternalValue.js +1 -1
- package/package.json +2 -2
- package/typings/mergeFields.d.cts +2 -1
- package/typings/mergeFields.d.ts +2 -1
package/cjs/mergeFields.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.mergeFields = exports.getUnpathedErrors = exports.getSubschema = exports
|
|
|
4
4
|
const graphql_1 = require("graphql");
|
|
5
5
|
const utils_1 = require("@graphql-tools/utils");
|
|
6
6
|
const symbols_js_1 = require("./symbols.js");
|
|
7
|
+
const value_or_promise_1 = require("value-or-promise");
|
|
7
8
|
function isExternalObject(data) {
|
|
8
9
|
return data[symbols_js_1.UNPATHED_ERRORS_SYMBOL] !== undefined;
|
|
9
10
|
}
|
|
@@ -28,60 +29,64 @@ function getUnpathedErrors(object) {
|
|
|
28
29
|
exports.getUnpathedErrors = getUnpathedErrors;
|
|
29
30
|
const EMPTY_ARRAY = [];
|
|
30
31
|
const EMPTY_OBJECT = Object.create(null);
|
|
31
|
-
|
|
32
|
+
function asyncForEach(array, fn) {
|
|
33
|
+
return array.reduce((prev, curr) => prev.then(() => fn(curr)), new value_or_promise_1.ValueOrPromise(() => { }));
|
|
34
|
+
}
|
|
35
|
+
function mergeFields(mergedTypeInfo, object, sourceSubschema, context, info) {
|
|
32
36
|
var _a;
|
|
33
37
|
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);
|
|
34
|
-
|
|
35
|
-
await executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info);
|
|
36
|
-
}
|
|
37
|
-
return object;
|
|
38
|
+
return asyncForEach(delegationMaps, delegationMap => executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info)).then(() => object);
|
|
38
39
|
}
|
|
39
40
|
exports.mergeFields = mergeFields;
|
|
40
|
-
|
|
41
|
+
function executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info) {
|
|
41
42
|
const combinedErrors = object[symbols_js_1.UNPATHED_ERRORS_SYMBOL];
|
|
42
43
|
const path = (0, graphql_1.responsePathAsArray)(info.path);
|
|
43
44
|
const combinedFieldSubschemaMap = object[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
44
|
-
|
|
45
|
+
function finallyFn(source, subschema, selectionSet) {
|
|
45
46
|
var _a;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
source = error;
|
|
56
|
-
}
|
|
57
|
-
if (source instanceof Error || source == null) {
|
|
58
|
-
const { fields } = (0, utils_1.collectFields)(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet);
|
|
59
|
-
const nullResult = {};
|
|
60
|
-
for (const [responseKey, fieldNodes] of fields) {
|
|
61
|
-
const combinedPath = [...path, responseKey];
|
|
62
|
-
if (source instanceof graphql_1.GraphQLError) {
|
|
63
|
-
nullResult[responseKey] = (0, utils_1.relocatedError)(source, combinedPath);
|
|
64
|
-
}
|
|
65
|
-
else if (source instanceof Error) {
|
|
66
|
-
nullResult[responseKey] = (0, graphql_1.locatedError)(source, fieldNodes, combinedPath);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
nullResult[responseKey] = null;
|
|
70
|
-
}
|
|
47
|
+
if (source instanceof Error || source == null) {
|
|
48
|
+
const schema = subschema.transformedSchema || info.schema;
|
|
49
|
+
const type = schema.getType(object.__typename);
|
|
50
|
+
const { fields } = (0, utils_1.collectFields)(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet);
|
|
51
|
+
const nullResult = {};
|
|
52
|
+
for (const [responseKey, fieldNodes] of fields) {
|
|
53
|
+
const combinedPath = [...path, responseKey];
|
|
54
|
+
if (source instanceof graphql_1.GraphQLError) {
|
|
55
|
+
nullResult[responseKey] = (0, utils_1.relocatedError)(source, combinedPath);
|
|
71
56
|
}
|
|
72
|
-
source
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
57
|
+
else if (source instanceof Error) {
|
|
58
|
+
nullResult[responseKey] = (0, graphql_1.locatedError)(source, fieldNodes, combinedPath);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
nullResult[responseKey] = null;
|
|
77
62
|
}
|
|
78
63
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
64
|
+
source = nullResult;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (source[symbols_js_1.UNPATHED_ERRORS_SYMBOL]) {
|
|
68
|
+
combinedErrors.push(...source[symbols_js_1.UNPATHED_ERRORS_SYMBOL]);
|
|
84
69
|
}
|
|
85
70
|
}
|
|
86
|
-
|
|
71
|
+
const objectSubschema = source[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL];
|
|
72
|
+
const fieldSubschemaMap = source[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
73
|
+
for (const responseKey in source) {
|
|
74
|
+
const existingPropValue = object[responseKey];
|
|
75
|
+
const sourcePropValue = source[responseKey];
|
|
76
|
+
if (sourcePropValue != null || existingPropValue == null) {
|
|
77
|
+
object[responseKey] = sourcePropValue;
|
|
78
|
+
}
|
|
79
|
+
combinedFieldSubschemaMap[responseKey] = (_a = fieldSubschemaMap === null || fieldSubschemaMap === void 0 ? void 0 : fieldSubschemaMap[responseKey]) !== null && _a !== void 0 ? _a : objectSubschema;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return value_or_promise_1.ValueOrPromise.all([...delegationMap.entries()].map(([subschema, selectionSet]) => new value_or_promise_1.ValueOrPromise(() => {
|
|
83
|
+
const schema = subschema.transformedSchema || info.schema;
|
|
84
|
+
const type = schema.getType(object.__typename);
|
|
85
|
+
const resolver = mergedTypeInfo.resolvers.get(subschema);
|
|
86
|
+
if (resolver) {
|
|
87
|
+
return resolver(object, context, info, subschema, selectionSet, undefined, type);
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
.then(source => finallyFn(source, subschema, selectionSet))
|
|
91
|
+
.catch(error => finallyFn(error, subschema, selectionSet)))).then(() => { });
|
|
87
92
|
}
|
|
@@ -52,7 +52,7 @@ function resolveExternalObject(type, object, unpathedErrors, subschema, context,
|
|
|
52
52
|
if (!targetSubschemas || !targetSubschemas.length) {
|
|
53
53
|
return object;
|
|
54
54
|
}
|
|
55
|
-
return (0, mergeFields_js_1.mergeFields)(mergedTypeInfo, object, subschema, context, info);
|
|
55
|
+
return (0, mergeFields_js_1.mergeFields)(mergedTypeInfo, object, subschema, context, info).resolve();
|
|
56
56
|
}
|
|
57
57
|
function resolveExternalList(type, list, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
|
58
58
|
return list.map(listMember => resolveExternalValue(listMember, unpathedErrors, subschema, context, info, type.ofType, skipTypeMerging));
|
package/esm/mergeFields.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { responsePathAsArray, GraphQLError, locatedError, } from 'graphql';
|
|
2
2
|
import { collectFields, relocatedError } from '@graphql-tools/utils';
|
|
3
3
|
import { FIELD_SUBSCHEMA_MAP_SYMBOL, OBJECT_SUBSCHEMA_SYMBOL, UNPATHED_ERRORS_SYMBOL } from './symbols.js';
|
|
4
|
+
import { ValueOrPromise } from 'value-or-promise';
|
|
4
5
|
export function isExternalObject(data) {
|
|
5
6
|
return data[UNPATHED_ERRORS_SYMBOL] !== undefined;
|
|
6
7
|
}
|
|
@@ -21,59 +22,63 @@ export function getUnpathedErrors(object) {
|
|
|
21
22
|
}
|
|
22
23
|
const EMPTY_ARRAY = [];
|
|
23
24
|
const EMPTY_OBJECT = Object.create(null);
|
|
24
|
-
|
|
25
|
+
function asyncForEach(array, fn) {
|
|
26
|
+
return array.reduce((prev, curr) => prev.then(() => fn(curr)), new ValueOrPromise(() => { }));
|
|
27
|
+
}
|
|
28
|
+
export function mergeFields(mergedTypeInfo, object, sourceSubschema, context, info) {
|
|
25
29
|
var _a;
|
|
26
30
|
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);
|
|
27
|
-
|
|
28
|
-
await executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info);
|
|
29
|
-
}
|
|
30
|
-
return object;
|
|
31
|
+
return asyncForEach(delegationMaps, delegationMap => executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info)).then(() => object);
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
function executeDelegationStage(mergedTypeInfo, delegationMap, object, context, info) {
|
|
33
34
|
const combinedErrors = object[UNPATHED_ERRORS_SYMBOL];
|
|
34
35
|
const path = responsePathAsArray(info.path);
|
|
35
36
|
const combinedFieldSubschemaMap = object[FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
36
|
-
|
|
37
|
+
function finallyFn(source, subschema, selectionSet) {
|
|
37
38
|
var _a;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
source = error;
|
|
48
|
-
}
|
|
49
|
-
if (source instanceof Error || source == null) {
|
|
50
|
-
const { fields } = collectFields(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet);
|
|
51
|
-
const nullResult = {};
|
|
52
|
-
for (const [responseKey, fieldNodes] of fields) {
|
|
53
|
-
const combinedPath = [...path, responseKey];
|
|
54
|
-
if (source instanceof GraphQLError) {
|
|
55
|
-
nullResult[responseKey] = relocatedError(source, combinedPath);
|
|
56
|
-
}
|
|
57
|
-
else if (source instanceof Error) {
|
|
58
|
-
nullResult[responseKey] = locatedError(source, fieldNodes, combinedPath);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
nullResult[responseKey] = null;
|
|
62
|
-
}
|
|
39
|
+
if (source instanceof Error || source == null) {
|
|
40
|
+
const schema = subschema.transformedSchema || info.schema;
|
|
41
|
+
const type = schema.getType(object.__typename);
|
|
42
|
+
const { fields } = collectFields(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet);
|
|
43
|
+
const nullResult = {};
|
|
44
|
+
for (const [responseKey, fieldNodes] of fields) {
|
|
45
|
+
const combinedPath = [...path, responseKey];
|
|
46
|
+
if (source instanceof GraphQLError) {
|
|
47
|
+
nullResult[responseKey] = relocatedError(source, combinedPath);
|
|
63
48
|
}
|
|
64
|
-
source
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
49
|
+
else if (source instanceof Error) {
|
|
50
|
+
nullResult[responseKey] = locatedError(source, fieldNodes, combinedPath);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
nullResult[responseKey] = null;
|
|
69
54
|
}
|
|
70
55
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
56
|
+
source = nullResult;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (source[UNPATHED_ERRORS_SYMBOL]) {
|
|
60
|
+
combinedErrors.push(...source[UNPATHED_ERRORS_SYMBOL]);
|
|
76
61
|
}
|
|
77
62
|
}
|
|
78
|
-
|
|
63
|
+
const objectSubschema = source[OBJECT_SUBSCHEMA_SYMBOL];
|
|
64
|
+
const fieldSubschemaMap = source[FIELD_SUBSCHEMA_MAP_SYMBOL];
|
|
65
|
+
for (const responseKey in source) {
|
|
66
|
+
const existingPropValue = object[responseKey];
|
|
67
|
+
const sourcePropValue = source[responseKey];
|
|
68
|
+
if (sourcePropValue != null || existingPropValue == null) {
|
|
69
|
+
object[responseKey] = sourcePropValue;
|
|
70
|
+
}
|
|
71
|
+
combinedFieldSubschemaMap[responseKey] = (_a = fieldSubschemaMap === null || fieldSubschemaMap === void 0 ? void 0 : fieldSubschemaMap[responseKey]) !== null && _a !== void 0 ? _a : objectSubschema;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return ValueOrPromise.all([...delegationMap.entries()].map(([subschema, selectionSet]) => new ValueOrPromise(() => {
|
|
75
|
+
const schema = subschema.transformedSchema || info.schema;
|
|
76
|
+
const type = schema.getType(object.__typename);
|
|
77
|
+
const resolver = mergedTypeInfo.resolvers.get(subschema);
|
|
78
|
+
if (resolver) {
|
|
79
|
+
return resolver(object, context, info, subschema, selectionSet, undefined, type);
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
.then(source => finallyFn(source, subschema, selectionSet))
|
|
83
|
+
.catch(error => finallyFn(error, subschema, selectionSet)))).then(() => { });
|
|
79
84
|
}
|
|
@@ -48,7 +48,7 @@ function resolveExternalObject(type, object, unpathedErrors, subschema, context,
|
|
|
48
48
|
if (!targetSubschemas || !targetSubschemas.length) {
|
|
49
49
|
return object;
|
|
50
50
|
}
|
|
51
|
-
return mergeFields(mergedTypeInfo, object, subschema, context, info);
|
|
51
|
+
return mergeFields(mergedTypeInfo, object, subschema, context, info).resolve();
|
|
52
52
|
}
|
|
53
53
|
function resolveExternalList(type, list, unpathedErrors, subschema, context, info, skipTypeMerging) {
|
|
54
54
|
return list.map(listMember => resolveExternalValue(listMember, unpathedErrors, subschema, context, info, type.ofType, skipTypeMerging));
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/delegate",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.34",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/batch-execute": "^8.5.
|
|
10
|
+
"@graphql-tools/batch-execute": "^8.5.21",
|
|
11
11
|
"@graphql-tools/executor": "^0.0.19",
|
|
12
12
|
"@graphql-tools/schema": "^9.0.19",
|
|
13
13
|
"@graphql-tools/utils": "^9.2.1",
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { GraphQLResolveInfo, GraphQLError, GraphQLSchema } from 'graphql';
|
|
2
2
|
import { ExternalObject, MergedTypeInfo, SubschemaConfig } from './types.cjs';
|
|
3
3
|
import { Subschema } from './Subschema.cjs';
|
|
4
|
+
import { ValueOrPromise } from 'value-or-promise';
|
|
4
5
|
export declare function isExternalObject(data: any): data is ExternalObject;
|
|
5
6
|
export declare function annotateExternalObject<TContext>(object: any, errors: Array<GraphQLError>, subschema: GraphQLSchema | SubschemaConfig<any, any, any, TContext> | undefined, subschemaMap: Record<string, GraphQLSchema | SubschemaConfig<any, any, any, Record<string, any>>>): ExternalObject;
|
|
6
7
|
export declare function getSubschema(object: ExternalObject, responseKey: string): GraphQLSchema | SubschemaConfig;
|
|
7
8
|
export declare function getUnpathedErrors(object: ExternalObject): Array<GraphQLError>;
|
|
8
|
-
export declare function mergeFields<TContext>(mergedTypeInfo: MergedTypeInfo, object: any, sourceSubschema: Subschema<any, any, any, TContext>, context: any, info: GraphQLResolveInfo):
|
|
9
|
+
export declare function mergeFields<TContext>(mergedTypeInfo: MergedTypeInfo, object: any, sourceSubschema: Subschema<any, any, any, TContext>, context: any, info: GraphQLResolveInfo): ValueOrPromise<any>;
|
package/typings/mergeFields.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { GraphQLResolveInfo, GraphQLError, GraphQLSchema } from 'graphql';
|
|
2
2
|
import { ExternalObject, MergedTypeInfo, SubschemaConfig } from './types.js';
|
|
3
3
|
import { Subschema } from './Subschema.js';
|
|
4
|
+
import { ValueOrPromise } from 'value-or-promise';
|
|
4
5
|
export declare function isExternalObject(data: any): data is ExternalObject;
|
|
5
6
|
export declare function annotateExternalObject<TContext>(object: any, errors: Array<GraphQLError>, subschema: GraphQLSchema | SubschemaConfig<any, any, any, TContext> | undefined, subschemaMap: Record<string, GraphQLSchema | SubschemaConfig<any, any, any, Record<string, any>>>): ExternalObject;
|
|
6
7
|
export declare function getSubschema(object: ExternalObject, responseKey: string): GraphQLSchema | SubschemaConfig;
|
|
7
8
|
export declare function getUnpathedErrors(object: ExternalObject): Array<GraphQLError>;
|
|
8
|
-
export declare function mergeFields<TContext>(mergedTypeInfo: MergedTypeInfo, object: any, sourceSubschema: Subschema<any, any, any, TContext>, context: any, info: GraphQLResolveInfo):
|
|
9
|
+
export declare function mergeFields<TContext>(mergedTypeInfo: MergedTypeInfo, object: any, sourceSubschema: Subschema<any, any, any, TContext>, context: any, info: GraphQLResolveInfo): ValueOrPromise<any>;
|