Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions src/main/java/graphql/GraphQLUnusualConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,26 @@ private DataloaderConfig(GraphQLContextConfiguration contextConfig) {
}

/**
* returns true if chained data loader dispatching is enabled
* Returns true if the per-level strategy's chained DataLoader dispatching is enabled.
* Exhausted dispatching supports chaining independently of this setting.
*/
public boolean isDataLoaderChainingEnabled() {
return contextConfig.getBoolean(ENABLE_DATA_LOADER_CHAINING);
}

/**
* Returns true if exhausted dispatching is enabled. Since version 27, this defaults to true
* unless per-level chained dispatching is explicitly enabled.
*/
public boolean isDataLoaderExhaustedDispatchingEnabled() {
return contextConfig.get(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING);
Boolean enabled = contextConfig.get(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING);
return enabled != null ? enabled : !isDataLoaderChainingEnabled();
}

/**
* Enables the ability that chained DataLoaders are dispatched automatically.
* Enables automatic dispatching of chained DataLoaders using the per-level strategy.
* This selects per-level dispatching when exhausted dispatching is not explicitly configured.
* Explicitly enabling both strategies is invalid.
*/
@ExperimentalApi
public DataloaderConfig enableDataLoaderChaining(boolean enable) {
Expand All @@ -385,6 +393,11 @@ public DataloaderConfig enableDataLoaderChaining(boolean enable) {
/**
* Enables a dispatching strategy that will dispatch as long as there is no
* other data fetcher or batch loader running.
* Since version 27, this is the default unless per-level chained dispatching is explicitly enabled.
* DataLoaders must be obtained through {@link graphql.schema.DataFetchingEnvironment#getDataLoader(String)}
* so that their load calls can be tracked, including chained loads.
* Set this to false to use per-level dispatching. Explicitly enabling both exhausted dispatching
* and per-level chained dispatching is invalid.
*/
@ExperimentalApi
public DataloaderConfig enableDataLoaderExhaustedDispatching(boolean enable) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,10 @@ private DataLoaderDispatchStrategy createDataLoaderDispatchStrategy(ExecutionCon
if (executionContext.getDataLoaderRegistry() == EMPTY_DATALOADER_REGISTRY || doNotAutomaticallyDispatchDataLoader) {
return DataLoaderDispatchStrategy.NO_OP;
}
if (executionContext.getGraphQLContext().getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, false)) {
if (executionContext.getGraphQLContext().getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false)) {
GraphQLContext graphQLContext = executionContext.getGraphQLContext();
boolean chainingEnabled = graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false);
if (graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, !chainingEnabled)) {
if (chainingEnabled) {
throw new GraphQLException("enabling data loader chaining and exhausted dispatching at the same time ambiguous");
}
return new ExhaustedDataLoaderDispatchStrategy(executionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,43 @@ private DataLoaderDispatchingContextKeys() {
}

/**
* Enables the ability to chain DataLoader dispatching.
* Enables chained DataLoader dispatching using the per-level strategy.
* <p>
* Because this requires that all DataLoaders are accessed via DataFetchingEnvironment.getLoader()
* this is not completely backwards compatible and therefore disabled by default.
* This is disabled by default. Enabling it selects per-level dispatching unless exhausted dispatching
* is explicitly configured. Explicitly enabling both strategies is invalid.
* DataLoaders must be accessed via {@link graphql.schema.DataFetchingEnvironment#getDataLoader(String)}.
* The default exhausted strategy supports chaining independently of this setting.
* <p>
* Expects a boolean value.
*/
public static final String ENABLE_DATA_LOADER_CHAINING = "__GJ_enable_data_loader_chaining";


/**
* Enabled a different dispatching strategy that mimics the JS event loop based one:
* Enables a dispatching strategy that mimics the JS event loop based one:
* DataLoader will be dispatched as soon as there is no data fetcher or batch loader currently running.
*
* Since version 27, this defaults to true unless {@link #ENABLE_DATA_LOADER_CHAINING} is true.
* Set this to false to use per-level dispatching. DataLoaders must be accessed via
* {@link graphql.schema.DataFetchingEnvironment#getDataLoader(String)} so their load calls can be tracked.
*/
public static final String ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING = "__GJ_enable_data_loader_exhausted_dispatching";

/**
* Enables the ability that chained DataLoaders are dispatched automatically.
* Enables automatic dispatching of chained DataLoaders using the per-level strategy.
*
* @param graphQLContext
* @param graphQLContext the execution context
* @param enabled whether to enable per-level chained dispatching
*/
public static void setEnableDataLoaderChaining(GraphQLContext graphQLContext, boolean enabled) {
graphQLContext.put(ENABLE_DATA_LOADER_CHAINING, enabled);
}

/**
* Enables the ability that chained DataLoaders are dispatched automatically.
* Configures exhausted dispatching, which is the default since version 27 unless per-level chained
* dispatching is explicitly enabled. Set this to false to use per-level dispatching.
*
* @param graphQLContext
* @param graphQLContext the execution context
* @param enabled whether to use exhausted dispatching
*/
public static void setEnableDataLoaderExhaustedDispatching(GraphQLContext graphQLContext, boolean enabled) {
graphQLContext.put(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, enabled);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/graphql/schema/DataFetchingEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro

/**
* This allows you to retrieve a named dataloader from the underlying {@link org.dataloader.DataLoaderRegistry}
* <p>
* Since version 27, the default exhausted dispatching strategy requires loaders to be obtained through
* this method so that load calls, including chained loads, can be tracked for automatic dispatching.
* Loading directly through the registry or a separately held loader bypasses this tracking and can
* leave the execution waiting for loads that are never dispatched.
*
* @param dataLoaderName the name of the data loader to fetch
* @param <K> the key type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public ExecutionStepInfo getExecutionStepInfo() {
return null;
}
if (!graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false)
&& !graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, false)) {
&& !graphQLContext.getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, true)) {
return dataLoader;
}
return new DataLoaderWithContext<>(this, dataLoaderName, dataLoader);
Expand Down
12 changes: 7 additions & 5 deletions src/test/groovy/graphql/ChainedDataLoaderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import java.util.concurrent.ExecutionException
import java.util.concurrent.atomic.AtomicInteger

import static graphql.ExecutionInput.newExecutionInput
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.setEnableDataLoaderChaining
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.setEnableDataLoaderExhaustedDispatching
import static java.util.concurrent.CompletableFuture.supplyAsync
Expand Down Expand Up @@ -71,19 +73,19 @@ class ChainedDataLoaderTest extends Specification {
def graphQL = GraphQL.newGraphQL(schema).build()

def query = "{ dogName catName } "
def ei = newExecutionInput(query).dataLoaderRegistry(dataLoaderRegistry).build()
chainedDataLoaderOrExhaustedDispatcher ? setEnableDataLoaderChaining(ei.graphQLContext, true) : setEnableDataLoaderExhaustedDispatching(ei.graphQLContext, true)
def ei = newExecutionInput(query).dataLoaderRegistry(dataLoaderRegistry).graphQLContext(dispatchConfiguration).build()

when:
def efCF = graphQL.executeAsync(ei)
Awaitility.await().until { efCF.isDone() }
def er = efCF.get()
then:
er.errors.empty
er.data == [dogName: "Luna", catName: "Tiger"]
batchLoadCalls == 2

where:
chainedDataLoaderOrExhaustedDispatcher << [true, false]
dispatchConfiguration << [[:], [(ENABLE_DATA_LOADER_CHAINING): true], [(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING): true]]
}

@Unroll
Expand Down Expand Up @@ -418,7 +420,7 @@ class ChainedDataLoaderTest extends Specification {

}

def "handling of chained DataLoaders is disabled by default"() {
def "handling of chained DataLoaders can be disabled with per-level dispatching"() {
given:
def sdl = '''

Expand Down Expand Up @@ -467,7 +469,7 @@ class ChainedDataLoaderTest extends Specification {

def query = "{ dogName catName } "
def ei = newExecutionInput(query).dataLoaderRegistry(dataLoaderRegistry).build()

setEnableDataLoaderExhaustedDispatching(ei.graphQLContext, false)

when:
def er = graphQL.executeAsync(ei)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ class GraphQLUnusualConfigurationTest extends Specification {
GraphQL.unusualConfiguration(graphqlContext).dataloaderConfig().isDataLoaderChainingEnabled()
}

def "exhausted dispatch configuration reports the effective default for contexts and builders"() {
given:
def builder = GraphQLContext.newContext()
def config = GraphQL.unusualConfiguration(builder).dataloaderConfig()
if (chaining != null) {
config.enableDataLoaderChaining(chaining)
}
if (exhausted != null) {
config.enableDataLoaderExhaustedDispatching(exhausted)
}

expect:
config.isDataLoaderExhaustedDispatchingEnabled() == expected
GraphQL.unusualConfiguration(builder.build()).dataloaderConfig().isDataLoaderExhaustedDispatchingEnabled() == expected

where:
exhausted | chaining | expected
null | null | true
null | false | true
null | true | false
false | null | false
false | false | false
false | true | false
true | null | true
true | false | true
}

def "we can access via the ExecutionInput"() {
when:
def eiBuilder = ExecutionInput.newExecutionInput("query q {f}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package graphql.execution.instrumentation.dataloader

import graphql.EngineRunningState
import graphql.ExecutionInput
import graphql.ExecutionResultImpl
import graphql.GraphQL
import graphql.MutationSchema
import graphql.Profiler
import graphql.TestUtil
import graphql.execution.DataLoaderDispatchStrategy
import graphql.execution.Execution
import graphql.execution.ExecutionContext
import graphql.execution.ExecutionId
import graphql.execution.ExecutionStrategy
import graphql.execution.ExecutionStrategyParameters
import graphql.execution.ValueUnboxer
import graphql.execution.instrumentation.SimplePerformantInstrumentation
import graphql.parser.Parser
import graphql.schema.DataFetcher
import org.dataloader.BatchLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING
import static java.util.concurrent.CompletableFuture.completedFuture
import static java.util.concurrent.TimeUnit.SECONDS

class DataLoaderDispatchStrategySelectionTest extends Specification {

def "per-level opt-out preserves automatic dispatch of directly accessed loaders"() {
given:
def loader = DataLoaderFactory.newDataLoader({ keys -> completedFuture(keys) } as BatchLoader)
def registry = new DataLoaderRegistry().register("names", loader)
DataFetcher fetcher = { environment -> registry.getDataLoader("names").load("Luna") }
def graphQL = GraphQL.newGraphQL(TestUtil.schema("type Query { name: String }", [Query: [name: fetcher]])).build()
def input = ExecutionInput.newExecutionInput("{ name }").dataLoaderRegistry(registry).build()
GraphQL.unusualConfiguration(input).dataloaderConfig().enableDataLoaderExhaustedDispatching(false)

when:
def result = graphQL.executeAsync(input).get(5, SECONDS)

then:
result.errors.empty
result.data == [name: "Luna"]
}

def "selects the configured dispatch strategy for #operation with exhausted=#exhausted and chaining=#chaining"() {
given:
def input = ExecutionInput.newExecutionInput("$operation { __typename }")
.dataLoaderRegistry(new DataLoaderRegistry())
.build()
if (exhausted != null) {
input.graphQLContext.put(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, exhausted)
}
if (chaining != null) {
input.graphQLContext.put(ENABLE_DATA_LOADER_CHAINING, chaining)
}
def strategy = Mock(ExecutionStrategy)
def execution = new Execution(strategy, strategy, strategy, SimplePerformantInstrumentation.INSTANCE, ValueUnboxer.DEFAULT, false)

when:
execution.execute(Parser.parse(input.query), MutationSchema.schema, ExecutionId.generate(), input, null,
new EngineRunningState(input, Profiler.NO_OP), Profiler.NO_OP).join()

then:
1 * strategy.execute(_, _) >> { ExecutionContext context, ExecutionStrategyParameters parameters ->
assert context.dataLoaderDispatcherStrategy.class == expected
completedFuture(new ExecutionResultImpl([__typename: "Root"], []))
}

where:
[operation, exhausted, chaining, expected] << [
["query", "mutation", "subscription"],
[
[null, null, ExhaustedDataLoaderDispatchStrategy],
[null, false, ExhaustedDataLoaderDispatchStrategy],
[null, true, PerLevelDataLoaderDispatchStrategy],
[false, null, PerLevelDataLoaderDispatchStrategy],
[false, false, PerLevelDataLoaderDispatchStrategy],
[false, true, PerLevelDataLoaderDispatchStrategy],
[true, null, ExhaustedDataLoaderDispatchStrategy],
[true, false, ExhaustedDataLoaderDispatchStrategy],
]
].combinations().collect { [it[0]] + it[1] }
}

def "automatic dispatch stays disabled without a registry or when explicitly disabled"() {
given:
def inputBuilder = ExecutionInput.newExecutionInput("{ __typename }")
.graphQLContext([(ENABLE_DATA_LOADER_CHAINING): true, (ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING): true])
if (withRegistry) {
inputBuilder.dataLoaderRegistry(new DataLoaderRegistry())
}
def input = inputBuilder.build()
def strategy = Mock(ExecutionStrategy)
def execution = new Execution(strategy, strategy, strategy, SimplePerformantInstrumentation.INSTANCE, ValueUnboxer.DEFAULT, disableAutomaticDispatch)

when:
execution.execute(Parser.parse(input.query), MutationSchema.schema, ExecutionId.generate(), input, null,
new EngineRunningState(input, Profiler.NO_OP), Profiler.NO_OP).join()

then:
1 * strategy.execute(_, _) >> { ExecutionContext context, ExecutionStrategyParameters parameters ->
assert context.dataLoaderDispatcherStrategy.is(DataLoaderDispatchStrategy.NO_OP)
completedFuture(new ExecutionResultImpl([__typename: "Root"], []))
}

where:
withRegistry | disableAutomaticDispatch
false | false
false | true
true | true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ class DataLoaderHangingTest extends Specification {
@Override
Object get(DataFetchingEnvironment environment) {
Product source = environment.getSource()
DataLoaderRegistry dlRegistry = environment.getGraphQlContext().get("registry")
DataLoader<Integer, Person> personDL = dlRegistry.getDataLoader("person")
DataLoader<Integer, Person> personDL = environment.getDataLoader("person")
return personDL.load(source.getSuppliedById()).thenApply({ person ->
if (person.id == 0) {
throw new RuntimeException("Failure in suppliedByDF for person with ID == 0.")
Expand All @@ -322,8 +321,7 @@ class DataLoaderHangingTest extends Specification {
@Override
Object get(DataFetchingEnvironment environment) {
Person source = environment.getSource()
DataLoaderRegistry dlRegistry = environment.getGraphQlContext().get("registry")
DataLoader<Integer, Company> companyDL = dlRegistry.getDataLoader("company")
DataLoader<Integer, Company> companyDL = environment.getDataLoader("company")
return companyDL.load(source.getCompanyId())
}
}
Expand Down Expand Up @@ -372,16 +370,18 @@ class DataLoaderHangingTest extends Specification {

ExecutionInput executionInput = newExecutionInput()
.query(query)
.graphQLContext(["registry": registry])
.graphQLContext([(ENABLE_DATA_LOADER_CHAINING): false])
.graphQLContext(dispatchConfiguration)
.dataLoaderRegistry(registry)
.build()

def executionResult = graphQL.execute(executionInput)
def executionResult = graphQL.executeAsync(executionInput).get(10, java.util.concurrent.TimeUnit.SECONDS)


then:

(executionResult.errors.size() > 0)

where:
dispatchConfiguration << [[:], [(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING): false], [(ENABLE_DATA_LOADER_CHAINING): true]]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class DeferWithDataLoaderTest extends Specification {
combined.errors == null
combined.data == expectedExpensiveData

if (contextKey == ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING) {
if (contextKey == ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING || contextKey == null) {
// based on the timing of shops vs expensiveShops DF it could be one or two batch loader calls
batchCompareDataFetchers.departmentsForShopsBatchLoaderCounter.get() == 1 || batchCompareDataFetchers.departmentsForShopsBatchLoaderCounter.get() == 2
} else {
Expand Down
Loading
Loading