From 63c20097fb28e296066f6654d715456668d91d9c Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Fri, 6 Mar 2020 14:43:52 +0100 Subject: [PATCH] schema: fix error message when provided type has no name (#2475) Avoid confusing error messages about "multiple types named undefined". --- src/type/__tests__/schema-test.js | 13 +++++++++++++ src/type/schema.js | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/type/__tests__/schema-test.js b/src/type/__tests__/schema-test.js index 57dc1d1310..f5f558799b 100644 --- a/src/type/__tests__/schema-test.js +++ b/src/type/__tests__/schema-test.js @@ -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: {} }), diff --git a/src/type/schema.js b/src/type/schema.js index 5cb5241547..2b42870374 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -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}".`,