Skip to content

Commit fd2454f

Browse files
kdlanbbakerman
authored andcommitted
make DataLoaderDispatcherInstrumentation support request scope for servlet usage
1 parent 4c0c33a commit fd2454f

3 files changed

Lines changed: 69 additions & 14 deletions

File tree

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.LinkedHashMap;
2929
import java.util.Map;
3030
import java.util.concurrent.CompletableFuture;
31+
import java.util.function.Supplier;
3132

3233
/**
3334
* This graphql {@link graphql.execution.instrumentation.Instrumentation} will dispatch
@@ -44,9 +45,8 @@ public class DataLoaderDispatcherInstrumentation extends SimpleInstrumentation {
4445

4546
private static final Logger log = LoggerFactory.getLogger(DataLoaderDispatcherInstrumentation.class);
4647

47-
private final DataLoaderRegistry dataLoaderRegistry;
4848
private final DataLoaderDispatcherInstrumentationOptions options;
49-
private final FieldLevelTrackingApproach fieldLevelTrackingApproach;
49+
private final Supplier<DataLoaderRegistry> supplier;
5050

5151
/**
5252
* You pass in a registry of N data loaders which will be {@link org.dataloader.DataLoader#dispatch() dispatched} as
@@ -66,15 +66,35 @@ public DataLoaderDispatcherInstrumentation(DataLoaderRegistry dataLoaderRegistry
6666
* @param options the options to control the behaviour
6767
*/
6868
public DataLoaderDispatcherInstrumentation(DataLoaderRegistry dataLoaderRegistry, DataLoaderDispatcherInstrumentationOptions options) {
69-
this.dataLoaderRegistry = dataLoaderRegistry;
69+
this(() -> dataLoaderRegistry, options);
70+
}
71+
72+
/**
73+
* You pass in supplier of a registry of N data loaders which will be {@link org.dataloader.DataLoader#dispatch() dispatched} as
74+
* each level of the query executes.
75+
*
76+
* @param supplier the supplier of registry of data loaders that will be dispatched
77+
*/
78+
public DataLoaderDispatcherInstrumentation(Supplier<DataLoaderRegistry> supplier) {
79+
this(supplier, DataLoaderDispatcherInstrumentationOptions.newOptions());
80+
}
81+
82+
/**
83+
* You pass in a supplier of registry of N data loaders which will be {@link org.dataloader.DataLoader#dispatch() dispatched} as
84+
* each level of the query executes.
85+
*
86+
* @param dataLoaderRegistry the registry of data loaders that will be dispatched
87+
* @param options the options to control the behaviour
88+
*/
89+
public DataLoaderDispatcherInstrumentation(Supplier<DataLoaderRegistry> supplier, DataLoaderDispatcherInstrumentationOptions options) {
90+
this.supplier = supplier;
7091
this.options = options;
71-
this.fieldLevelTrackingApproach = new FieldLevelTrackingApproach(log, dataLoaderRegistry);
7292
}
7393

7494

7595
@Override
7696
public InstrumentationState createState() {
77-
return fieldLevelTrackingApproach.createState();
97+
return new DataLoaderDispatcherInstrumentationState(log, supplier.get());
7898
}
7999

80100

@@ -90,13 +110,13 @@ public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, Instrume
90110
// which allows them to work if used.
91111
return (DataFetcher<Object>) environment -> {
92112
Object obj = dataFetcher.get(environment);
93-
immediatelyDispatch();
113+
immediatelyDispatch(state);
94114
return obj;
95115
};
96116
}
97117

98-
private void immediatelyDispatch() {
99-
fieldLevelTrackingApproach.dispatch();
118+
private void immediatelyDispatch(DataLoaderDispatcherInstrumentationState state) {
119+
state.getApproach().dispatch();
100120
}
101121

102122
@Override
@@ -124,36 +144,41 @@ private boolean isDataLoaderCompatibleExecution(ExecutionContext executionContex
124144

125145
@Override
126146
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
127-
return fieldLevelTrackingApproach.beginExecutionStrategy(parameters);
147+
DataLoaderDispatcherInstrumentationState state = parameters.getInstrumentationState();
148+
return state.getApproach().beginExecutionStrategy(parameters.withNewState(state.getState()));
128149
}
129150

130151
@Override
131152
public DeferredFieldInstrumentationContext beginDeferredField(InstrumentationDeferredFieldParameters parameters) {
132-
return fieldLevelTrackingApproach.beginDeferredField(parameters);
153+
DataLoaderDispatcherInstrumentationState state = parameters.getInstrumentationState();
154+
return state.getApproach().beginDeferredField(parameters.withNewState(state.getState()));
133155
}
134156

135157
@Override
136158
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
137-
return fieldLevelTrackingApproach.beginFieldFetch(parameters);
159+
DataLoaderDispatcherInstrumentationState state = parameters.getInstrumentationState();
160+
return state.getApproach().beginFieldFetch(parameters.withNewState(state.getState()));
138161
}
139162

140163
@Override
141164
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
142165
if (!options.isIncludeStatistics()) {
143166
return CompletableFuture.completedFuture(executionResult);
144167
}
168+
DataLoaderDispatcherInstrumentationState state = parameters.getInstrumentationState();
145169
Map<Object, Object> currentExt = executionResult.getExtensions();
146170
Map<Object, Object> statsMap = new LinkedHashMap<>();
147171
statsMap.putAll(currentExt == null ? Collections.emptyMap() : currentExt);
148-
Map<Object, Object> dataLoaderStats = buildStatsMap();
172+
Map<Object, Object> dataLoaderStats = buildStatsMap(state);
149173
statsMap.put("dataloader", dataLoaderStats);
150174

151175
log.debug("Data loader stats : {}", dataLoaderStats);
152176

153177
return CompletableFuture.completedFuture(new ExecutionResultImpl(executionResult.getData(), executionResult.getErrors(), statsMap));
154178
}
155179

156-
private Map<Object, Object> buildStatsMap() {
180+
private Map<Object, Object> buildStatsMap(DataLoaderDispatcherInstrumentationState state ) {
181+
DataLoaderRegistry dataLoaderRegistry = state.getDataLoaderRegistry();
157182
Statistics allStats = dataLoaderRegistry.getStatistics();
158183
Map<Object, Object> statsMap = new LinkedHashMap<>();
159184
statsMap.put("overall-statistics", allStats.toMap());

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package graphql.execution.instrumentation.dataloader;
22

3+
import org.dataloader.DataLoaderRegistry;
4+
import org.slf4j.Logger;
5+
36
import graphql.execution.instrumentation.InstrumentationState;
47

58
/**
69
* A base class that keeps track of whether aggressive batching can be used
710
*/
811
public class DataLoaderDispatcherInstrumentationState implements InstrumentationState {
12+
13+
private final FieldLevelTrackingApproach approach;
14+
15+
private final DataLoaderRegistry dataLoaderRegistry;
16+
17+
private final InstrumentationState state;
18+
919
private boolean aggressivelyBatching = true;
1020

21+
public DataLoaderDispatcherInstrumentationState(Logger log, DataLoaderRegistry dataLoaderRegistry) {
22+
23+
this.dataLoaderRegistry = dataLoaderRegistry;
24+
this.approach = new FieldLevelTrackingApproach(log, dataLoaderRegistry);
25+
this.state = approach.createState();
26+
}
27+
1128
boolean isAggressivelyBatching() {
1229
return aggressivelyBatching;
1330
}
@@ -16,5 +33,18 @@ void setAggressivelyBatching(boolean aggressivelyBatching) {
1633
this.aggressivelyBatching = aggressivelyBatching;
1734
}
1835

36+
FieldLevelTrackingApproach getApproach() {
37+
return approach;
38+
}
39+
40+
DataLoaderRegistry getDataLoaderRegistry() {
41+
return dataLoaderRegistry;
42+
}
43+
44+
InstrumentationState getState() {
45+
return state;
46+
}
47+
48+
1949

2050
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class FieldLevelTrackingApproach {
3333
private final DataLoaderRegistry dataLoaderRegistry;
3434
private final Logger log;
3535

36-
private static class CallStack extends DataLoaderDispatcherInstrumentationState {
36+
private static class CallStack implements InstrumentationState {
3737

3838
private final Map<Integer, Integer> expectedFetchCountPerLevel = new LinkedHashMap<>();
3939
private final Map<Integer, Integer> fetchCountPerLevel = new LinkedHashMap<>();

0 commit comments

Comments
 (0)