Skip to content

Commit e36c6a8

Browse files
committed
RFC: Enforce Interfaces having at least one implementing Object
It is unclear whether the spec validation from graphql/graphql-spec#424 as implemented by #1277 is what we want to have for the GraphQL Schema long term. This RFC lets us explore that option more, to see whether the breaking schema change is the right path forward.
1 parent 9925e50 commit e36c6a8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/type/__tests__/validation-test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ describe('Type System: Interface fields must have output types', () => {
12051205
]);
12061206
});
12071207

1208-
it('accepts an interface not implemented by at least one object', () => {
1208+
it('rejects an interface not implemented by at least one object', () => {
12091209
const schema = buildSchema(`
12101210
type Query {
12111211
test: SomeInterface
@@ -1215,7 +1215,13 @@ describe('Type System: Interface fields must have output types', () => {
12151215
foo: String
12161216
}
12171217
`);
1218-
expect(validateSchema(schema)).to.deep.equal([]);
1218+
expect(validateSchema(schema)).to.deep.equal([
1219+
{
1220+
message:
1221+
'Interface SomeInterface must be implemented by at least one Object type.',
1222+
locations: [{ line: 6, column: 7 }],
1223+
},
1224+
]);
12191225
});
12201226
});
12211227

src/type/validate.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ function validateTypes(context: SchemaValidationContext): void {
257257
} else if (isInterfaceType(type)) {
258258
// Ensure fields are valid.
259259
validateFields(context, type);
260+
261+
// Ensure Interfaces include at least 1 Object type.
262+
validateInterfaces(context, type);
260263
} else if (isUnionType(type)) {
261264
// Ensure Unions include valid member types.
262265
validateUnionMembers(context, type);
@@ -364,6 +367,21 @@ function validateObjectInterfaces(
364367
});
365368
}
366369

370+
function validateInterfaces(
371+
context: SchemaValidationContext,
372+
iface: GraphQLInterfaceType,
373+
): void {
374+
const possibleTypes = context.schema.getPossibleTypes(iface);
375+
376+
if (possibleTypes.length === 0) {
377+
context.reportError(
378+
`Interface ${iface.name} must be implemented ` +
379+
`by at least one Object type.`,
380+
iface.astNode,
381+
);
382+
}
383+
}
384+
367385
function validateObjectImplementsInterface(
368386
context: SchemaValidationContext,
369387
object: GraphQLObjectType,

0 commit comments

Comments
 (0)