From 2e04913fac23e031e10346c166abb02401c96663 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 8 Mar 2018 17:14:14 +0200 Subject: [PATCH] Throws descriptive error when non-type used instead of interface --- src/type/__tests__/validation-test.js | 16 ++++++++++++++++ src/type/schema.js | 12 +++++++----- src/type/validate.js | 18 +++++++++--------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/type/__tests__/validation-test.js b/src/type/__tests__/validation-test.js index 2105f1bda3..19f13a336b 100644 --- a/src/type/__tests__/validation-test.js +++ b/src/type/__tests__/validation-test.js @@ -824,6 +824,22 @@ describe('Type System: Object fields must have output types', () => { }); describe('Type System: Objects can only implement unique interfaces', () => { + it('rejects an Object implementing a non-type values', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'BadObject', + interfaces: [undefined], + }), + }); + + expect(validateSchema(schema)).to.containSubset([ + { + message: + 'Type BadObject must only implement Interface types, it cannot implement undefined.', + }, + ]); + }); + it('rejects an Object implementing a non-Interface type', () => { const schema = buildSchema(` type Query { diff --git a/src/type/schema.js b/src/type/schema.js index 1b2ea738d1..e0822d0cf5 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -152,11 +152,13 @@ export class GraphQLSchema { const type = this._typeMap[typeName]; if (isObjectType(type)) { type.getInterfaces().forEach(iface => { - const impls = this._implementations[iface.name]; - if (impls) { - impls.push(type); - } else { - this._implementations[iface.name] = [type]; + if (isInterfaceType(iface)) { + const impls = this._implementations[iface.name]; + if (impls) { + impls.push(type); + } else { + this._implementations[iface.name] = [type]; + } } }); } diff --git a/src/type/validate.js b/src/type/validate.js index d65b0dc87c..812d869fc4 100644 --- a/src/type/validate.js +++ b/src/type/validate.js @@ -343,6 +343,15 @@ function validateObjectInterfaces( ): void { const implementedTypeNames = Object.create(null); object.getInterfaces().forEach(iface => { + if (!isInterfaceType(iface)) { + context.reportError( + `Type ${String(object)} must only implement Interface types, ` + + `it cannot implement ${String(iface)}.`, + getImplementsInterfaceNode(object, iface), + ); + return; + } + if (implementedTypeNames[iface.name]) { context.reportError( `Type ${object.name} can only implement ${iface.name} once.`, @@ -360,15 +369,6 @@ function validateObjectImplementsInterface( object: GraphQLObjectType, iface: GraphQLInterfaceType, ): void { - if (!isInterfaceType(iface)) { - context.reportError( - `Type ${String(object)} must only implement Interface types, ` + - `it cannot implement ${String(iface)}.`, - getImplementsInterfaceNode(object, iface), - ); - return; - } - const objectFieldMap = object.getFields(); const ifaceFieldMap = iface.getFields();