@@ -109,6 +109,72 @@ class ProfilerTest extends Specification {
109109
110110 }
111111
112+ def " cached dataloader values" () {
113+ given :
114+ def sdl = '''
115+
116+ type Query {
117+ dog: Dog
118+ }
119+ type Dog {
120+ name: String
121+ }
122+ '''
123+ AtomicInteger batchLoadCalls = new AtomicInteger ()
124+ BatchLoader<String , String > batchLoader = { keys ->
125+ return supplyAsync {
126+ batchLoadCalls. incrementAndGet()
127+ Thread . sleep(250 )
128+ println " BatchLoader called with keys: $keys "
129+ return [" Luna" ]
130+ }
131+ }
132+
133+ DataLoader<String , String > nameDataLoader = DataLoaderFactory . newDataLoader(batchLoader);
134+
135+ DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry ();
136+ dataLoaderRegistry. register(" name" , nameDataLoader);
137+
138+ def dogDF = { env ->
139+ def loader = env. getDataLoader(" name" )
140+ def result = loader. load(" Key1" ). thenCompose {
141+ return loader. load(" Key1" ) // This should hit the cache
142+ }
143+ } as DataFetcher
144+
145+ def nameDF = { env ->
146+ def loader = env. getDataLoader(" name" )
147+ def result = loader. load(" Key1" ). thenCompose {
148+ return loader. load(" Key1" ) // This should hit the cache
149+ }
150+ } as DataFetcher
151+
152+
153+ def fetchers = [Query : [dog : dogDF], Dog : [name : nameDF]]
154+ def schema = TestUtil . schema(sdl, fetchers)
155+ def graphQL = GraphQL . newGraphQL(schema). build()
156+
157+ def query = " { dog {name } } "
158+ def ei = newExecutionInput(query). dataLoaderRegistry(dataLoaderRegistry). profileExecution(true ). build()
159+ setEnableDataLoaderChaining(ei. graphQLContext, true )
160+
161+ when :
162+ def efCF = graphQL. executeAsync(ei)
163+ Awaitility . await(). until { efCF. isDone() }
164+ def er = efCF. get()
165+ def profilerResult = ei. getGraphQLContext(). get(ProfilerResult . PROFILER_CONTEXT_KEY ) as ProfilerResult
166+ then :
167+ er. data == [dog : [name : " Luna" ]]
168+ batchLoadCalls. get() == 1
169+ then :
170+ profilerResult. getDataLoaderLoadInvocations(). get(" name" ) == 4
171+ profilerResult. getDispatchEvents()[0 ]. type == ProfilerResult.DispatchEventType . STRATEGY_DISPATCH
172+ profilerResult. getDispatchEvents()[0 ]. dataLoaderName == " name"
173+ profilerResult. getDispatchEvents()[0 ]. count == 1
174+ profilerResult. getDispatchEvents()[0 ]. level == 1
175+
176+ }
177+
112178
113179 def " collects instrumentation list" () {
114180 given :
0 commit comments