Skip to content

Commit

Permalink
fix: check directive isNode on merge if inherited from obj (#5973)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleydreyer authored Mar 11, 2024
1 parent a01dd1b commit 0b43440
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/merge/src/typedefs-mergers/merge-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SchemaDefinitionNode,
SchemaExtensionNode,
} from 'graphql';
import { isNode } from 'graphql/language/ast.js';
import { collectComment, NamedDefinitionNode } from '@graphql-tools/utils';
import { mergeDirective } from './directives.js';
import { mergeEnum } from './enum.js';
Expand Down Expand Up @@ -104,6 +105,14 @@ export function mergeGraphQLNodes(
);
break;
case Kind.DIRECTIVE_DEFINITION:
if (mergedResultMap[name]) {
const isInheritedFromPrototype = name in {};
if (isInheritedFromPrototype) {
if (!isNode(mergedResultMap[name])) {
mergedResultMap[name] = undefined as any;
}
}
}
mergedResultMap[name] = mergeDirective(nodeDefinition, mergedResultMap[name] as any);
break;
}
Expand Down
21 changes: 21 additions & 0 deletions packages/merge/tests/merge-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,27 @@ describe('Merge Nodes', () => {
'Unable to merge GraphQL type "A": Field "f1" already defined with a different type. Declared as "String", but you tried to override with "Int"',
);
});

it('Should merge GraphQL Types and merge directives (when the directive is inherited from the Object prototype)', () => {
const type1 = parse(/* GraphQL */ `
type A @toString {
f1: String
}
`);
const type2 = parse(/* GraphQL */ `
type A @test2 {
f2: Int
}
`);
const merged = mergeGraphQLNodes([...type1.definitions, ...type2.definitions]);
const type = merged['A'];
assertObjectTypeDefinitionNode(type);
assertSome(type.directives);

expect(type.directives.length).toBe(2);
expect(type.directives[0].name.value).toBe('toString');
expect(type.directives[1].name.value).toBe('test2');
});
});

describe('enum', () => {
Expand Down

0 comments on commit 0b43440

Please sign in to comment.