Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: link merging #6964

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/merge/src/typedefs-mergers/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@ import {
import { isSome } from '@graphql-tools/utils';
import { Config } from './merge-typedefs.js';

const isLinkDirective = (directive: DirectiveNode): boolean => directive.name.value === 'link';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we already have AST narrowing fns. Further, I didn't see a LinkDirective type, which woulda been nice. Not going to add anything like that without discussion :)


const getLinkDirectiveURL = (directive: DirectiveNode): string | undefined => {
const stringValue = isLinkDirective(directive)
? directive.arguments?.find(arg => arg.name.value === 'url')?.value
: undefined;
return stringValue?.kind === 'StringValue' ? stringValue.value : undefined;
};

/**
* Directives by default do not carry distinctual metadata, thus are not
* considered distinct by default by this function. However @link directives are
* only considered distinct if they have different URLs.
*/
const isDirectiveMetadataDistinct = (directiveA: DirectiveNode, directiveB: DirectiveNode) => {
const isLinkPair = isLinkDirective(directiveA) && isLinkDirective(directiveB);
return isLinkPair ? getLinkDirectiveURL(directiveA) !== getLinkDirectiveURL(directiveB) : false;
};

const isMatchingDirective = (a: DirectiveNode, b: DirectiveNode) => a.name.value === b.name.value;

function directiveAlreadyExists(
directivesArr: ReadonlyArray<DirectiveNode>,
otherDirective: DirectiveNode,
): boolean {
return !!directivesArr.find(directive => directive.name.value === otherDirective.name.value);
return !!directivesArr.find(directive => {
const isDirectiveNameMatching = directive.name.value === otherDirective.name.value;
return isDirectiveNameMatching && !isDirectiveMetadataDistinct(directive, otherDirective);
});
}

function isRepeatableDirective(
Expand Down Expand Up @@ -98,7 +122,7 @@ export function mergeDirectives(
directiveAlreadyExists(result, directive) &&
!isRepeatableDirective(directive, directives)
) {
const existingDirectiveIndex = result.findIndex(d => d.name.value === directive.name.value);
const existingDirectiveIndex = result.findIndex(it => isMatchingDirective(it, directive));
const existingDirective = result[existingDirectiveIndex];
(result[existingDirectiveIndex] as any).arguments = mergeArguments(
directive.arguments || [],
Expand Down
38 changes: 29 additions & 9 deletions packages/merge/tests/merge-typedefs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1704,17 +1704,37 @@ describe('Merge TypeDefs', () => {
});
expect(reformulatedGraphQL).toBeSimilarString(schemaWithDescription);
});

it('merges the directives with the same name and same arguments', () => {
const directive = parse(/* GraphQL */ `
directive @link(
url: String!
as: String
import: [link__Import]
for: link__Purpose
) on SCHEMA
const schema1 = parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@composeDirective", "@external", "@foo"]
)
`);

const schema2 = parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@composeDirective", "@external"]
)
@link(url: "file://foo.org/trackable/v2.3", import: ["@trackable"])
`);
const typeDefs = [directive, directive];
const typeDefs = [schema1, schema2];
const merged = mergeTypeDefs(typeDefs);
expect(print(merged)).toBeSimilarString(print(directive));
const prettyOutput = print(merged);
const prettyExpected = print(
parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@composeDirective", "@external", "@foo"]
)
@link(url: "file://foo.org/trackable/v2.3", import: ["@trackable"]) # unique to schema 2
`),
);
expect(prettyOutput).toBeSimilarString(prettyExpected);
});
});
Loading