22
33
44import graphql .ExecutionResult ;
5+ import graphql .ExecutionResultImpl ;
56import graphql .GraphQLException ;
7+ import graphql .MutationNotSupportedError ;
68import graphql .execution .instrumentation .Instrumentation ;
79import graphql .execution .instrumentation .InstrumentationContext ;
810import graphql .execution .instrumentation .parameters .DataFetchParameters ;
1315import graphql .schema .GraphQLSchema ;
1416
1517import java .util .ArrayList ;
18+ import java .util .Collections ;
1619import java .util .LinkedHashMap ;
1720import java .util .List ;
1821import java .util .Map ;
1922
23+ import static graphql .language .OperationDefinition .Operation .MUTATION ;
24+ import static graphql .language .OperationDefinition .Operation .QUERY ;
25+
2026public class Execution {
2127
2228 private final FieldCollector fieldCollector = new FieldCollector ();
@@ -38,15 +44,15 @@ public ExecutionResult execute(ExecutionId executionId, GraphQLSchema graphQLSch
3844 return executeOperation (executionContext , root , executionContext .getOperationDefinition ());
3945 }
4046
41- private GraphQLObjectType getOperationRootType (GraphQLSchema graphQLSchema , OperationDefinition operationDefinition ) {
42- if (operationDefinition . getOperation () == OperationDefinition . Operation . MUTATION ) {
47+ private GraphQLObjectType getOperationRootType (GraphQLSchema graphQLSchema , OperationDefinition . Operation operation ) {
48+ if (operation == MUTATION ) {
4349 return graphQLSchema .getMutationType ();
4450
45- } else if (operationDefinition . getOperation () == OperationDefinition . Operation . QUERY ) {
51+ } else if (operation == QUERY ) {
4652 return graphQLSchema .getQueryType ();
4753
4854 } else {
49- throw new GraphQLException ();
55+ throw new GraphQLException ("Unhandled case. An extra operation enum has been added without code support" );
5056 }
5157 }
5258
@@ -57,13 +63,23 @@ private ExecutionResult executeOperation(
5763
5864 InstrumentationContext <ExecutionResult > dataFetchCtx = instrumentation .beginDataFetch (new DataFetchParameters (executionContext ));
5965
60- GraphQLObjectType operationRootType = getOperationRootType (executionContext .getGraphQLSchema (), operationDefinition );
66+ OperationDefinition .Operation operation = operationDefinition .getOperation ();
67+ GraphQLObjectType operationRootType = getOperationRootType (executionContext .getGraphQLSchema (), operation );
68+
69+ //
70+ // do we have a mutation operation root type. The spec says if we don't then mutations are not allowed to be executed
71+ //
72+ // for the record earlier code has asserted that we have a query type in the schema since the spec says this is
73+ // ALWAYS required
74+ if (operation == MUTATION && operationRootType == null ) {
75+ return new ExecutionResultImpl (Collections .singletonList (new MutationNotSupportedError ()));
76+ }
6177
6278 Map <String , List <Field >> fields = new LinkedHashMap <String , List <Field >>();
6379 fieldCollector .collectFields (executionContext , operationRootType , operationDefinition .getSelectionSet (), new ArrayList <String >(), fields );
6480
6581 ExecutionResult result ;
66- if (operationDefinition . getOperation () == OperationDefinition . Operation . MUTATION ) {
82+ if (operation == MUTATION ) {
6783 result = mutationStrategy .execute (executionContext , operationRootType , root , fields );
6884 } else {
6985 result = queryStrategy .execute (executionContext , operationRootType , root , fields );
0 commit comments