Skip to content

RFC: Enforce Interfaces having at least one implementing Object #1377

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ describe('Type System: Interface fields must have output types', () => {
]);
});

it('accepts an interface not implemented by at least one object', () => {
it('rejects an interface not implemented by at least one object', () => {
const schema = buildSchema(`
type Query {
test: SomeInterface
Expand All @@ -1215,7 +1215,13 @@ describe('Type System: Interface fields must have output types', () => {
foo: String
}
`);
expect(validateSchema(schema)).to.deep.equal([]);
expect(validateSchema(schema)).to.deep.equal([
{
message:
'Interface SomeInterface must be implemented by at least one Object type.',
locations: [{ line: 6, column: 7 }],
},
]);
});
});

Expand Down
18 changes: 18 additions & 0 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ function validateTypes(context: SchemaValidationContext): void {
} else if (isInterfaceType(type)) {
// Ensure fields are valid.
validateFields(context, type);

// Ensure Interfaces include at least 1 Object type.
validateInterfaces(context, type);
} else if (isUnionType(type)) {
// Ensure Unions include valid member types.
validateUnionMembers(context, type);
Expand Down Expand Up @@ -364,6 +367,21 @@ function validateObjectInterfaces(
});
}

function validateInterfaces(
context: SchemaValidationContext,
iface: GraphQLInterfaceType,
): void {
const possibleTypes = context.schema.getPossibleTypes(iface);

if (possibleTypes.length === 0) {
context.reportError(
`Interface ${iface.name} must be implemented ` +
`by at least one Object type.`,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe expand the error message a little?

Interface ${iface.name} must be implemented by at least one Object type. An interface that does not have any implementation can never be instantiated.

iface.astNode,
);
}
}

function validateObjectImplementsInterface(
context: SchemaValidationContext,
object: GraphQLObjectType,
Expand Down