11package graphql .schema ;
22
3- import com .google .common .collect .Maps ;
43import graphql .Internal ;
5- import graphql .execution .Async ;
64import graphql .execution .incremental .AlternativeCallContext ;
75import graphql .execution .instrumentation .dataloader .ExhaustedDataLoaderDispatchStrategy ;
86import graphql .execution .instrumentation .dataloader .PerLevelDataLoaderDispatchStrategy ;
1210import org .jspecify .annotations .NullMarked ;
1311import org .jspecify .annotations .Nullable ;
1412
15- import java .util .ArrayList ;
16- import java .util .HashMap ;
1713import java .util .List ;
1814import java .util .Map ;
1915import java .util .concurrent .CompletableFuture ;
2016
21- import static graphql .Assert .assertNotNull ;
22-
2317@ Internal
2418@ NullMarked
2519public class DataLoaderWithContext <K , V > extends DelegatingDataLoader <K , V > {
@@ -32,10 +26,18 @@ public DataLoaderWithContext(DataFetchingEnvironment dfe, String dataLoaderName,
3226 this .dfe = dfe ;
3327 }
3428
29+ // general note: calling super.load() is important, because otherwise the data loader will sometimes called
30+ // later than the dispatch, which results in a hanging DL
31+
32+ @ Override
33+ public CompletableFuture <V > load (K key ) {
34+ CompletableFuture <V > result = super .load (key );
35+ newDataLoaderInvocation ();
36+ return result ;
37+ }
38+
3539 @ Override
3640 public CompletableFuture <V > load (@ NonNull K key , @ Nullable Object keyContext ) {
37- // calling super.load() is important, because otherwise the data loader will sometimes called
38- // later than the dispatch, which results in a hanging DL
3941 CompletableFuture <V > result = super .load (key , keyContext );
4042 newDataLoaderInvocation ();
4143 return result ;
@@ -44,41 +46,20 @@ public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
4446
4547 @ Override
4648 public CompletableFuture <List <V >> loadMany (List <K > keys , List <Object > keyContexts ) {
47- assertNotNull (keys );
48- assertNotNull (keyContexts );
49-
50- CompletableFuture <List <V >> result ;
51- List <CompletableFuture <V >> collect = new ArrayList <>(keys .size ());
52- for (int i = 0 ; i < keys .size (); i ++) {
53- K key = keys .get (i );
54- Object keyContext = null ;
55- if (i < keyContexts .size ()) {
56- keyContext = keyContexts .get (i );
57- }
58- collect .add (delegate .load (key , keyContext ));
59- }
60- result = Async .allOf (collect );
49+ CompletableFuture <List <V >> result = super .loadMany (keys , keyContexts );
6150 newDataLoaderInvocation ();
6251 return result ;
6352 }
6453
6554 @ Override
6655 public CompletableFuture <Map <K , V >> loadMany (Map <K , ?> keysAndContexts ) {
67- assertNotNull (keysAndContexts );
68-
69- CompletableFuture <Map <K , V >> result ;
70- Map <K , CompletableFuture <V >> collect = Maps .newHashMapWithExpectedSize (keysAndContexts .size ());
71- for (Map .Entry <K , ?> entry : keysAndContexts .entrySet ()) {
72- K key = entry .getKey ();
73- Object keyContext = entry .getValue ();
74- collect .put (key , delegate .load (key , keyContext ));
75- }
76- result = Async .allOf (collect );
56+ CompletableFuture <Map <K , V >> result = super .loadMany (keysAndContexts );
7757 newDataLoaderInvocation ();
7858 return result ;
7959 }
8060
8161 private void newDataLoaderInvocation () {
62+ System .out .println ("newDataLoaderInvocation" );
8263 DataFetchingEnvironmentImpl dfeImpl = (DataFetchingEnvironmentImpl ) dfe ;
8364 DataFetchingEnvironmentImpl .DFEInternalState dfeInternalState = (DataFetchingEnvironmentImpl .DFEInternalState ) dfeImpl .toInternal ();
8465 if (dfeInternalState .getDataLoaderDispatchStrategy () instanceof PerLevelDataLoaderDispatchStrategy ) {
0 commit comments