Skip to content

Commit 85d7c95

Browse files
authored
Handle nested keys with arguments (#1187)
1 parent a50da5c commit 85d7c95

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

.changeset/polite-beers-beg.md

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+
Handle nested keys with arguments

packages/federation/src/supergraph.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,35 @@ export function getStitchingOptionsFromSupergraphSdl(
797797
const fieldsConfig: Record<string, MergedFieldConfig> =
798798
(mergedTypeConfig.fields = {});
799799
for (const [fieldName, fieldNameKey] of fieldsKeyMap) {
800-
const aliasedFieldNameKey = fieldNameKey.includes('(')
801-
? `_${fieldNameKey.split('(')[0]}: ${fieldNameKey}`
802-
: fieldNameKey;
803-
extraKeys.add(aliasedFieldNameKey);
800+
const selectionSetNode = parseSelectionSet(`{${fieldNameKey}}`);
801+
(function aliasFieldsWithArgs(selectionSetNode: SelectionSetNode) {
802+
for (const selection of selectionSetNode.selections) {
803+
if (
804+
selection.kind === Kind.FIELD &&
805+
selection.arguments?.length
806+
) {
807+
// @ts-expect-error it's ok we're mutating consciously
808+
selection.alias = {
809+
kind: Kind.NAME,
810+
value: '_' + selection.name.value,
811+
};
812+
}
813+
if ('selectionSet' in selection && selection.selectionSet) {
814+
aliasFieldsWithArgs(selection.selectionSet);
815+
}
816+
}
817+
})(selectionSetNode);
818+
const selectionSet = print(selectionSetNode)
819+
// remove new lines
820+
.replaceAll(/\n/g, ' ')
821+
// remove extra spaces (only one space between tokens)
822+
.replaceAll(/\s+/g, ' ');
823+
extraKeys.add(
824+
// remove first and last characters (curly braces)
825+
selectionSet.slice(1, -1),
826+
);
804827
fieldsConfig[fieldName] = {
805-
selectionSet: `{ ${aliasedFieldNameKey} }`,
828+
selectionSet,
806829
computed: true,
807830
};
808831
}

packages/fusion-runtime/src/unifiedGraphManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export class UnifiedGraphManager<TContext> implements AsyncDisposable {
403403
}
404404

405405
public getUnifiedGraph(): MaybePromise<GraphQLSchema> {
406+
// TODO: error is not bubbled up here
406407
return handleMaybePromise(
407408
() => this.ensureUnifiedGraph(),
408409
() => {

0 commit comments

Comments
 (0)