From f4c6e41b06ce1d6350c7aa44305472837b88644e Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Sat, 9 Apr 2016 11:41:12 -0700 Subject: [PATCH] Error logging for new interface type semantics --- src/type/__tests__/schema-test.js | 61 +++++++++++++++++++++++++++++++ src/type/schema.js | 9 ++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/type/__tests__/schema-test.js diff --git a/src/type/__tests__/schema-test.js b/src/type/__tests__/schema-test.js new file mode 100644 index 0000000000..881bc221b5 --- /dev/null +++ b/src/type/__tests__/schema-test.js @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +import { + GraphQLSchema, + GraphQLInterfaceType, + GraphQLObjectType, + GraphQLString +} from '../'; + +import { describe, it } from 'mocha'; +import { expect } from 'chai'; + +const InterfaceType = new GraphQLInterfaceType({ + name: 'Interface', + fields: { fieldName: { type: GraphQLString } }, + resolveType() { + return ImplementingType; + } +}); + +const ImplementingType = new GraphQLObjectType({ + name: 'Object', + interfaces: [ InterfaceType ], + fields: { fieldName: { type: GraphQLString, resolve: () => '' }} +}); + +const Schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + getObject: { + type: InterfaceType, + resolve() { + return {}; + } + } + } + }) +}); + +describe('Type System: Schema', () => { + describe('Getting possible types', () => { + it('throws human-reable error if schema.types is not defined', () => { + const checkPossible = () => { + return Schema.isPossibleType(InterfaceType, ImplementingType); + }; + expect(checkPossible).to.throw( + 'Could not find possible implementing types for Interface in schema. ' + + 'Check that schema.types is defined and is an array ofall possible ' + + 'types in the schema.' + ); + }); + }); +}); diff --git a/src/type/schema.js b/src/type/schema.js index ddbc3e8f16..bfb097313d 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -183,8 +183,15 @@ export class GraphQLSchema { } if (!possibleTypeMap[abstractType.name]) { + const possibleTypes = this.getPossibleTypes(abstractType); + invariant( + Array.isArray(possibleTypes), + `Could not find possible implementing types for ${abstractType} in ` + + 'schema. Check that schema.types is defined and is an array of' + + 'all possible types in the schema.' + ); possibleTypeMap[abstractType.name] = - this.getPossibleTypes(abstractType).reduce( + possibleTypes.reduce( (map, type) => ((map[type.name] = true), map), Object.create(null) );