Skip to content

Commit 2e5c616

Browse files
committed
cleanup
1 parent 0407946 commit 2e5c616

File tree

7 files changed

+71
-24
lines changed

7 files changed

+71
-24
lines changed

src/jmh/java/performance/DataLoaderPerformance.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,23 @@ public Pet(String id, String name, String ownerId, List<String> friendsIds) {
483483
static BatchLoader<String, Owner> ownerBatchLoader = list -> {
484484
List<Owner> collect = list.stream().map(key -> {
485485
Owner owner = owners.get(key);
486+
try {
487+
Thread.sleep(50);
488+
} catch (InterruptedException e) {
489+
throw new RuntimeException(e);
490+
}
486491
return owner;
487492
}).collect(Collectors.toList());
488493
return CompletableFuture.completedFuture(collect);
489494
};
490495
static BatchLoader<String, Pet> petBatchLoader = list -> {
491496
List<Pet> collect = list.stream().map(key -> {
492497
Pet owner = pets.get(key);
498+
try {
499+
Thread.sleep(5);
500+
} catch (InterruptedException e) {
501+
throw new RuntimeException(e);
502+
}
493503
return owner;
494504
}).collect(Collectors.toList());
495505
return CompletableFuture.completedFuture(collect);
@@ -532,20 +542,20 @@ public void setup() {
532542
});
533543
DataFetcher petsDf = (env -> {
534544
Owner owner = env.getSource();
535-
return env.getDataLoader(petDLName).loadMany((List) owner.petIds)
536-
.thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
545+
return env.getDataLoader(petDLName).loadMany((List) owner.petIds);
546+
// .thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
537547
});
538548

539549
DataFetcher petFriendsDF = (env -> {
540550
Pet pet = env.getSource();
541-
return env.getDataLoader(petDLName).loadMany((List) pet.friendsIds)
542-
.thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
551+
return env.getDataLoader(petDLName).loadMany((List) pet.friendsIds);
552+
// .thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
543553
});
544554

545555
DataFetcher petOwnerDF = (env -> {
546556
Pet pet = env.getSource();
547-
return env.getDataLoader(ownerDLName).load(pet.ownerId)
548-
.thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
557+
return env.getDataLoader(ownerDLName).load(pet.ownerId);
558+
// .thenCompose((result) -> CompletableFuture.supplyAsync(() -> null).thenApply((__) -> result));
549559
});
550560

551561

src/main/java/graphql/GraphQLUnusualConfiguration.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static graphql.Assert.assertNotNull;
99
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING;
10+
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING;
1011

1112
/**
1213
* This allows you to control "unusual" aspects of the GraphQL system
@@ -344,12 +345,16 @@ private DataloaderConfig(GraphQLContextConfiguration contextConfig) {
344345
}
345346

346347
/**
347-
* @return true if @defer and @stream behaviour is enabled for this execution.
348+
* returns true if chained data loader dispatching is enabled
348349
*/
349350
public boolean isDataLoaderChainingEnabled() {
350351
return contextConfig.getBoolean(ENABLE_DATA_LOADER_CHAINING);
351352
}
352353

354+
public boolean isDataLoaderExhaustedDispatchingEnabled() {
355+
return contextConfig.get(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING);
356+
}
357+
353358
/**
354359
* Enables the ability that chained DataLoaders are dispatched automatically.
355360
*/
@@ -359,6 +364,17 @@ public DataloaderConfig enableDataLoaderChaining(boolean enable) {
359364
return this;
360365
}
361366

367+
/**
368+
* Enables a dispatching strategy that will dispatch as long as there is no
369+
* other data fetcher or batch loader running.
370+
*/
371+
@ExperimentalApi
372+
public DataloaderConfig enableDataLoaderExhaustedDispatching(boolean enable) {
373+
contextConfig.put(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, enable);
374+
return this;
375+
}
376+
377+
362378
}
363379

364380
public static class ResponseMapFactoryConfig extends BaseContextConfig {

src/main/java/graphql/execution/Execution.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import graphql.execution.instrumentation.Instrumentation;
1616
import graphql.execution.instrumentation.InstrumentationContext;
1717
import graphql.execution.instrumentation.InstrumentationState;
18+
import graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys;
1819
import graphql.execution.instrumentation.dataloader.ExhaustedDataLoaderDispatchStrategy;
20+
import graphql.execution.instrumentation.dataloader.PerLevelDataLoaderDispatchStrategy;
1921
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
2022
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
2123
import graphql.extensions.ExtensionsBuilder;
@@ -262,8 +264,10 @@ private DataLoaderDispatchStrategy createDataLoaderDispatchStrategy(ExecutionCon
262264
if (executionContext.getDataLoaderRegistry() == EMPTY_DATALOADER_REGISTRY || doNotAutomaticallyDispatchDataLoader) {
263265
return DataLoaderDispatchStrategy.NO_OP;
264266
}
265-
// return new PerLevelDataLoaderDispatchStrategy(executionContext);
266-
return new ExhaustedDataLoaderDispatchStrategy(executionContext);
267+
if (executionContext.getGraphQLContext().getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, false)) {
268+
return new ExhaustedDataLoaderDispatchStrategy(executionContext);
269+
}
270+
return new PerLevelDataLoaderDispatchStrategy(executionContext);
267271
}
268272

269273

@@ -274,7 +278,8 @@ private void addExtensionsBuilderNotPresent(GraphQLContext graphQLContext) {
274278
}
275279
}
276280

277-
private ExecutionResult mergeExtensionsBuilderIfPresent(ExecutionResult executionResult, GraphQLContext graphQLContext) {
281+
private ExecutionResult mergeExtensionsBuilderIfPresent(ExecutionResult executionResult, GraphQLContext
282+
graphQLContext) {
278283
Object builder = graphQLContext.get(ExtensionsBuilder.class);
279284
if (builder instanceof ExtensionsBuilder) {
280285
ExtensionsBuilder extensionsBuilder = (ExtensionsBuilder) builder;

src/main/java/graphql/execution/instrumentation/dataloader/DataLoaderDispatchingContextKeys.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ private DataLoaderDispatchingContextKeys() {
2525
public static final String ENABLE_DATA_LOADER_CHAINING = "__GJ_enable_data_loader_chaining";
2626

2727

28+
/**
29+
* Enabled a different dispatching strategy that mimics the JS event loop based one:
30+
* DataLoader will be dispatched as soon as there is no data fetcher or batch loader currently running.
31+
*
32+
*/
33+
public static final String ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING = "__GJ_enable_data_loader_exhausted_dispatching";
34+
2835
/**
2936
* Enables the ability that chained DataLoaders are dispatched automatically.
3037
*
@@ -34,5 +41,14 @@ public static void setEnableDataLoaderChaining(GraphQLContext graphQLContext, bo
3441
graphQLContext.put(ENABLE_DATA_LOADER_CHAINING, enabled);
3542
}
3643

44+
/**
45+
* Enables the ability that chained DataLoaders are dispatched automatically.
46+
*
47+
* @param graphQLContext
48+
*/
49+
public static void setEnableDataLoaderExhaustedDispatching(GraphQLContext graphQLContext, boolean enabled) {
50+
graphQLContext.put(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, enabled);
51+
}
52+
3753

3854
}

src/main/java/graphql/execution/instrumentation/dataloader/ExhaustedDataLoaderDispatchStrategy.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,7 @@ private void dispatchImpl(CallStack callStack) {
265265
}
266266

267267

268-
public void newDataLoaderInvocation(String resultPath,
269-
int level,
270-
DataLoader dataLoader,
271-
String dataLoaderName,
272-
Object key,
273-
@Nullable AlternativeCallContext alternativeCallContext) {
274-
// DataLoaderInvocation dataLoaderInvocation = new DataLoaderInvocation(resultPath, level, dataLoader, dataLoaderName, key);
268+
public void newDataLoaderInvocation(@Nullable AlternativeCallContext alternativeCallContext) {
275269
CallStack callStack = getCallStack(alternativeCallContext);
276270
newDataLoaderInvocationMaybeDispatch(callStack);
277271
}

src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import graphql.execution.MergedField;
1616
import graphql.execution.directives.QueryDirectives;
1717
import graphql.execution.incremental.AlternativeCallContext;
18+
import graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys;
1819
import graphql.language.Document;
1920
import graphql.language.Field;
2021
import graphql.language.FragmentDefinition;
@@ -232,9 +233,10 @@ public ExecutionStepInfo getExecutionStepInfo() {
232233
if (dataLoader == null) {
233234
return null;
234235
}
235-
// if (!graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false)) {
236-
// return dataLoader;
237-
// }
236+
if (!graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false)
237+
&& !graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, false)) {
238+
return dataLoader;
239+
}
238240
return new DataLoaderWithContext<>(this, dataLoaderName, dataLoader);
239241
}
240242

@@ -272,8 +274,8 @@ public Object toInternal() {
272274
@Override
273275
public String toString() {
274276
return "DataFetchingEnvironmentImpl{" +
275-
"executionStepInfo=" + executionStepInfo +
276-
'}';
277+
"executionStepInfo=" + executionStepInfo +
278+
'}';
277279
}
278280

279281
@NullUnmarked

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphql.Internal;
44
import graphql.execution.incremental.AlternativeCallContext;
55
import graphql.execution.instrumentation.dataloader.ExhaustedDataLoaderDispatchStrategy;
6+
import graphql.execution.instrumentation.dataloader.PerLevelDataLoaderDispatchStrategy;
67
import org.dataloader.DataLoader;
78
import org.dataloader.DelegatingDataLoader;
89
import org.jspecify.annotations.NonNull;
@@ -32,11 +33,14 @@ public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
3233
DataFetchingEnvironmentImpl dfeImpl = (DataFetchingEnvironmentImpl) dfe;
3334
DataFetchingEnvironmentImpl.DFEInternalState dfeInternalState = (DataFetchingEnvironmentImpl.DFEInternalState) dfeImpl.toInternal();
3435
dfeInternalState.getProfiler().dataLoaderUsed(dataLoaderName);
35-
if (dfeInternalState.getDataLoaderDispatchStrategy() instanceof ExhaustedDataLoaderDispatchStrategy) {
36+
if (dfeInternalState.getDataLoaderDispatchStrategy() instanceof PerLevelDataLoaderDispatchStrategy) {
3637
AlternativeCallContext alternativeCallContext = dfeInternalState.getDeferredCallContext();
3738
int level = dfe.getExecutionStepInfo().getPath().getLevel();
3839
String path = dfe.getExecutionStepInfo().getPath().toString();
39-
((ExhaustedDataLoaderDispatchStrategy) dfeInternalState.dataLoaderDispatchStrategy).newDataLoaderInvocation(path, level, delegate, dataLoaderName, key, alternativeCallContext);
40+
((PerLevelDataLoaderDispatchStrategy) dfeInternalState.dataLoaderDispatchStrategy).newDataLoaderInvocation(path, level, delegate, dataLoaderName, key, alternativeCallContext);
41+
} else if (dfeInternalState.getDataLoaderDispatchStrategy() instanceof ExhaustedDataLoaderDispatchStrategy) {
42+
AlternativeCallContext alternativeCallContext = dfeInternalState.getDeferredCallContext();
43+
((ExhaustedDataLoaderDispatchStrategy) dfeInternalState.dataLoaderDispatchStrategy).newDataLoaderInvocation(alternativeCallContext);
4044
}
4145
return result;
4246
}

0 commit comments

Comments
 (0)