Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws descriptive error when non-type used instead of interface #1282

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
});
}
Expand Down
18 changes: 9 additions & 9 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand All @@ -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();

Expand Down