Skip to content

Commit 1a6251d

Browse files
cdaringetheguild-botgithub-actions[bot]ardatan
authored
fix: link merging (#6964)
* chore(release): update monorepo packages versions (#6826) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * fix: link merging * Format --------- Co-authored-by: TheGuildBot <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Arda TANRIKULU <[email protected]>
1 parent faf6853 commit 1a6251d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/merge/src/typedefs-mergers/directives.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ const matchValues = (a: ValueNode, b: ValueNode): boolean => {
8484
return false;
8585
};
8686

87+
const isLinkDirective = (directive: DirectiveNode): boolean => directive.name.value === 'link';
88+
const getLinkDirectiveURL = (directive: DirectiveNode): string | undefined => {
89+
const stringValue = isLinkDirective(directive)
90+
? directive.arguments?.find(arg => arg.name.value === 'url')?.value
91+
: undefined;
92+
return stringValue?.kind === 'StringValue' ? stringValue.value : undefined;
93+
};
94+
8795
const matchArguments = (a: ArgumentNode, b: ArgumentNode): boolean =>
8896
a.name.value === b.name.value && a.value.kind === b.value.kind && matchValues(a.value, b.value);
8997

@@ -124,6 +132,15 @@ export function mergeDirectives(
124132
// if did not find a directive with this name on the result set already
125133
result.push(directive);
126134
} else {
135+
if (isLinkDirective(directive) && isLinkDirective(result[firstAt])) {
136+
const url1 = getLinkDirectiveURL(directive);
137+
const url2 = getLinkDirectiveURL(result[firstAt]);
138+
// if both are link directives but with different urls, do not merge them
139+
if (url1 && url2 && url1 !== url2) {
140+
result.push(directive);
141+
continue;
142+
}
143+
}
127144
// if not repeatable and found directive with the same name already in the result set,
128145
// then merge the arguments of the existing directive and the new directive
129146
const mergedArguments = mergeArguments(

packages/merge/tests/merge-typedefs.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,39 @@ describe('Merge TypeDefs', () => {
17491749
expect(reformulatedGraphQL).toBeSimilarString(schemaWithDescription);
17501750
});
17511751

1752+
it('merges the directives with the same name and same arguments (@link)', () => {
1753+
const schema1 = parse(/* GraphQL */ `
1754+
extend schema
1755+
@link(
1756+
url: "https://specs.apollo.dev/federation/v2.3"
1757+
import: ["@composeDirective", "@external", "@foo"]
1758+
)
1759+
`);
1760+
1761+
const schema2 = parse(/* GraphQL */ `
1762+
extend schema
1763+
@link(
1764+
url: "https://specs.apollo.dev/federation/v2.3"
1765+
import: ["@composeDirective", "@external"]
1766+
)
1767+
@link(url: "file://foo.org/trackable/v2.3", import: ["@trackable"])
1768+
`);
1769+
const typeDefs = [schema1, schema2];
1770+
const merged = mergeTypeDefs(typeDefs);
1771+
const prettyOutput = print(merged);
1772+
const prettyExpected = print(
1773+
parse(/* GraphQL */ `
1774+
extend schema
1775+
@link(
1776+
url: "https://specs.apollo.dev/federation/v2.3"
1777+
import: ["@composeDirective", "@external", "@foo"]
1778+
)
1779+
@link(url: "file://foo.org/trackable/v2.3", import: ["@trackable"]) # unique to schema 2
1780+
`),
1781+
);
1782+
expect(prettyOutput).toBeSimilarString(prettyExpected);
1783+
});
1784+
17521785
it('merges the directives with the same name and same arguments', () => {
17531786
const directive = parse(/* GraphQL */ `
17541787
directive @link(
@@ -1762,7 +1795,6 @@ describe('Merge TypeDefs', () => {
17621795
const merged = mergeTypeDefs(typeDefs);
17631796
expect(print(merged)).toBeSimilarString(print(directive));
17641797
});
1765-
17661798
it('does not merge repeatable Federation directives without the same arguments', () => {
17671799
const ast = parse(/* GraphQL */ `
17681800
extend schema

0 commit comments

Comments
 (0)