Skip to content

Commit

Permalink
Validate schema root types and directives
Browse files Browse the repository at this point in the history
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
  • Loading branch information
leebyron committed Dec 7, 2017
1 parent 96f92f3 commit 5f6690e
Show file tree
Hide file tree
Showing 10 changed files with 950 additions and 720 deletions.
15 changes: 11 additions & 4 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,33 @@ export function getOperationRootType(
): GraphQLObjectType {
switch (operation.operation) {
case 'query':
return schema.getQueryType();
const queryType = schema.getQueryType();
if (!queryType) {
throw new GraphQLError(
'Schema does not define the required query root type.',
[operation],
);
}
return queryType;
case 'mutation':
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError('Schema is not configured for mutations', [
throw new GraphQLError('Schema is not configured for mutations.', [
operation,
]);
}
return mutationType;
case 'subscription':
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError('Schema is not configured for subscriptions', [
throw new GraphQLError('Schema is not configured for subscriptions.', [
operation,
]);
}
return subscriptionType;
default:
throw new GraphQLError(
'Can only execute queries, mutations and subscriptions',
'Can only execute queries, mutations and subscriptions.',
[operation],
);
}
Expand Down
Loading

0 comments on commit 5f6690e

Please sign in to comment.