1212import graphql .execution .SimpleDataFetcherExceptionHandler ;
1313import graphql .execution .SubscriptionExecutionStrategy ;
1414import graphql .execution .ValueUnboxer ;
15- import graphql .execution .instrumentation .ChainedInstrumentation ;
1615import graphql .execution .instrumentation .DocumentAndVariables ;
1716import graphql .execution .instrumentation .Instrumentation ;
1817import graphql .execution .instrumentation .InstrumentationContext ;
1918import graphql .execution .instrumentation .InstrumentationState ;
20- import graphql .execution .instrumentation .NoContextChainedInstrumentation ;
2119import graphql .execution .instrumentation .SimplePerformantInstrumentation ;
22- import graphql .execution .instrumentation .dataloader .DataLoaderDispatcherInstrumentation ;
2320import graphql .execution .instrumentation .parameters .InstrumentationCreateStateParameters ;
2421import graphql .execution .instrumentation .parameters .InstrumentationExecutionParameters ;
2522import graphql .execution .instrumentation .parameters .InstrumentationValidationParameters ;
3027import graphql .schema .GraphQLSchema ;
3128import graphql .validation .ValidationError ;
3229
33- import java .util .ArrayList ;
3430import java .util .List ;
3531import java .util .Locale ;
3632import java .util .Optional ;
@@ -96,6 +92,7 @@ public class GraphQL {
9692 private final Instrumentation instrumentation ;
9793 private final PreparsedDocumentProvider preparsedDocumentProvider ;
9894 private final ValueUnboxer valueUnboxer ;
95+ private final boolean doNotAutomaticallyDispatchDataLoader ;
9996
10097
10198 private GraphQL (Builder builder ) {
@@ -107,6 +104,7 @@ private GraphQL(Builder builder) {
107104 this .instrumentation = assertNotNull (builder .instrumentation , () -> "instrumentation must not be null" );
108105 this .preparsedDocumentProvider = assertNotNull (builder .preparsedDocumentProvider , () -> "preparsedDocumentProvider must be non null" );
109106 this .valueUnboxer = assertNotNull (builder .valueUnboxer , () -> "valueUnboxer must not be null" );
107+ this .doNotAutomaticallyDispatchDataLoader = builder .doNotAutomaticallyDispatchDataLoader ;
110108 }
111109
112110 /**
@@ -151,6 +149,10 @@ public Instrumentation getInstrumentation() {
151149 return instrumentation ;
152150 }
153151
152+ public boolean isDoNotAutomaticallyDispatchDataLoader () {
153+ return doNotAutomaticallyDispatchDataLoader ;
154+ }
155+
154156 /**
155157 * @return the PreparsedDocumentProvider for this {@link GraphQL} instance
156158 */
@@ -209,7 +211,7 @@ public static class Builder {
209211 private ExecutionIdProvider idProvider = DEFAULT_EXECUTION_ID_PROVIDER ;
210212 private Instrumentation instrumentation = null ; // deliberate default here
211213 private PreparsedDocumentProvider preparsedDocumentProvider = NoOpPreparsedDocumentProvider .INSTANCE ;
212- private boolean doNotAddDefaultInstrumentations = false ;
214+ private boolean doNotAutomaticallyDispatchDataLoader = false ;
213215 private ValueUnboxer valueUnboxer = ValueUnboxer .DEFAULT ;
214216
215217
@@ -265,20 +267,15 @@ public Builder executionIdProvider(ExecutionIdProvider executionIdProvider) {
265267 return this ;
266268 }
267269
270+
268271 /**
269- * For performance reasons you can opt into situation where the default instrumentations (such
270- * as {@link graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation} will not be
271- * automatically added into the graphql instance.
272- * <p>
273- * For most situations this is not needed unless you are really pushing the boundaries of performance
274- * <p>
275- * By default a certain graphql instrumentations will be added to the mix to more easily enable certain functionality. This
276- * allows you to stop this behavior
272+ * Deactivates the automatic dispatching of DataLoaders.
273+ * If deactivated the user is responsible for dispatching the DataLoaders manually.
277274 *
278275 * @return this builder
279276 */
280- public Builder doNotAddDefaultInstrumentations () {
281- this .doNotAddDefaultInstrumentations = true ;
277+ public Builder doNotAutomaticallyDispatchDataLoader () {
278+ this .doNotAutomaticallyDispatchDataLoader = true ;
282279 return this ;
283280 }
284281
@@ -299,7 +296,9 @@ public GraphQL build() {
299296 this .subscriptionExecutionStrategy = new SubscriptionExecutionStrategy (this .defaultExceptionHandler );
300297 }
301298
302- this .instrumentation = checkInstrumentationDefaultState (this .instrumentation , this .doNotAddDefaultInstrumentations );
299+ if (instrumentation == null ) {
300+ this .instrumentation = SimplePerformantInstrumentation .INSTANCE ;
301+ }
303302 return new GraphQL (this );
304303 }
305304 }
@@ -540,42 +539,16 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
540539 return validationErrors ;
541540 }
542541
543- private CompletableFuture <ExecutionResult > execute (ExecutionInput executionInput , Document document , GraphQLSchema graphQLSchema , InstrumentationState instrumentationState ) {
542+ private CompletableFuture <ExecutionResult > execute (ExecutionInput executionInput ,
543+ Document document ,
544+ GraphQLSchema graphQLSchema ,
545+ InstrumentationState instrumentationState
546+ ) {
544547
545- Execution execution = new Execution (queryStrategy , mutationStrategy , subscriptionStrategy , instrumentation , valueUnboxer );
548+ Execution execution = new Execution (queryStrategy , mutationStrategy , subscriptionStrategy , instrumentation , valueUnboxer , doNotAutomaticallyDispatchDataLoader );
546549 ExecutionId executionId = executionInput .getExecutionId ();
547550
548551 return execution .execute (document , graphQLSchema , executionId , executionInput , instrumentationState );
549552 }
550553
551- private static Instrumentation checkInstrumentationDefaultState (Instrumentation instrumentation , boolean doNotAddDefaultInstrumentations ) {
552- if (doNotAddDefaultInstrumentations ) {
553- return instrumentation == null ? SimplePerformantInstrumentation .INSTANCE : instrumentation ;
554- }
555- if (instrumentation instanceof DataLoaderDispatcherInstrumentation ) {
556- return instrumentation ;
557- }
558- if (instrumentation instanceof NoContextChainedInstrumentation ) {
559- return instrumentation ;
560- }
561- if (instrumentation == null ) {
562- return new DataLoaderDispatcherInstrumentation ();
563- }
564-
565- //
566- // if we don't have a DataLoaderDispatcherInstrumentation in play, we add one. We want DataLoader to be 1st class in graphql without requiring
567- // people to remember to wire it in. Later we may decide to have more default instrumentations but for now it's just the one
568- //
569- List <Instrumentation > instrumentationList = new ArrayList <>();
570- if (instrumentation instanceof ChainedInstrumentation ) {
571- instrumentationList .addAll (((ChainedInstrumentation ) instrumentation ).getInstrumentations ());
572- } else {
573- instrumentationList .add (instrumentation );
574- }
575- boolean containsDLInstrumentation = instrumentationList .stream ().anyMatch (instr -> instr instanceof DataLoaderDispatcherInstrumentation );
576- if (!containsDLInstrumentation ) {
577- instrumentationList .add (new DataLoaderDispatcherInstrumentation ());
578- }
579- return new ChainedInstrumentation (instrumentationList );
580- }
581554}
0 commit comments