Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
0a27e76
introducing of a special CompletableFuture enabling scheduling of nes…
andimarek Mar 30, 2025
f655893
replace countdown latch with an async solution
andimarek Mar 30, 2025
3675d8a
wip
andimarek Mar 30, 2025
c2d0676
refactor
andimarek Mar 31, 2025
36b2d64
refactor
andimarek Mar 31, 2025
eb8f9bb
naming
andimarek Mar 31, 2025
d1df820
comment
andimarek Mar 31, 2025
2c5da50
fix test
andimarek Mar 31, 2025
62325eb
green tests without any chain
andimarek Apr 1, 2025
89f2c48
cleanup
andimarek Apr 1, 2025
67df3a9
use ResultPath to identify a field instead DFE
andimarek Apr 1, 2025
dc8ac9a
fix a bug in the PerLevel dispatcher regarding which level to dispatch
andimarek Apr 1, 2025
3be8443
fix test
andimarek Apr 1, 2025
0686385
don't put ExecutionStrategy into context
andimarek Apr 1, 2025
bcfeff8
introduce toInternal method in DFE
andimarek Apr 1, 2025
bf0b730
Merge branch 'master' into dataloader-cfs
andimarek Apr 2, 2025
89b8c13
use new DataLoaderDelegate
andimarek Apr 2, 2025
6d02760
Merge branch 'master' into dataloader-cfs
andimarek Apr 4, 2025
5e00964
update dataloader
andimarek Apr 4, 2025
ce4d3f6
make batch window configurable and make one test more stable
andimarek Apr 4, 2025
401d09d
scheduled executor can be configured for delayed dispatching
andimarek Apr 4, 2025
2a29686
new handling of Dataloders can be disabled
andimarek Apr 4, 2025
e08ca66
Merge branch 'master' into dataloader-cfs
andimarek Apr 7, 2025
42d1d1d
more stuff
andimarek Apr 7, 2025
24d4687
Merge branch 'master' into dataloader-cfs
andimarek Apr 7, 2025
51efbfc
fix mutations with DataLoader
andimarek Apr 7, 2025
6e0da52
testing and cleanup
andimarek Apr 7, 2025
12fe329
green tests
andimarek Apr 8, 2025
e3539c7
disable new chaining algo by default
andimarek Apr 8, 2025
2369db5
cleanup
andimarek Apr 8, 2025
861a317
cleanup
andimarek Apr 8, 2025
1f4d18e
some performance,naming,cleanup, docs
andimarek Apr 9, 2025
efa8fb0
fix an engine state edge case
andimarek Apr 10, 2025
f70630d
fix an engine state edge case
andimarek Apr 10, 2025
e759137
change list implementation
andimarek Apr 10, 2025
c20ed90
Merge branch 'master' into dataloader-cfs
andimarek Apr 11, 2025
bcfb9ef
fix dispatching edge case
andimarek Apr 11, 2025
572fbdf
data loader performance test with chaining on
andimarek Apr 11, 2025
6c3d14c
data loader performance test with chaining on
andimarek Apr 11, 2025
ee1541d
make result path to String faster
andimarek Apr 14, 2025
0d76d31
Merge branch 'master' into dataloader-cfs
andimarek Apr 15, 2025
47300cb
fix merge problem
andimarek Apr 15, 2025
fed7456
Merge branch 'master' into dataloader-cfs
andimarek May 1, 2025
d57d4fd
naming
andimarek May 1, 2025
34bc3d7
add helper methods to configure new dispatching strategy
andimarek May 1, 2025
604f679
implement toInternal by default
andimarek May 1, 2025
6328062
formatting
andimarek May 1, 2025
65cc42a
cleanup
andimarek May 1, 2025
51f9344
cleanup
andimarek May 2, 2025
d1384e1
cleanup
andimarek May 2, 2025
5b04714
cleanup
andimarek May 2, 2025
a622d0d
PR feedback
andimarek May 2, 2025
cdb335f
PR feedback
andimarek May 2, 2025
81c4b09
PR feedback
andimarek May 2, 2025
4e0c26a
Merge branch 'master' into dataloader-cfs
andimarek May 16, 2025
c9a3ff3
merging
andimarek May 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/jmh/java/performance/DataLoaderPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
Expand Down Expand Up @@ -184,6 +185,7 @@ public void executeRequestWithDataLoaders(MyState myState, Blackhole blackhole)
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry().register(ownerDLName, ownerDL).register(petDLName, petDL).build();

ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(myState.query).dataLoaderRegistry(registry).build();
executionInput.getGraphQLContext().put(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, true);
ExecutionResult execute = myState.graphQL.execute(executionInput);
Assert.assertTrue(execute.isDataPresent());
Assert.assertTrue(execute.getErrors().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import graphql.Internal;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;
import java.util.function.Supplier;

@Internal
public interface DataLoaderDispatchStrategy {
Expand Down Expand Up @@ -44,7 +46,8 @@ default void executeObjectOnFieldValuesException(Throwable t, ExecutionStrategyP
default void fieldFetched(ExecutionContext executionContext,
ExecutionStrategyParameters executionStrategyParameters,
DataFetcher<?> dataFetcher,
Object fetchedValue) {
Object fetchedValue,
Supplier<DataFetchingEnvironment> dataFetchingEnvironment) {

}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class Execution {
private final ResponseMapFactory responseMapFactory;
private final boolean doNotAutomaticallyDispatchDataLoader;


public Execution(ExecutionStrategy queryStrategy,
ExecutionStrategy mutationStrategy,
ExecutionStrategy subscriptionStrategy,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
}

MergedField field = parameters.getField();
String pathString = parameters.getPath().toString();
GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType();

// if the DF (like PropertyDataFetcher) does not use the arguments or execution step info then dont build any
Expand Down Expand Up @@ -450,7 +451,7 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
dataFetcher = instrumentation.instrumentDataFetcher(dataFetcher, instrumentationFieldFetchParams, executionContext.getInstrumentationState());
dataFetcher = executionContext.getDataLoaderDispatcherStrategy().modifyDataFetcher(dataFetcher);
Object fetchedObject = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, dataFetcher);
executionContext.getDataLoaderDispatcherStrategy().fieldFetched(executionContext, parameters, dataFetcher, fetchedObject);
executionContext.getDataLoaderDispatcherStrategy().fieldFetched(executionContext, parameters, dataFetcher, fetchedObject, dataFetchingEnvironment);
fetchCtx.onDispatched();
fetchCtx.onFetchedValue(fetchedObject);
// if it's a subscription, leave any reactive objects alone
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/graphql/execution/ResultPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,36 @@ public static ResultPath rootPath() {

// hash is effective immutable but lazily initialized similar to the hash code of java.lang.String
private int hash;
private final String toStringValue;

private ResultPath() {
parent = null;
segment = null;
this.toStringValue = initString();
}

private ResultPath(ResultPath parent, String segment) {
this.parent = assertNotNull(parent, () -> "Must provide a parent path");
this.segment = assertNotNull(segment, () -> "Must provide a sub path");
this.toStringValue = initString();
}

private ResultPath(ResultPath parent, int segment) {
this.parent = assertNotNull(parent, () -> "Must provide a parent path");
this.segment = segment;
this.toStringValue = initString();
}

private String initString() {
if (parent == null) {
return "";
}

if (ROOT_PATH.equals(parent)) {
return segmentToString();
}
return parent + segmentToString();

}

public int getLevel() {
Expand Down Expand Up @@ -294,15 +310,7 @@ public List<String> getKeysOnly() {
*/
@Override
public String toString() {
if (parent == null) {
return "";
}

if (ROOT_PATH.equals(parent)) {
return segmentToString();
}

return parent.toString() + segmentToString();
return toStringValue;
}

public String segmentToString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package graphql.execution.instrumentation.dataloader;


import graphql.ExperimentalApi;
import graphql.GraphQLContext;
import org.jspecify.annotations.NullMarked;

import java.time.Duration;

/**
* GraphQLContext keys related to DataLoader dispatching.
*/
@ExperimentalApi
@NullMarked
public final class DataLoaderDispatchingContextKeys {
private DataLoaderDispatchingContextKeys() {
}

/**
* In nano seconds, the batch window size for delayed DataLoaders.
* That is for DataLoaders, that are not batched as part of the normal per level
* dispatching, because they were created after the level was already dispatched.
* <p>
* Expect Long values
* <p>
* Default is 500_000 (0.5 ms)
*/
public static final String DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS = "__GJ_delayed_data_loader_batch_window_size_nano_seconds";

/**
* An instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
* <p>
* Default is one static executor thread pool with a single thread.
*/
public static final String DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY = "__GJ_delayed_data_loader_dispatching_executor_factory";


/**
* Enables the ability to chain DataLoader dispatching.
* <p>
* Because this requires that all DataLoaders are accessed via DataFetchingEnvironment.getLoader()
* this is not completely backwards compatible and therefore disabled by default.
* <p>
* Expects a boolean value.
*/
public static final String ENABLE_DATA_LOADER_CHAINING = "__GJ_enable_data_loader_chaining";


/**
* Enables the ability that chained DataLoaders are dispatched automatically.
*
* @param graphQLContext
*/
public static void setEnableDataLoaderChaining(GraphQLContext graphQLContext, boolean enabled) {
graphQLContext.put(ENABLE_DATA_LOADER_CHAINING, enabled);
}


/**
* Sets nanoseconds the batch window duration size for delayed DataLoaders.
* That is for DataLoaders, that are not batched as part of the normal per level
* dispatching, because they were created after the level was already dispatched.
*
* @param graphQLContext
* @param batchWindowSize
*/
public static void setDelayedDataLoaderBatchWindowSize(GraphQLContext graphQLContext, Duration batchWindowSize) {
graphQLContext.put(DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS, batchWindowSize.toNanos());
}

/**
* Sets the instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
* <p>
*
* @param graphQLContext
* @param delayedDataLoaderDispatcherExecutorFactory
*/
public static void setDelayedDataLoaderDispatchingExecutorFactory(GraphQLContext graphQLContext, DelayedDataLoaderDispatcherExecutorFactory delayedDataLoaderDispatcherExecutorFactory) {
graphQLContext.put(DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY, delayedDataLoaderDispatcherExecutorFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package graphql.execution.instrumentation.dataloader;

import graphql.ExperimentalApi;
import graphql.GraphQLContext;
import graphql.execution.ExecutionId;
import org.jspecify.annotations.NullMarked;

import java.util.concurrent.ScheduledExecutorService;

/**
* See {@link DataLoaderDispatchingContextKeys} for how to set it.
*/
@ExperimentalApi
@NullMarked
@FunctionalInterface
public interface DelayedDataLoaderDispatcherExecutorFactory {

/**
* Called once per execution to create the {@link ScheduledExecutorService} for the delayed DataLoader dispatching.
*
* Will only called if needed, i.e. if there are delayed DataLoaders.
*
* @param executionId
* @param graphQLContext
*
* @return
*/
ScheduledExecutorService createExecutor(ExecutionId executionId, GraphQLContext graphQLContext);
}
Loading
Loading