Skip to content

Commit 334393e

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

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/type/__tests__/validation-test.js

+46
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,52 @@ 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(schema, parse(`
334+
extend schema {
335+
query: SomeInputObject
336+
}
337+
`));
338+
339+
schema = extendSchema(schema, parse(`
340+
extend schema {
341+
mutation: SomeInputObject
342+
}
343+
`));
344+
345+
schema = extendSchema(schema, parse(`
346+
extend schema {
347+
subscription: SomeInputObject
348+
}
349+
`));
350+
351+
const errorMsg = (operation) =>
352+
`${operation} root type must be Object type, it cannot be SomeInputObject.`;
353+
expect(validateSchema(schema)).to.deep.equal([
354+
{
355+
message:
356+
'Query root type must be Object type, it cannot be SomeInputObject.',
357+
locations: [{ line: 3, column: 16 }],
358+
},
359+
{
360+
message:
361+
'Mutation root type must be Object type if provided, it cannot be SomeInputObject.',
362+
locations: [{ line: 3, column: 19 }],
363+
},
364+
{
365+
message:
366+
'Subscription root type must be Object type if provided, it cannot be SomeInputObject.',
367+
locations: [{ line: 3, column: 23 }],
368+
},
369+
]);
370+
});
371+
326372
it('rejects a Schema whose directives are incorrectly typed', () => {
327373
const schema = new GraphQLSchema({
328374
query: SomeObjectType,

src/type/validate.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ 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+
for (const operationType of node.operationTypes) {
157+
if (operationType.operation === operation) {
158+
return operationType.type;
159+
}
160+
}
161+
}
162+
163+
return type.astNode;
162164
}
163165

164166
function validateDirectives(context: SchemaValidationContext): void {

0 commit comments

Comments
 (0)