Skip to content

Commit

Permalink
schema: fix error message when provided type has no name (#2475)
Browse files Browse the repository at this point in the history
Avoid confusing error messages about "multiple types named undefined".
  • Loading branch information
Cito authored Mar 6, 2020
1 parent 905b7f3 commit 63c2009
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,19 @@ describe('Type System: Schema', () => {
);
});

it('rejects a Schema when a provided type has no name', () => {
const query = new GraphQLObjectType({
name: 'Query',
fields: { foo: { type: GraphQLString } },
});
const types = [{}, query, {}];

// $DisableFlowOnNegativeTest
expect(() => new GraphQLSchema({ query, types })).to.throw(
'One of the provided types for building the Schema is missing a name.',
);
});

it('rejects a Schema which defines an object type twice', () => {
const types = [
new GraphQLObjectType({ name: 'SameName', fields: {} }),
Expand Down
4 changes: 4 additions & 0 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export class GraphQLSchema {
}

const typeName = namedType.name;
devAssert(
typeName,
'One of the provided types for building the Schema is missing a name.',
);
if (this._typeMap[typeName] !== undefined) {
throw new Error(
`Schema must contain uniquely named types but contains multiple types named "${typeName}".`,
Expand Down

0 comments on commit 63c2009

Please sign in to comment.