Skip to content

Commit

Permalink
Forbid to define duplicated member types in Union
Browse files Browse the repository at this point in the history
For details please see this PR: graphql/graphql-spec#266
  • Loading branch information
IvanGoncharov committed Apr 17, 2017
1 parent bacd412 commit e13834b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,19 @@ describe('Type System: Union types must be array', () => {
);
});

it('rejects a Union type with duplicated member type', () => {
expect(
() => schemaWithFieldType(new GraphQLUnionType({
name: 'SomeUnion',
resolveType: () => null,
types: [
SomeObjectType,
SomeObjectType,
],
}))
).to.throw('SomeUnion can include SomeObject type only once.');
});

});


Expand Down
11 changes: 11 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,18 @@ function defineTypes(
'Must provide Array of types or a function which returns ' +
`such an array for Union ${unionType.name}.`
);
const seenObjectNames = [];
types.forEach(objType => {
invariant(
objType instanceof GraphQLObjectType,
`${unionType.name} may only contain Object types, it cannot contain: ` +
`${String(objType)}.`
);
invariant(
seenObjectNames.indexOf(objType.name) === -1,
`${unionType.name} can include ${objType.name} type only once.`
);
seenObjectNames.push(objType.name);
if (typeof unionType.resolveType !== 'function') {
invariant(
typeof objType.isTypeOf === 'function',
Expand Down Expand Up @@ -942,6 +948,11 @@ function defineEnumValues(
);
return valueNames.map(valueName => {
assertValidName(valueName);
invariant(
[ 'true', 'false', 'null' ].indexOf(valueName) === -1,
`Name "${valueName}" is can not be used for Enum value.`
);

const value = valueMap[valueName];
invariant(
isPlainObj(value),
Expand Down

0 comments on commit e13834b

Please sign in to comment.