@@ -412,41 +412,44 @@ public CompletableFuture<ExecutionResult> executeAsync(UnaryOperator<ExecutionIn
412412 * @return a promise to an {@link ExecutionResult} which can include errors
413413 */
414414 public CompletableFuture <ExecutionResult > executeAsync (ExecutionInput executionInput ) {
415- ExecutionInput executionInputWithId = ensureInputHasId (executionInput );
416-
417- CompletableFuture <InstrumentationState > instrumentationStateCF = instrumentation .createStateAsync (new InstrumentationCreateStateParameters (this .graphQLSchema , executionInputWithId ));
418- return Async .orNullCompletedFuture (instrumentationStateCF ).thenCompose (instrumentationState -> {
419- try {
420- InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters (executionInputWithId , this .graphQLSchema );
421- ExecutionInput instrumentedExecutionInput = instrumentation .instrumentExecutionInput (executionInputWithId , inputInstrumentationParameters , instrumentationState );
422-
423- InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters (instrumentedExecutionInput , this .graphQLSchema );
424- InstrumentationContext <ExecutionResult > executionInstrumentation = nonNullCtx (instrumentation .beginExecution (instrumentationParameters , instrumentationState ));
425- executionInstrumentation .onDispatched ();
426-
427- GraphQLSchema graphQLSchema = instrumentation .instrumentSchema (this .graphQLSchema , instrumentationParameters , instrumentationState );
428-
429- CompletableFuture <ExecutionResult > executionResult = parseValidateAndExecute (instrumentedExecutionInput , graphQLSchema , instrumentationState );
430- //
431- // finish up instrumentation
432- executionResult = executionResult .whenComplete (completeInstrumentationCtxCF (executionInstrumentation ));
433- //
434- // allow instrumentation to tweak the result
435- executionResult = executionResult .thenCompose (result -> instrumentation .instrumentExecutionResult (result , instrumentationParameters , instrumentationState ));
436- return executionResult ;
437- } catch (AbortExecutionException abortException ) {
438- return handleAbortException (executionInput , instrumentationState , abortException );
439- }
415+ EngineRunningState engineRunningState = new EngineRunningState (executionInput );
416+ return engineRunningState .call (() -> {
417+ ExecutionInput executionInputWithId = ensureInputHasId (executionInput );
418+ engineRunningState .updateExecutionId (executionInputWithId .getExecutionId ());
419+
420+ CompletableFuture <InstrumentationState > instrumentationStateCF = instrumentation .createStateAsync (new InstrumentationCreateStateParameters (this .graphQLSchema , executionInputWithId ));
421+ instrumentationStateCF = Async .orNullCompletedFuture (instrumentationStateCF );
422+
423+ return engineRunningState .compose (instrumentationStateCF , (instrumentationState -> {
424+ try {
425+ InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters (executionInputWithId , this .graphQLSchema );
426+ ExecutionInput instrumentedExecutionInput = instrumentation .instrumentExecutionInput (executionInputWithId , inputInstrumentationParameters , instrumentationState );
427+
428+ InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters (instrumentedExecutionInput , this .graphQLSchema );
429+ InstrumentationContext <ExecutionResult > executionInstrumentation = nonNullCtx (instrumentation .beginExecution (instrumentationParameters , instrumentationState ));
430+ executionInstrumentation .onDispatched ();
431+
432+ GraphQLSchema graphQLSchema = instrumentation .instrumentSchema (this .graphQLSchema , instrumentationParameters , instrumentationState );
433+
434+ CompletableFuture <ExecutionResult > executionResult = parseValidateAndExecute (instrumentedExecutionInput , graphQLSchema , instrumentationState , engineRunningState );
435+ //
436+ // finish up instrumentation
437+ executionResult = executionResult .whenComplete (completeInstrumentationCtxCF (executionInstrumentation ));
438+ //
439+ // allow instrumentation to tweak the result
440+ executionResult = engineRunningState .compose (executionResult , (result -> instrumentation .instrumentExecutionResult (result , instrumentationParameters , instrumentationState )));
441+ return executionResult ;
442+ } catch (AbortExecutionException abortException ) {
443+ return handleAbortException (executionInput , instrumentationState , abortException );
444+ }
445+ }));
440446 });
441447 }
442448
449+
443450 private CompletableFuture <ExecutionResult > handleAbortException (ExecutionInput executionInput , InstrumentationState instrumentationState , AbortExecutionException abortException ) {
444- CompletableFuture <ExecutionResult > executionResult = CompletableFuture .completedFuture (abortException .toExecutionResult ());
445451 InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters (executionInput , this .graphQLSchema );
446- //
447- // allow instrumentation to tweak the result
448- executionResult = executionResult .thenCompose (result -> instrumentation .instrumentExecutionResult (result , instrumentationParameters , instrumentationState ));
449- return executionResult ;
452+ return instrumentation .instrumentExecutionResult (abortException .toExecutionResult (), instrumentationParameters , instrumentationState );
450453 }
451454
452455 private ExecutionInput ensureInputHasId (ExecutionInput executionInput ) {
@@ -460,24 +463,24 @@ private ExecutionInput ensureInputHasId(ExecutionInput executionInput) {
460463 }
461464
462465
463- private CompletableFuture <ExecutionResult > parseValidateAndExecute (ExecutionInput executionInput , GraphQLSchema graphQLSchema , InstrumentationState instrumentationState ) {
466+ private CompletableFuture <ExecutionResult > parseValidateAndExecute (ExecutionInput executionInput , GraphQLSchema graphQLSchema , InstrumentationState instrumentationState , EngineRunningState engineRunningState ) {
464467 AtomicReference <ExecutionInput > executionInputRef = new AtomicReference <>(executionInput );
465468 Function <ExecutionInput , PreparsedDocumentEntry > computeFunction = transformedInput -> {
466469 // if they change the original query in the pre-parser, then we want to see it downstream from then on
467470 executionInputRef .set (transformedInput );
468471 return parseAndValidate (executionInputRef , graphQLSchema , instrumentationState );
469472 };
470473 CompletableFuture <PreparsedDocumentEntry > preparsedDoc = preparsedDocumentProvider .getDocumentAsync (executionInput , computeFunction );
471- return preparsedDoc . thenCompose (preparsedDocumentEntry -> {
474+ return engineRunningState . compose ( preparsedDoc , (preparsedDocumentEntry -> {
472475 if (preparsedDocumentEntry .hasErrors ()) {
473476 return CompletableFuture .completedFuture (new ExecutionResultImpl (preparsedDocumentEntry .getErrors ()));
474477 }
475478 try {
476- return execute (executionInputRef .get (), preparsedDocumentEntry .getDocument (), graphQLSchema , instrumentationState );
479+ return execute (executionInputRef .get (), preparsedDocumentEntry .getDocument (), graphQLSchema , instrumentationState , engineRunningState );
477480 } catch (AbortExecutionException e ) {
478481 return CompletableFuture .completedFuture (e .toExecutionResult ());
479482 }
480- });
483+ })) ;
481484 }
482485
483486 private PreparsedDocumentEntry parseAndValidate (AtomicReference <ExecutionInput > executionInputRef , GraphQLSchema graphQLSchema , InstrumentationState instrumentationState ) {
@@ -536,13 +539,14 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
536539 private CompletableFuture <ExecutionResult > execute (ExecutionInput executionInput ,
537540 Document document ,
538541 GraphQLSchema graphQLSchema ,
539- InstrumentationState instrumentationState
542+ InstrumentationState instrumentationState ,
543+ EngineRunningState engineRunningState
540544 ) {
541545
542546 Execution execution = new Execution (queryStrategy , mutationStrategy , subscriptionStrategy , instrumentation , valueUnboxer , doNotAutomaticallyDispatchDataLoader );
543547 ExecutionId executionId = executionInput .getExecutionId ();
544548
545- return execution .execute (document , graphQLSchema , executionId , executionInput , instrumentationState );
549+ return execution .execute (document , graphQLSchema , executionId , executionInput , instrumentationState , engineRunningState );
546550 }
547551
548552}
0 commit comments