Skip to content

Commit 1fbd7ec

Browse files
IvanGoncharovleebyron
authored andcommitted
Throws descriptive error when non-type used instead of interface (#1282)
1 parent fb27b92 commit 1fbd7ec

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/type/__tests__/validation-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,22 @@ describe('Type System: Object fields must have output types', () => {
824824
});
825825

826826
describe('Type System: Objects can only implement unique interfaces', () => {
827+
it('rejects an Object implementing a non-type values', () => {
828+
const schema = new GraphQLSchema({
829+
query: new GraphQLObjectType({
830+
name: 'BadObject',
831+
interfaces: [undefined],
832+
}),
833+
});
834+
835+
expect(validateSchema(schema)).to.containSubset([
836+
{
837+
message:
838+
'Type BadObject must only implement Interface types, it cannot implement undefined.',
839+
},
840+
]);
841+
});
842+
827843
it('rejects an Object implementing a non-Interface type', () => {
828844
const schema = buildSchema(`
829845
type Query {

src/type/schema.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ export class GraphQLSchema {
152152
const type = this._typeMap[typeName];
153153
if (isObjectType(type)) {
154154
type.getInterfaces().forEach(iface => {
155-
const impls = this._implementations[iface.name];
156-
if (impls) {
157-
impls.push(type);
158-
} else {
159-
this._implementations[iface.name] = [type];
155+
if (isInterfaceType(iface)) {
156+
const impls = this._implementations[iface.name];
157+
if (impls) {
158+
impls.push(type);
159+
} else {
160+
this._implementations[iface.name] = [type];
161+
}
160162
}
161163
});
162164
}

src/type/validate.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ function validateObjectInterfaces(
343343
): void {
344344
const implementedTypeNames = Object.create(null);
345345
object.getInterfaces().forEach(iface => {
346+
if (!isInterfaceType(iface)) {
347+
context.reportError(
348+
`Type ${String(object)} must only implement Interface types, ` +
349+
`it cannot implement ${String(iface)}.`,
350+
getImplementsInterfaceNode(object, iface),
351+
);
352+
return;
353+
}
354+
346355
if (implementedTypeNames[iface.name]) {
347356
context.reportError(
348357
`Type ${object.name} can only implement ${iface.name} once.`,
@@ -360,15 +369,6 @@ function validateObjectImplementsInterface(
360369
object: GraphQLObjectType,
361370
iface: GraphQLInterfaceType,
362371
): void {
363-
if (!isInterfaceType(iface)) {
364-
context.reportError(
365-
`Type ${String(object)} must only implement Interface types, ` +
366-
`it cannot implement ${String(iface)}.`,
367-
getImplementsInterfaceNode(object, iface),
368-
);
369-
return;
370-
}
371-
372372
const objectFieldMap = object.getFields();
373373
const ifaceFieldMap = iface.getFields();
374374

0 commit comments

Comments
 (0)