Skip to content

Commit bbe6a65

Browse files
authored
Merge pull request #4157 from graphql-java/update-java-dataloader
Update to DataLoader 6.0.0
2 parents 8dc1bee + 7031555 commit bbe6a65

2 files changed

Lines changed: 19 additions & 33 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ jar {
120120
}
121121

122122
dependencies {
123-
api 'com.graphql-java:java-dataloader:5.0.3'
123+
api 'com.graphql-java:java-dataloader:6.0.0'
124124
api 'org.reactivestreams:reactive-streams:' + reactiveStreamsVersion
125125
api "org.jspecify:jspecify:1.0.0"
126126

src/main/java/graphql/schema/DataLoaderWithContext.java

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graphql.schema;
22

3-
import com.google.common.collect.Maps;
43
import graphql.Internal;
5-
import graphql.execution.Async;
64
import graphql.execution.incremental.AlternativeCallContext;
75
import graphql.execution.instrumentation.dataloader.ExhaustedDataLoaderDispatchStrategy;
86
import graphql.execution.instrumentation.dataloader.PerLevelDataLoaderDispatchStrategy;
@@ -12,14 +10,10 @@
1210
import org.jspecify.annotations.NullMarked;
1311
import org.jspecify.annotations.Nullable;
1412

15-
import java.util.ArrayList;
16-
import java.util.HashMap;
1713
import java.util.List;
1814
import java.util.Map;
1915
import java.util.concurrent.CompletableFuture;
2016

21-
import static graphql.Assert.assertNotNull;
22-
2317
@Internal
2418
@NullMarked
2519
public class DataLoaderWithContext<K, V> extends DelegatingDataLoader<K, V> {
@@ -32,48 +26,40 @@ 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;
4244
}
4345

46+
@Override
47+
public CompletableFuture<List<V>> loadMany(List<K> keys) {
48+
CompletableFuture<List<V>> result = super.loadMany(keys);
49+
newDataLoaderInvocation();
50+
return result;
51+
}
4452

4553
@Override
4654
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);
55+
CompletableFuture<List<V>> result = super.loadMany(keys, keyContexts);
6156
newDataLoaderInvocation();
6257
return result;
6358
}
6459

6560
@Override
6661
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);
62+
CompletableFuture<Map<K, V>> result = super.loadMany(keysAndContexts);
7763
newDataLoaderInvocation();
7864
return result;
7965
}

0 commit comments

Comments
 (0)