|
21 | 21 | import org.slf4j.Logger; |
22 | 22 | import org.slf4j.LoggerFactory; |
23 | 23 |
|
24 | | -import java.util.Collections; |
25 | 24 | import java.util.List; |
26 | 25 | import java.util.Map; |
27 | 26 |
|
@@ -280,50 +279,106 @@ public ExecutionResult execute(String query, String operationName, Object contex |
280 | 279 | * @return result including errors |
281 | 280 | */ |
282 | 281 | public ExecutionResult execute(ExecutionInput executionInput) { |
283 | | - String query = executionInput.getQuery(); |
284 | | - String operationName = executionInput.getOperationName(); |
285 | | - Object context = executionInput.getContext(); |
286 | | - Object root = executionInput.getRoot(); |
287 | | - Map<String, Object> variables = executionInput.getVariables() != null ? executionInput.getVariables() : Collections.emptyMap(); |
| 282 | + log.debug("Executing request. operation name: {}. query: {} ", executionInput.getOperationName(), executionInput.getQuery()); |
288 | 283 |
|
289 | | - log.debug("Executing request. operation name: {}. query: {} ", operationName, query); |
| 284 | + InstrumentationContext<ExecutionResult> executionInstrumentation = instrumentation.beginExecution(new InstrumentationExecutionParameters(executionInput)); |
| 285 | + final ExecutionResult executionResult = parseValidateAndExecute(executionInput); |
| 286 | + executionInstrumentation.onEnd(executionResult); |
| 287 | + |
| 288 | + return executionResult; |
| 289 | + } |
| 290 | + |
| 291 | + private ExecutionResult parseValidateAndExecute(ExecutionInput executionInput) { |
| 292 | + ParseResult parseResult = parse(executionInput); |
| 293 | + if (parseResult.isFailure()) { |
| 294 | + return toParseFailureExecutionResult(parseResult.getException()); |
| 295 | + } |
| 296 | + final Document document = parseResult.getDocument(); |
| 297 | + |
| 298 | + final List<ValidationError> errors = validate(executionInput, document); |
| 299 | + if (!errors.isEmpty()) { |
| 300 | + return new ExecutionResultImpl(errors); |
| 301 | + } |
290 | 302 |
|
291 | | - InstrumentationContext<ExecutionResult> executionCtx = instrumentation.beginExecution(new InstrumentationExecutionParameters(query, operationName, context, variables)); |
| 303 | + return execute(executionInput, document); |
| 304 | + } |
292 | 305 |
|
| 306 | + private ParseResult parse(ExecutionInput executionInput) { |
| 307 | + InstrumentationContext<Document> parseInstrumentation = instrumentation.beginParse(new InstrumentationExecutionParameters(executionInput)); |
293 | 308 |
|
294 | | - InstrumentationContext<Document> parseCtx = instrumentation.beginParse(new InstrumentationExecutionParameters(query, operationName, context, variables)); |
295 | 309 | Parser parser = new Parser(); |
296 | 310 | Document document; |
297 | 311 | try { |
298 | | - document = parser.parseDocument(query); |
299 | | - parseCtx.onEnd(document); |
| 312 | + document = parser.parseDocument(executionInput.getQuery()); |
300 | 313 | } catch (ParseCancellationException e) { |
301 | | - RecognitionException recognitionException = (RecognitionException) e.getCause(); |
302 | | - SourceLocation sourceLocation = null; |
303 | | - if (recognitionException != null) { |
304 | | - sourceLocation = new SourceLocation(recognitionException.getOffendingToken().getLine(), recognitionException.getOffendingToken().getCharPositionInLine()); |
305 | | - } |
306 | | - InvalidSyntaxError invalidSyntaxError = new InvalidSyntaxError(sourceLocation); |
307 | | - return new ExecutionResultImpl(Collections.singletonList(invalidSyntaxError)); |
| 314 | + parseInstrumentation.onEnd(e); |
| 315 | + return ParseResult.ofError((RecognitionException) e.getCause()); |
308 | 316 | } |
309 | 317 |
|
310 | | - InstrumentationContext<List<ValidationError>> validationCtx = instrumentation.beginValidation(new InstrumentationValidationParameters(query, operationName, context, variables, document)); |
| 318 | + parseInstrumentation.onEnd(document); |
| 319 | + return ParseResult.of(document); |
| 320 | + } |
| 321 | + |
| 322 | + private List<ValidationError> validate(ExecutionInput executionInput, Document document) { |
| 323 | + InstrumentationContext<List<ValidationError>> validationCtx = instrumentation.beginValidation(new InstrumentationValidationParameters(executionInput, document)); |
311 | 324 |
|
312 | 325 | Validator validator = new Validator(); |
313 | 326 | List<ValidationError> validationErrors = validator.validateDocument(graphQLSchema, document); |
314 | 327 |
|
315 | 328 | validationCtx.onEnd(validationErrors); |
| 329 | + return validationErrors; |
| 330 | + } |
316 | 331 |
|
317 | | - if (validationErrors.size() > 0) { |
318 | | - return new ExecutionResultImpl(validationErrors); |
319 | | - } |
320 | | - ExecutionId executionId = idProvider.provide(query, operationName, context); |
| 332 | + private ExecutionResult execute(ExecutionInput executionInput, Document document) { |
| 333 | + String query = executionInput.getQuery(); |
| 334 | + String operationName = executionInput.getOperationName(); |
| 335 | + Object context = executionInput.getContext(); |
321 | 336 |
|
322 | 337 | Execution execution = new Execution(queryStrategy, mutationStrategy, subscriptionStrategy, instrumentation); |
323 | | - ExecutionResult result = execution.execute(executionId, graphQLSchema, context, root, document, operationName, variables); |
| 338 | + ExecutionId executionId = idProvider.provide(query, operationName, context); |
| 339 | + return execution.execute(document, graphQLSchema, executionId, executionInput); |
| 340 | + } |
| 341 | + |
| 342 | + private ExecutionResult toParseFailureExecutionResult(RecognitionException exception) { |
| 343 | + InvalidSyntaxError invalidSyntaxError = toInvalidSyntaxError(exception); |
| 344 | + return new ExecutionResultImpl(invalidSyntaxError); |
| 345 | + } |
| 346 | + |
| 347 | + private InvalidSyntaxError toInvalidSyntaxError(RecognitionException recognitionException) { |
| 348 | + SourceLocation sourceLocation = null; |
| 349 | + if (recognitionException != null) { |
| 350 | + sourceLocation = new SourceLocation(recognitionException.getOffendingToken().getLine(), recognitionException.getOffendingToken().getCharPositionInLine()); |
| 351 | + } |
| 352 | + return new InvalidSyntaxError(sourceLocation); |
| 353 | + } |
| 354 | + |
| 355 | + private static class ParseResult { |
| 356 | + private final Document document; |
| 357 | + private final RecognitionException exception; |
| 358 | + |
| 359 | + private ParseResult(Document document, RecognitionException exception) { |
| 360 | + this.document = document; |
| 361 | + this.exception = exception; |
| 362 | + } |
324 | 363 |
|
325 | | - executionCtx.onEnd(result); |
| 364 | + private boolean isFailure() { |
| 365 | + return document == null; |
| 366 | + } |
326 | 367 |
|
327 | | - return result; |
| 368 | + private Document getDocument() { |
| 369 | + return document; |
| 370 | + } |
| 371 | + |
| 372 | + private RecognitionException getException() { |
| 373 | + return exception; |
| 374 | + } |
| 375 | + |
| 376 | + private static ParseResult of(Document document) { |
| 377 | + return new ParseResult(document, null); |
| 378 | + } |
| 379 | + |
| 380 | + private static ParseResult ofError(RecognitionException e) { |
| 381 | + return new ParseResult(null, e); |
| 382 | + } |
328 | 383 | } |
329 | 384 | } |
0 commit comments