Skip to content

Commit ca7b3cd

Browse files
committed
Error logging for new interface type semantics
1 parent dd02973 commit ca7b3cd

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/type/__tests__/schema-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import {
11+
GraphQLSchema,
12+
GraphQLInterfaceType,
13+
GraphQLObjectType,
14+
GraphQLString
15+
} from '../';
16+
17+
import { describe, it } from 'mocha';
18+
import { expect } from 'chai';
19+
20+
const InterfaceType = new GraphQLInterfaceType({
21+
name : 'Interface',
22+
fields : { fieldName : { type : GraphQLString } },
23+
resolveType() {
24+
return ImplementingType;
25+
}
26+
});
27+
28+
const ImplementingType = new GraphQLObjectType({
29+
name : 'Object',
30+
interfaces : [InterfaceType],
31+
fields : { fieldName : { type: GraphQLString, resolve: () => '' }}
32+
});
33+
34+
const Schema = new GraphQLSchema({
35+
query: new GraphQLObjectType({
36+
name: 'Query',
37+
fields : {
38+
getObject : {
39+
type : InterfaceType,
40+
resolve() {
41+
return {};
42+
}
43+
}
44+
}
45+
})
46+
});
47+
48+
describe('Type System: Schema', () => {
49+
describe('Getting possible types', () => {
50+
it('throws human-reable error if schema.types is not defined', () => {
51+
const checkPossible = () => {
52+
return Schema.isPossibleType(InterfaceType, ImplementingType);
53+
};
54+
expect(checkPossible).to.throw(
55+
'Could not find possible implementing types for Interface in schema. ' +
56+
'Check that schema.types is defined and is an array ofall possible ' +
57+
'types in the schema.'
58+
);
59+
});
60+
});
61+
});

src/type/schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,15 @@ export class GraphQLSchema {
183183
}
184184

185185
if (!possibleTypeMap[abstractType.name]) {
186+
const possibleTypes = this.getPossibleTypes(abstractType);
187+
invariant(
188+
Array.isArray(possibleTypes),
189+
`Could not find possible implementing types for ${abstractType} in ` +
190+
`schema. Check that schema.types is defined and is an array of` +
191+
`all possible types in the schema.`
192+
);
186193
possibleTypeMap[abstractType.name] =
187-
this.getPossibleTypes(abstractType).reduce(
194+
possibleTypes.reduce(
188195
(map, type) => ((map[type.name] = true), map),
189196
Object.create(null)
190197
);

0 commit comments

Comments
 (0)