Skip to content

Commit 5a9d276

Browse files
authored
Fix patch duplicating schema definition nodes (#2937)
1 parent f8e637f commit 5a9d276

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

.changeset/few-beans-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-inspector/patch': patch
3+
---
4+
5+
Fix duplicated schema definitions

packages/patch/__tests__/directives.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { parse, print } from 'graphql';
2+
import { Change } from '@graphql-inspector/core';
3+
import { errors, patch } from '../src/index.js';
14
import {
25
expectDiffAndPatchToMatch,
36
expectDiffAndPatchToPass,
@@ -476,4 +479,25 @@ describe('repeat directives', () => {
476479
`;
477480
await expectDiffAndPatchToMatch(before, after);
478481
});
482+
483+
test('Schema Extensions can be used with directives', async () => {
484+
const before = `
485+
extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@external"])
486+
487+
type Query {
488+
text: Boolean
489+
}
490+
`;
491+
492+
const changes: Change<any>[] = [];
493+
494+
expect(print(patch(parse(before), changes, { onError: errors.looseErrorHandler })))
495+
.toMatchInlineSnapshot(`
496+
"extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@external"])
497+
498+
type Query {
499+
text: Boolean
500+
}"
501+
`);
502+
});
479503
});

packages/patch/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,12 @@ export function patchCoordinatesAST(
530530
// filter out the non-definition nodes (e.g. field definitions)
531531
definitions: [
532532
...schemaNodes,
533-
...Array.from(nodesByCoordinate.values()).filter(isDefinitionNode),
533+
...Array.from(nodesByCoordinate.values()).filter(
534+
n =>
535+
isDefinitionNode(n) &&
536+
n.kind !== Kind.SCHEMA_EXTENSION &&
537+
n.kind !== Kind.SCHEMA_DEFINITION,
538+
),
534539
],
535540
};
536541
}

0 commit comments

Comments
 (0)