Skip to content

Commit 5e17f83

Browse files
authored
Merge pull request #813 from APIs-guru/uniq_union
Forbid to define duplicated member types in Union
2 parents 126cd6d + 7893dba commit 5e17f83

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/type/__tests__/validation-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,19 @@ describe('Type System: Union types must be array', () => {
637637
);
638638
});
639639

640+
it('rejects a Union type with duplicated member type', () => {
641+
expect(
642+
() => schemaWithFieldType(new GraphQLUnionType({
643+
name: 'SomeUnion',
644+
resolveType: () => null,
645+
types: [
646+
SomeObjectType,
647+
SomeObjectType,
648+
],
649+
}))
650+
).to.throw('SomeUnion can include SomeObject type only once.');
651+
});
652+
640653
});
641654

642655

src/type/definition.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,12 +789,18 @@ function defineTypes(
789789
'Must provide Array of types or a function which returns ' +
790790
`such an array for Union ${unionType.name}.`
791791
);
792+
const includedTypeNames = {};
792793
types.forEach(objType => {
793794
invariant(
794795
objType instanceof GraphQLObjectType,
795796
`${unionType.name} may only contain Object types, it cannot contain: ` +
796797
`${String(objType)}.`
797798
);
799+
invariant(
800+
!includedTypeNames[objType.name],
801+
`${unionType.name} can include ${objType.name} type only once.`
802+
);
803+
includedTypeNames[objType.name] = true;
798804
if (typeof unionType.resolveType !== 'function') {
799805
invariant(
800806
typeof objType.isTypeOf === 'function',

0 commit comments

Comments
 (0)