|
13 | 13 | import graphql.ExecutionResultImpl; |
14 | 14 | import graphql.GraphQLError; |
15 | 15 | import graphql.Internal; |
16 | | -import graphql.OperationNotSupportedError; |
17 | 16 | import graphql.execution.instrumentation.Instrumentation; |
18 | 17 | import graphql.execution.instrumentation.InstrumentationContext; |
19 | 18 | import graphql.execution.instrumentation.InstrumentationState; |
@@ -106,24 +105,17 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe |
106 | 105 | InstrumentationContext<ExecutionResult> dataFetchCtx = instrumentation.beginDataFetch(dataFetchParameters); |
107 | 106 |
|
108 | 107 | OperationDefinition.Operation operation = operationDefinition.getOperation(); |
109 | | - GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operation); |
110 | | - |
111 | | - // |
112 | | - // do we have a mutation operation root type. The spec says if we don't then mutations are not allowed to be executed |
113 | | - // |
114 | | - // for the record earlier code has asserted that we have a query type in the schema since the spec says this is |
115 | | - // ALWAYS required |
116 | | - if (operation == MUTATION && operationRootType == null) { |
117 | | - OperationNotSupportedError mutationNotSupportedError = new OperationNotSupportedError("Schema is not configured for mutations.", operationDefinition.getSourceLocation()); |
118 | | - CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture( |
119 | | - new ExecutionResultImpl(Collections.singletonList(mutationNotSupportedError))); |
120 | | - executionDispatchCtx.onEnd(resultCompletableFuture, null); |
121 | | - return resultCompletableFuture; |
122 | | - } else if (operation == SUBSCRIPTION && operationRootType == null) { |
123 | | - OperationNotSupportedError subscriptionNotSupportedError = new OperationNotSupportedError("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation()); |
124 | | - CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(new ExecutionResultImpl(Collections.singletonList(subscriptionNotSupportedError))); |
125 | | - executionDispatchCtx.onEnd(resultCompletableFuture, null); |
126 | | - return resultCompletableFuture; |
| 108 | + GraphQLObjectType operationRootType; |
| 109 | + |
| 110 | + try { |
| 111 | + operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition); |
| 112 | + } catch (RuntimeException rte) { |
| 113 | + if (rte instanceof GraphQLError) { |
| 114 | + CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte))); |
| 115 | + executionDispatchCtx.onEnd(resultCompletableFuture, null); |
| 116 | + return resultCompletableFuture; |
| 117 | + } |
| 118 | + throw rte; |
127 | 119 | } |
128 | 120 |
|
129 | 121 | FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters() |
@@ -178,13 +170,26 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe |
178 | 170 | return result; |
179 | 171 | } |
180 | 172 |
|
181 | | - private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) { |
| 173 | + private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) { |
| 174 | + OperationDefinition.Operation operation = operationDefinition.getOperation(); |
182 | 175 | if (operation == MUTATION) { |
183 | | - return graphQLSchema.getMutationType(); |
| 176 | + GraphQLObjectType mutationType = graphQLSchema.getMutationType(); |
| 177 | + if (mutationType == null) { |
| 178 | + throw new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation()); |
| 179 | + } |
| 180 | + return mutationType; |
184 | 181 | } else if (operation == QUERY) { |
185 | | - return graphQLSchema.getQueryType(); |
| 182 | + GraphQLObjectType queryType = graphQLSchema.getQueryType(); |
| 183 | + if (queryType == null) { |
| 184 | + throw new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation()); |
| 185 | + } |
| 186 | + return queryType; |
186 | 187 | } else if (operation == SUBSCRIPTION) { |
187 | | - return graphQLSchema.getSubscriptionType(); |
| 188 | + GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType(); |
| 189 | + if (subscriptionType == null) { |
| 190 | + throw new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation()); |
| 191 | + } |
| 192 | + return subscriptionType; |
188 | 193 | } else { |
189 | 194 | return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support"); |
190 | 195 | } |
|
0 commit comments