Skip to content
Open
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
123 changes: 123 additions & 0 deletions src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,34 @@ describe('Type System: A Schema must have Object root types', () => {
expectJSON(validateSchema(schemaWithDef)).toDeepEqual([]);
});

it('accepts a Schema whose query type implements interfaces', () => {
const schema = buildSchema(`
interface SomeInterface {
test: String
}

type Query implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([]);

const schemaWithDef = buildSchema(`
schema {
query: QueryRoot
}

interface SomeInterface {
test: String
}

type QueryRoot implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schemaWithDef)).toDeepEqual([]);
});

it('rejects a Schema without a query type', () => {
const schema = buildSchema(`
type Mutation {
Expand Down Expand Up @@ -296,6 +324,53 @@ describe('Type System: A Schema must have Object root types', () => {
]);
});

it('rejects a Schema whose mutation type implements interfaces', () => {
const schema = buildSchema(`
type Query {
field: String
}

interface SomeInterface {
test: String
}

type Mutation implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message: 'Root type "Mutation" cannot implement interfaces.',
locations: [{ line: 10, column: 7 }],
},
]);

const schemaWithDef = buildSchema(`
schema {
query: Query
mutation: MutationRoot
}

type Query {
field: String
}

interface SomeInterface {
test: String
}

type MutationRoot implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schemaWithDef)).toDeepEqual([
{
message: 'Root type "MutationRoot" cannot implement interfaces.',
locations: [{ line: 15, column: 7 }],
},
]);
});

it('rejects a Schema whose subscription type is an input type', () => {
const schema = buildSchema(`
type Query {
Expand Down Expand Up @@ -337,6 +412,54 @@ describe('Type System: A Schema must have Object root types', () => {
]);
});

it('rejects a Schema whose subscription type implements interfaces', () => {
const schema = buildSchema(`
type Query {
field: String
}

interface SomeInterface {
test: String
}

type Subscription implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message: 'Root type "Subscription" cannot implement interfaces.',
locations: [{ line: 10, column: 7 }],
},
]);

const schemaWithDef = buildSchema(`
schema {
query: Query
subscription: SubscriptionRoot
}

type Query {
field: String
}

interface SomeInterface {
test: String
}


type SubscriptionRoot implements SomeInterface {
test: String
}
`);
expectJSON(validateSchema(schemaWithDef)).toDeepEqual([
{
message: 'Root type "SubscriptionRoot" cannot implement interfaces.',
locations: [{ line: 16, column: 7 }],
},
]);
});

it('rejects a Schema extended with invalid root types', () => {
let schema = buildSchema(`
input SomeInputObject {
Expand Down
9 changes: 9 additions & 0 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ function validateRootTypes(context: SchemaValidationContext): void {
getOperationTypeNode(schema, operationType) ??
(rootType as any).astNode,
);
} else if (
operationType !== 'query' &&
rootType.getInterfaces().length > 0
) {
const rootTypeStr = inspect(rootType);
context.reportError(
`Root type "${rootTypeStr}" cannot implement interfaces.`,
rootType.astNode,
);
} else {
rootTypesMap.add(rootType, operationType);
}
Expand Down
Loading