Skip to content

Commit 819a59c

Browse files
authored
Validate schema root types and directives (#1124)
This moves validation out of GraphQLSchema's constructor (but not yet from other type constructors), which is responsible for root type validation and interface implementation checking. Reduces time to construct GraphQLSchema significantly, shifting the time to validation. This also allows for much looser rules within the schema builders, which implicitly validate while trying to adhere to flow types. Instead we use any casts to loosen the rules to defer that to validation where errors can be richer. This also loosens the rule that a schema can only be constructed if it has a query type, moving that to validation as well. That makes flow typing slightly less nice, but allows for incremental schema building which is valuable
1 parent 5fec485 commit 819a59c

File tree

10 files changed

+956
-720
lines changed

10 files changed

+956
-720
lines changed

src/execution/execute.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,26 +393,33 @@ export function getOperationRootType(
393393
): GraphQLObjectType {
394394
switch (operation.operation) {
395395
case 'query':
396-
return schema.getQueryType();
396+
const queryType = schema.getQueryType();
397+
if (!queryType) {
398+
throw new GraphQLError(
399+
'Schema does not define the required query root type.',
400+
[operation],
401+
);
402+
}
403+
return queryType;
397404
case 'mutation':
398405
const mutationType = schema.getMutationType();
399406
if (!mutationType) {
400-
throw new GraphQLError('Schema is not configured for mutations', [
407+
throw new GraphQLError('Schema is not configured for mutations.', [
401408
operation,
402409
]);
403410
}
404411
return mutationType;
405412
case 'subscription':
406413
const subscriptionType = schema.getSubscriptionType();
407414
if (!subscriptionType) {
408-
throw new GraphQLError('Schema is not configured for subscriptions', [
415+
throw new GraphQLError('Schema is not configured for subscriptions.', [
409416
operation,
410417
]);
411418
}
412419
return subscriptionType;
413420
default:
414421
throw new GraphQLError(
415-
'Can only execute queries, mutations and subscriptions',
422+
'Can only execute queries, mutations and subscriptions.',
416423
[operation],
417424
);
418425
}

0 commit comments

Comments
 (0)