Skip to content

Commit 0678722

Browse files
committed
added two new context settings that do not add dispatching instrumentation
1 parent 8f5dad2 commit 0678722

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ Here's an example of a GraphQL provider that implements three interfaces at the
233233

234234
* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java)
235235

236-
## Request-scoped DataLoaders
236+
## Context and DataLoader settings
237237

238-
It is possible to use dataloaders in both a request scope and a per query scope by customizing [GraphQLContextBuilder](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/GraphQLContextBuilder.java) and selecting the appropriate [ContextSetting](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/ContextSetting.java) with the provided [GraphQLConfiguration](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/config/GraphQLConfiguration.java).
238+
It is possible to create context, and consequently dataloaders, in both a request scope and a per query scope by customizing [GraphQLContextBuilder](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/GraphQLContextBuilder.java) and selecting the appropriate [ContextSetting](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/ContextSetting.java) with the provided [GraphQLConfiguration](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/config/GraphQLConfiguration.java).
239239
A new [DataLoaderRegistry](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoaderRegistry.java) should be created in each call to the GraphQLContextBuilder, and the servlet will call the builder at the appropriate times.
240240
For eg:
241241
```java
@@ -281,4 +281,6 @@ public class CustomGraphQLContextBuilder implements GraphQLContextBuilder {
281281
}
282282

283283
```
284-
If per request is selected, this will cause all queries within the http request, if using a batch, to share dataloader caches and batch together load calls as efficently as possible. The dataloaders are dispatched using instrumentation and the correct instrumentation will be selected according to the ContextSetting. The default context setting in GraphQLConfiguration is per query.
284+
If per request is selected this will cause all queries within the http request, if using a batch, to share dataloader caches and batch together load calls as efficently as possible. The dataloaders are dispatched using instrumentation and the correct instrumentation will be selected according to the ContextSetting. The default context setting in GraphQLConfiguration is per query.
285+
286+
Two additional context settings are provided, one for each of the previous settings but without the addition of the Dataloader dispatching instrumentation. This is useful for those not using Dataloaders or wanting to supply their own dispatching instrumentation though the instrumentation supplier within the GraphQLQueryInvoker.

src/main/java/graphql/servlet/config/GraphQLConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static class Builder {
111111
private boolean asyncServletModeEnabled = false;
112112
private Executor asyncExecutor = Executors.newCachedThreadPool(new GraphQLThreadFactory());
113113
private long subscriptionTimeout = 0;
114-
private ContextSetting contextSetting = ContextSetting.PER_QUERY;
114+
private ContextSetting contextSetting = ContextSetting.PER_QUERY_WITH_INSTRUMENTATION;
115115
private Supplier<BatchInputPreProcessor> batchInputPreProcessorSupplier = () -> new NoOpBatchInputPreProcessor();
116116

117117
private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder) {

src/main/java/graphql/servlet/context/ContextSetting.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ public enum ContextSetting {
2929
/**
3030
* A context object, and therefor dataloader registry and subject, should be shared between all GraphQL executions in a http request.
3131
*/
32-
PER_REQUEST,
32+
PER_REQUEST_WITH_INSTRUMENTATION,
33+
PER_REQUEST_WITHOUT_INSTRUMENTATION,
3334
/**
3435
* Each GraphQL execution should always have its own context.
3536
*/
36-
PER_QUERY;
37+
PER_QUERY_WITH_INSTRUMENTATION,
38+
PER_QUERY_WITHOUT_INSTRUMENTATION;
3739

3840
/**
3941
* Creates a set of inputs with the correct context based on the setting.
@@ -45,9 +47,13 @@ public enum ContextSetting {
4547
*/
4648
public GraphQLBatchedInvocationInput getBatch(List<GraphQLRequest> requests, GraphQLSchema schema, Supplier<GraphQLContext> contextSupplier, Object root) {
4749
switch (this) {
48-
case PER_QUERY:
50+
case PER_QUERY_WITH_INSTRUMENTATION:
51+
//Intentional fallthrough
52+
case PER_QUERY_WITHOUT_INSTRUMENTATION:
4953
return new PerQueryBatchedInvocationInput(requests, schema, contextSupplier, root);
50-
case PER_REQUEST:
54+
case PER_REQUEST_WITHOUT_INSTRUMENTATION:
55+
//Intentional fallthrough
56+
case PER_REQUEST_WITH_INSTRUMENTATION:
5157
return new PerRequestBatchedInvocationInput(requests, schema, contextSupplier.get(), root);
5258
default:
5359
throw new RuntimeException("Unconfigured context setting type");
@@ -65,18 +71,22 @@ public Supplier<Instrumentation> configureInstrumentationForContext(Supplier<Ins
6571
DataLoaderDispatcherInstrumentationOptions options) {
6672
ConfigurableDispatchInstrumentation dispatchInstrumentation;
6773
switch (this) {
68-
case PER_REQUEST:
74+
case PER_REQUEST_WITH_INSTRUMENTATION:
6975
DataLoaderRegistry registry = executionInputs.stream().findFirst().map(ExecutionInput::getDataLoaderRegistry)
7076
.orElseThrow(IllegalArgumentException::new);
7177
List<ExecutionId> executionIds = executionInputs.stream().map(ExecutionInput::getExecutionId).collect(Collectors.toList());
7278
RequestLevelTrackingApproach requestTrackingApproach = new RequestLevelTrackingApproach(executionIds, registry);
7379
dispatchInstrumentation = new ConfigurableDispatchInstrumentation(options,
7480
(dataLoaderRegistry -> requestTrackingApproach));
7581
break;
76-
case PER_QUERY:
82+
case PER_QUERY_WITH_INSTRUMENTATION:
7783
dispatchInstrumentation = new ConfigurableDispatchInstrumentation(options,
7884
(dataLoaderRegistry) -> new FieldLevelTrackingApproach(dataLoaderRegistry));
7985
break;
86+
case PER_REQUEST_WITHOUT_INSTRUMENTATION:
87+
//Intentional fallthrough
88+
case PER_QUERY_WITHOUT_INSTRUMENTATION:
89+
return () -> instrumentation.get();
8090
default:
8191
throw new RuntimeException("Unconfigured context setting type");
8292
}

src/test/groovy/graphql/servlet/DataLoaderDispatchingSpec.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package graphql.servlet
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4-
import graphql.execution.reactive.SingleSubscriberPublisher
54
import graphql.schema.DataFetcher
65
import graphql.schema.DataFetchingEnvironment
76
import graphql.servlet.context.DefaultGraphQLContext
@@ -24,7 +23,6 @@ import javax.websocket.server.HandshakeRequest
2423
import java.util.concurrent.CompletableFuture
2524
import java.util.concurrent.CompletionStage
2625
import java.util.concurrent.atomic.AtomicInteger
27-
import java.util.concurrent.atomic.AtomicReference
2826

2927
class DataLoaderDispatchingSpec extends Specification {
3028

@@ -115,7 +113,7 @@ class DataLoaderDispatchingSpec extends Specification {
115113

116114
def "batched query with per query context does not batch loads together"() {
117115
setup:
118-
configureServlet(ContextSetting.PER_QUERY)
116+
configureServlet(ContextSetting.PER_QUERY_WITH_INSTRUMENTATION)
119117
request.addParameter('query', '[{ "query": "query { query(arg:\\"test\\") { echo(arg:\\"test\\") { echo(arg:\\"test\\") } }}" }, { "query": "query{query(arg:\\"test\\") { echo (arg:\\"test\\") { echo(arg:\\"test\\")} }}" },' +
120118
' { "query": "query{queryTwo(arg:\\"test\\") { echo (arg:\\"test\\")}}" }, { "query": "query{queryTwo(arg:\\"test\\") { echo (arg:\\"test\\")}}" }]')
121119
resetCounters()
@@ -140,7 +138,7 @@ class DataLoaderDispatchingSpec extends Specification {
140138

141139
def "batched query with per request context batches all queries within the request"() {
142140
setup:
143-
servlet = configureServlet(ContextSetting.PER_REQUEST)
141+
servlet = configureServlet(ContextSetting.PER_REQUEST_WITH_INSTRUMENTATION)
144142
request.addParameter('query', '[{ "query": "query { query(arg:\\"test\\") { echo(arg:\\"test\\") { echo(arg:\\"test\\") } }}" }, { "query": "query{query(arg:\\"test\\") { echo (arg:\\"test\\") { echo(arg:\\"test\\")} }}" },' +
145143
' { "query": "query{queryTwo(arg:\\"test\\") { echo (arg:\\"test\\")}}" }, { "query": "query{queryTwo(arg:\\"test\\") { echo (arg:\\"test\\")}}" }]')
146144
resetCounters()

0 commit comments

Comments
 (0)