11package graphql
22
33import graphql.execution.ExecutionId
4+ import graphql.execution.instrumentation.ExecuteObjectInstrumentationContext
45import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext
56import graphql.execution.instrumentation.Instrumentation
67import graphql.execution.instrumentation.InstrumentationContext
78import graphql.execution.instrumentation.InstrumentationState
9+ import graphql.execution.instrumentation.SimplePerformantInstrumentation
810import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters
911import graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters
1012import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters
@@ -768,10 +770,7 @@ class ExecutionInputTest extends Specification {
768770 }
769771
770772 def " without capturePartialResultsOnCancel a late cancel discards even fully gathered nested data" () {
771- // capture is OFF, so there is no cancellation race; the top-level 'outer' field value info
772- // completes quickly while its nested 'value' is still resolving. Cancelling at that point and
773- // then letting 'value' finish means the field values are fully gathered, but because capture
774- // is disabled the gathered data must be discarded and only the cancel error returned.
773+ given:
775774 def sdl = '''
776775 type Query {
777776 outer : Inner
@@ -780,44 +779,31 @@ class ExecutionInputTest extends Specification {
780779 value : String
781780 }
782781 '''
783-
784- CountDownLatch valueStarted = new CountDownLatch(1)
785- CountDownLatch valueRelease = new CountDownLatch(1)
786-
782+ def valueResult = new CompletableFuture<String>()
787783 DataFetcher outerDf = { DataFetchingEnvironment env -> [:] }
788- DataFetcher valueDf = { DataFetchingEnvironment env ->
789- return CompletableFuture.supplyAsync {
790- valueStarted.countDown()
791- valueRelease.await()
792- return " value"
793- }
794- }
784+ DataFetcher valueDf = { DataFetchingEnvironment env -> valueResult }
785+ def schema = TestUtil.schema(sdl, [" Query " : [" outer" : outerDf], " Inner " : [" value" : valueDf]])
786+ def executionInput = ExecutionInput.newExecutionInput(" { outer { value } }" ).build()
795787
796- def fetcherMap = [" Query " : [" outer" : outerDf], " Inner " : [" value" : valueDf]]
797- def schema = TestUtil.schema(sdl, fetcherMap)
798- def graphQL = GraphQL.newGraphQL(schema).build()
788+ // Cancel after the nested values have been gathered, before the object result is assembled.
789+ def objectInstrumentation = Stub(ExecuteObjectInstrumentationContext) {
790+ onFieldValuesInfo(_) >> { executionInput.cancel() }
791+ }
792+ def instrumentation = Spy(SimplePerformantInstrumentation) {
793+ beginExecuteObject(_, _) >> objectInstrumentation
794+ }
795+ def graphQL = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build()
799796
800797 when:
801- ExecutionInput executionInput = ExecutionInput.newExecutionInput()
802- .query(" { outer { value } }" )
803- .build() // capturePartialResultsOnCancel NOT enabled
804-
805798 def cf = graphQL.executeAsync(executionInput)
806-
807- // the nested value DF has started, so 'outer' field value info is already done
808- valueStarted.await()
809- // cancel while the nested value is still resolving, then let it finish
810- executionInput.cancel()
811- valueRelease.countDown()
812-
799+ valueResult.complete(" value" )
813800 await().atMost(Duration.ofSeconds(10)).until({ -> cf.isDone() })
814801 def er = cf.join()
815802
816803 then:
817804 !cf.isCompletedExceptionally()
818805 !er.errors.isEmpty()
819806 er.errors[0][" message" ] == " Execution has been asked to be cancelled"
820- // capture disabled - even though the data was fully gathered, it is discarded
821807 er.data == null
822808 }
823809
0 commit comments