Skip to content

Commit

Permalink
Error logging for new interface type semantics (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp authored and leebyron committed May 5, 2016
1 parent 51362e7 commit e6e8d19
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
@@ -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.'
);
});
});
});
9 changes: 8 additions & 1 deletion src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down

0 comments on commit e6e8d19

Please sign in to comment.