Skip to content

Commit 433237b

Browse files
committed
Correctly report error for invalid root type in extended schema
1 parent 7d160b7 commit 433237b

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

src/type/__tests__/validation-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,59 @@ describe('Type System: A Schema must have Object root types', () => {
323323
]);
324324
});
325325

326+
it('rejects a schema extended with invalid root types', () => {
327+
let schema = buildSchema(`
328+
input SomeInputObject {
329+
test: String
330+
}
331+
`);
332+
333+
schema = extendSchema(
334+
schema,
335+
parse(`
336+
extend schema {
337+
query: SomeInputObject
338+
}
339+
`),
340+
);
341+
342+
schema = extendSchema(
343+
schema,
344+
parse(`
345+
extend schema {
346+
mutation: SomeInputObject
347+
}
348+
`),
349+
);
350+
351+
schema = extendSchema(
352+
schema,
353+
parse(`
354+
extend schema {
355+
subscription: SomeInputObject
356+
}
357+
`),
358+
);
359+
360+
expect(validateSchema(schema)).to.deep.equal([
361+
{
362+
message:
363+
'Query root type must be Object type, it cannot be SomeInputObject.',
364+
locations: [{ line: 3, column: 18 }],
365+
},
366+
{
367+
message:
368+
'Mutation root type must be Object type if provided, it cannot be SomeInputObject.',
369+
locations: [{ line: 3, column: 21 }],
370+
},
371+
{
372+
message:
373+
'Subscription root type must be Object type if provided, it cannot be SomeInputObject.',
374+
locations: [{ line: 3, column: 25 }],
375+
},
376+
]);
377+
});
378+
326379
it('rejects a Schema whose directives are incorrectly typed', () => {
327380
const schema = new GraphQLSchema({
328381
query: SomeObjectType,

src/type/validate.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ function getOperationTypeNode(
152152
type: GraphQLObjectType,
153153
operation: string,
154154
): ?ASTNode {
155-
const astNode = schema.astNode;
156-
const operationTypeNode =
157-
astNode &&
158-
astNode.operationTypes.find(
159-
operationType => operationType.operation === operation,
160-
);
161-
return operationTypeNode ? operationTypeNode.type : type && type.astNode;
155+
for (const node of getAllNodes(schema)) {
156+
if (node.operationTypes) {
157+
for (const operationType of node.operationTypes) {
158+
if (operationType.operation === operation) {
159+
return operationType.type;
160+
}
161+
}
162+
}
163+
}
164+
165+
return type.astNode;
162166
}
163167

164168
function validateDirectives(context: SchemaValidationContext): void {

0 commit comments

Comments
 (0)