2828import java .util .LinkedHashMap ;
2929import java .util .Map ;
3030import 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 ());
0 commit comments