Skip to content

Commit a6a3d4d

Browse files
authored
fix(federation): keep the original subgraph reference while handling shared root resolution (#1533)
1 parent 3c893b1 commit a6a3d4d

File tree

14 files changed

+2516
-6
lines changed

14 files changed

+2516
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/federation': patch
3+
---
4+
5+
Fix shared root handling in case of heavily nested selections

packages/federation/src/supergraph.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import {
22
BatchingOptions,
33
delegateToSchema,
44
extractUnavailableFieldsFromSelectionSet,
5+
FIELD_SUBSCHEMA_MAP_SYMBOL,
56
getTypeInfo,
67
isExternalObject,
78
MergedFieldConfig,
89
MergedTypeConfig,
10+
OBJECT_SUBSCHEMA_SYMBOL,
911
SubschemaConfig,
1012
subtractSelectionSets,
1113
Transform,
@@ -1241,7 +1243,7 @@ export function getStitchingOptionsFromSupergraphSdl(
12411243
if (operationType) {
12421244
const defaultMergedField = defaultMerger(candidates);
12431245
const mergedResolver: GraphQLFieldResolver<{}, {}> =
1244-
function mergedResolver(_root, _args, context, info) {
1246+
function mergedResolver(_root, args, context, info) {
12451247
const originalSelectionSet: SelectionSetNode = {
12461248
kind: Kind.SELECTION_SET,
12471249
selections: info.fieldNodes,
@@ -1339,6 +1341,7 @@ export function getStitchingOptionsFromSupergraphSdl(
13391341
rootTypeMap.get(info.parentType.name) ||
13401342
('query' as OperationTypeNode),
13411343
context,
1344+
args,
13421345
info: currentFriendSubschemas?.size
13431346
? {
13441347
...info,
@@ -1367,6 +1370,7 @@ export function getStitchingOptionsFromSupergraphSdl(
13671370
rootTypeMap.get(info.parentType.name) ||
13681371
('query' as OperationTypeNode),
13691372
context,
1373+
args,
13701374
info: {
13711375
...info,
13721376
fieldNodes: friendSelectionSet.selections as FieldNode[],
@@ -1676,11 +1680,26 @@ function mergeResults(results: unknown[], getFieldNames: () => Set<string>) {
16761680
if (datas.length === 1) {
16771681
return makeExternalObject(datas[0], errors, getFieldNames);
16781682
}
1679-
return makeExternalObject(
1680-
mergeDeep(datas, undefined, true, true),
1681-
errors,
1682-
getFieldNames,
1683-
);
1683+
const mergedData = mergeDeep(datas, undefined, true, true);
1684+
// Put original symbols on the merged object
1685+
const symbols = [
1686+
OBJECT_SUBSCHEMA_SYMBOL,
1687+
FIELD_SUBSCHEMA_MAP_SYMBOL,
1688+
UNPATHED_ERRORS_SYMBOL,
1689+
];
1690+
for (const symbol of symbols) {
1691+
if (mergedData?.[symbol] == null) {
1692+
for (const data of datas) {
1693+
// @ts-expect-error - we know it is there
1694+
const symbolValue = data?.[symbol];
1695+
if (symbolValue != null) {
1696+
mergedData[symbol] = symbolValue;
1697+
break;
1698+
}
1699+
}
1700+
}
1701+
}
1702+
return makeExternalObject(mergedData, errors, getFieldNames);
16841703
}
16851704
if (errors.length) {
16861705
if (errors.length === 1) {

packages/federation/tests/fixtures/many-plans/expected.json

Lines changed: 430 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)