diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index ee5ca318ad..5755ead544 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -1198,90 +1198,3 @@ describe('Type System: NonNull must only accept non-nullable types', () => { }); } }); - -describe('Type System: A Schema must contain uniquely named types', () => { - it('rejects a Schema which redefines a built-in type', () => { - expect(() => { - const FakeString = new GraphQLScalarType({ - name: 'String', - serialize: () => null, - }); - - const QueryType = new GraphQLObjectType({ - name: 'Query', - fields: { - normal: { type: GraphQLString }, - fake: { type: FakeString }, - }, - }); - - return new GraphQLSchema({ query: QueryType }); - }).to.throw( - 'Schema must contain unique named types but contains multiple types ' + - 'named "String".', - ); - }); - - it('rejects a Schema which defines an object type twice', () => { - expect(() => { - const A = new GraphQLObjectType({ - name: 'SameName', - fields: { f: { type: GraphQLString } }, - }); - - const B = new GraphQLObjectType({ - name: 'SameName', - fields: { f: { type: GraphQLString } }, - }); - - const QueryType = new GraphQLObjectType({ - name: 'Query', - fields: { - a: { type: A }, - b: { type: B }, - }, - }); - - return new GraphQLSchema({ query: QueryType }); - }).to.throw( - 'Schema must contain unique named types but contains multiple types ' + - 'named "SameName".', - ); - }); - - it('rejects a Schema which have same named objects implementing an interface', () => { - expect(() => { - const AnotherInterface = new GraphQLInterfaceType({ - name: 'AnotherInterface', - fields: { f: { type: GraphQLString } }, - }); - - const FirstBadObject = new GraphQLObjectType({ - name: 'BadObject', - interfaces: [AnotherInterface], - fields: { f: { type: GraphQLString } }, - }); - - const SecondBadObject = new GraphQLObjectType({ - name: 'BadObject', - interfaces: [AnotherInterface], - fields: { f: { type: GraphQLString } }, - }); - - const QueryType = new GraphQLObjectType({ - name: 'Query', - fields: { - iface: { type: AnotherInterface }, - }, - }); - - return new GraphQLSchema({ - query: QueryType, - types: [FirstBadObject, SecondBadObject], - }); - }).to.throw( - 'Schema must contain unique named types but contains multiple types ' + - 'named "BadObject".', - ); - }); -}); diff --git a/src/type/__tests__/schema-test.js b/src/type/__tests__/schema-test.js index 5d86d8d298..124fc657d0 100644 --- a/src/type/__tests__/schema-test.js +++ b/src/type/__tests__/schema-test.js @@ -9,6 +9,7 @@ import { GraphQLSchema, + GraphQLScalarType, GraphQLInterfaceType, GraphQLObjectType, GraphQLString, @@ -109,6 +110,53 @@ describe('Type System: Schema', () => { }); }); + describe('A Schema must contain uniquely named types', () => { + it('rejects a Schema which redefines a built-in type', () => { + const FakeString = new GraphQLScalarType({ + name: 'String', + serialize: () => null, + }); + + const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + normal: { type: GraphQLString }, + fake: { type: FakeString }, + }, + }); + + expect(() => new GraphQLSchema({ query: QueryType })).to.throw( + 'Schema must contain unique named types but contains multiple types named "String".', + ); + }); + + it('rejects a Schema which defines an object type twice', () => { + const types = [ + new GraphQLObjectType({ name: 'SameName', fields: {} }), + new GraphQLObjectType({ name: 'SameName', fields: {} }), + ]; + + expect(() => new GraphQLSchema({ types })).to.throw( + 'Schema must contain unique named types but contains multiple types named "SameName".', + ); + }); + + it('rejects a Schema which defines fields with conflicting types', () => { + const fields = {}; + const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + a: { type: new GraphQLObjectType({ name: 'SameName', fields }) }, + b: { type: new GraphQLObjectType({ name: 'SameName', fields }) }, + }, + }); + + expect(() => new GraphQLSchema({ query: QueryType })).to.throw( + 'Schema must contain unique named types but contains multiple types named "SameName".', + ); + }); + }); + describe('when assumed valid', () => { it('configures the schema to have no errors', () => { expect(