33import graphql .execution .AbortExecutionException ;
44import graphql .execution .AsyncExecutionStrategy ;
55import graphql .execution .AsyncSerialExecutionStrategy ;
6+ import graphql .execution .DataFetcherExceptionHandler ;
67import graphql .execution .Execution ;
78import graphql .execution .ExecutionId ;
89import graphql .execution .ExecutionIdProvider ;
910import graphql .execution .ExecutionStrategy ;
11+ import graphql .execution .SimpleDataFetcherExceptionHandler ;
1012import graphql .execution .SubscriptionExecutionStrategy ;
1113import graphql .execution .ValueUnboxer ;
1214import graphql .execution .instrumentation .ChainedInstrumentation ;
@@ -87,12 +89,6 @@ public class GraphQL {
8789 private static final Logger log = LoggerFactory .getLogger (GraphQL .class );
8890 private static final Logger logNotSafe = LogKit .getNotPrivacySafeLogger (GraphQL .class );
8991
90- private final static Instrumentation DEFAULT_INSTRUMENTATION = new DataLoaderDispatcherInstrumentation ();
91- private final static ExecutionStrategy DEFAULT_QUERY_STRATEGY = new AsyncExecutionStrategy ();
92- private final static ExecutionStrategy DEFAULT_MUTATION_STRATEGY = new AsyncSerialExecutionStrategy ();
93- private final static ExecutionStrategy DEFAULT_SUBSCRIPTION_STRATEGY = new SubscriptionExecutionStrategy ();
94-
95-
9692 private final GraphQLSchema graphQLSchema ;
9793 private final ExecutionStrategy queryStrategy ;
9894 private final ExecutionStrategy mutationStrategy ;
@@ -103,76 +99,15 @@ public class GraphQL {
10399 private final ValueUnboxer valueUnboxer ;
104100
105101
106- /**
107- * A GraphQL object ready to execute queries
108- *
109- * @param graphQLSchema the schema to use
110- * @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
111- */
112- @ Internal
113- @ Deprecated
114- public GraphQL (GraphQLSchema graphQLSchema ) {
115- this (graphQLSchema , null , null );
116- }
117-
118- /**
119- * A GraphQL object ready to execute queries
120- *
121- * @param graphQLSchema the schema to use
122- * @param queryStrategy the query execution strategy to use
123- * @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
124- */
125- @ Internal
126- @ Deprecated
127- public GraphQL (GraphQLSchema graphQLSchema , ExecutionStrategy queryStrategy ) {
128- this (graphQLSchema , queryStrategy , null );
129- }
130-
131- /**
132- * A GraphQL object ready to execute queries
133- *
134- * @param graphQLSchema the schema to use
135- * @param queryStrategy the query execution strategy to use
136- * @param mutationStrategy the mutation execution strategy to use
137- * @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
138- */
139- @ Internal
140- @ Deprecated
141- public GraphQL (GraphQLSchema graphQLSchema , ExecutionStrategy queryStrategy , ExecutionStrategy mutationStrategy ) {
142- this (graphQLSchema , queryStrategy , mutationStrategy , null , DEFAULT_EXECUTION_ID_PROVIDER , DEFAULT_INSTRUMENTATION , NoOpPreparsedDocumentProvider .INSTANCE , ValueUnboxer .DEFAULT );
143- }
144-
145- /**
146- * A GraphQL object ready to execute queries
147- *
148- * @param graphQLSchema the schema to use
149- * @param queryStrategy the query execution strategy to use
150- * @param mutationStrategy the mutation execution strategy to use
151- * @param subscriptionStrategy the subscription execution strategy to use
152- * @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
153- */
154- @ Internal
155- @ Deprecated
156- public GraphQL (GraphQLSchema graphQLSchema , ExecutionStrategy queryStrategy , ExecutionStrategy mutationStrategy , ExecutionStrategy subscriptionStrategy ) {
157- this (graphQLSchema , queryStrategy , mutationStrategy , subscriptionStrategy , DEFAULT_EXECUTION_ID_PROVIDER , DEFAULT_INSTRUMENTATION , NoOpPreparsedDocumentProvider .INSTANCE , ValueUnboxer .DEFAULT );
158- }
159-
160- private GraphQL (GraphQLSchema graphQLSchema ,
161- ExecutionStrategy queryStrategy ,
162- ExecutionStrategy mutationStrategy ,
163- ExecutionStrategy subscriptionStrategy ,
164- ExecutionIdProvider idProvider ,
165- Instrumentation instrumentation ,
166- PreparsedDocumentProvider preparsedDocumentProvider ,
167- ValueUnboxer valueUnboxer ) {
168- this .graphQLSchema = assertNotNull (graphQLSchema , () -> "graphQLSchema must be non null" );
169- this .queryStrategy = queryStrategy != null ? queryStrategy : DEFAULT_QUERY_STRATEGY ;
170- this .mutationStrategy = mutationStrategy != null ? mutationStrategy : DEFAULT_MUTATION_STRATEGY ;
171- this .subscriptionStrategy = subscriptionStrategy != null ? subscriptionStrategy : DEFAULT_SUBSCRIPTION_STRATEGY ;
172- this .idProvider = assertNotNull (idProvider , () -> "idProvider must be non null" );
173- this .instrumentation = assertNotNull (instrumentation );
174- this .preparsedDocumentProvider = assertNotNull (preparsedDocumentProvider , () -> "preparsedDocumentProvider must be non null" );
175- this .valueUnboxer = valueUnboxer ;
102+ private GraphQL (Builder builder ) {
103+ this .graphQLSchema = assertNotNull (builder .graphQLSchema , () -> "graphQLSchema must be non null" );
104+ this .queryStrategy = assertNotNull (builder .queryExecutionStrategy , () -> "queryStrategy must not be null" );
105+ this .mutationStrategy = assertNotNull (builder .mutationExecutionStrategy , () -> "mutationStrategy must not be null" );
106+ this .subscriptionStrategy = assertNotNull (builder .subscriptionExecutionStrategy , () -> "subscriptionStrategy must not be null" );
107+ this .idProvider = assertNotNull (builder .idProvider , () -> "idProvider must be non null" );
108+ this .instrumentation = assertNotNull (builder .instrumentation , () -> "instrumentation must not be null" );
109+ this .preparsedDocumentProvider = assertNotNull (builder .preparsedDocumentProvider , () -> "preparsedDocumentProvider must be non null" );
110+ this .valueUnboxer = assertNotNull (builder .valueUnboxer , () -> "valueUnboxer must not be null" );
176111 }
177112
178113 /**
@@ -195,9 +130,9 @@ public static Builder newGraphQL(GraphQLSchema graphQLSchema) {
195130 public GraphQL transform (Consumer <GraphQL .Builder > builderConsumer ) {
196131 Builder builder = new Builder (this .graphQLSchema );
197132 builder
198- .queryExecutionStrategy (Optional . ofNullable ( this .queryStrategy ). orElse ( builder . queryExecutionStrategy ) )
199- .mutationExecutionStrategy (Optional . ofNullable ( this .mutationStrategy ). orElse ( builder . mutationExecutionStrategy ) )
200- .subscriptionExecutionStrategy (Optional . ofNullable ( this .subscriptionStrategy ). orElse ( builder . subscriptionExecutionStrategy ) )
133+ .queryExecutionStrategy (this .queryStrategy )
134+ .mutationExecutionStrategy (this .mutationStrategy )
135+ .subscriptionExecutionStrategy (this .subscriptionStrategy )
201136 .executionIdProvider (Optional .ofNullable (this .idProvider ).orElse (builder .idProvider ))
202137 .instrumentation (Optional .ofNullable (this .instrumentation ).orElse (builder .instrumentation ))
203138 .preparsedDocumentProvider (Optional .ofNullable (this .preparsedDocumentProvider ).orElse (builder .preparsedDocumentProvider ));
@@ -210,9 +145,10 @@ public GraphQL transform(Consumer<GraphQL.Builder> builderConsumer) {
210145 @ PublicApi
211146 public static class Builder {
212147 private GraphQLSchema graphQLSchema ;
213- private ExecutionStrategy queryExecutionStrategy = DEFAULT_QUERY_STRATEGY ;
214- private ExecutionStrategy mutationExecutionStrategy = DEFAULT_MUTATION_STRATEGY ;
215- private ExecutionStrategy subscriptionExecutionStrategy = DEFAULT_SUBSCRIPTION_STRATEGY ;
148+ private ExecutionStrategy queryExecutionStrategy ;
149+ private ExecutionStrategy mutationExecutionStrategy ;
150+ private ExecutionStrategy subscriptionExecutionStrategy ;
151+ private DataFetcherExceptionHandler defaultExceptionHandler = new SimpleDataFetcherExceptionHandler ();
216152 private ExecutionIdProvider idProvider = DEFAULT_EXECUTION_ID_PROVIDER ;
217153 private Instrumentation instrumentation = null ; // deliberate default here
218154 private PreparsedDocumentProvider preparsedDocumentProvider = NoOpPreparsedDocumentProvider .INSTANCE ;
@@ -244,6 +180,18 @@ public Builder subscriptionExecutionStrategy(ExecutionStrategy executionStrategy
244180 return this ;
245181 }
246182
183+ /**
184+ * This allows you to set a default {@link graphql.execution.DataFetcherExceptionHandler} that will be used to handle exceptions that happen
185+ * in {@link graphql.schema.DataFetcher} invocations.
186+ *
187+ * @param dataFetcherExceptionHandler the default handler for data fetching exception
188+ * @return this builder
189+ */
190+ public Builder defaultDataFetcherExceptionHandler (DataFetcherExceptionHandler dataFetcherExceptionHandler ) {
191+ this .defaultExceptionHandler = assertNotNull (dataFetcherExceptionHandler , () -> "The DataFetcherExceptionHandler must be non null" );
192+ return this ;
193+ }
194+
247195 public Builder instrumentation (Instrumentation instrumentation ) {
248196 this .instrumentation = assertNotNull (instrumentation , () -> "Instrumentation must be non null" );
249197 return this ;
@@ -282,11 +230,19 @@ public Builder valueUnboxer(ValueUnboxer valueUnboxer) {
282230 }
283231
284232 public GraphQL build () {
285- assertNotNull (graphQLSchema , () -> "graphQLSchema must be non null" );
286- assertNotNull (queryExecutionStrategy , () -> "queryStrategy must be non null" );
287- assertNotNull (idProvider , () -> "idProvider must be non null" );
288- final Instrumentation augmentedInstrumentation = checkInstrumentationDefaultState (instrumentation , doNotAddDefaultInstrumentations );
289- return new GraphQL (graphQLSchema , queryExecutionStrategy , mutationExecutionStrategy , subscriptionExecutionStrategy , idProvider , augmentedInstrumentation , preparsedDocumentProvider , valueUnboxer );
233+ // we use the data fetcher exception handler unless they set their own strategy in which case bets are off
234+ if (queryExecutionStrategy == null ) {
235+ this .queryExecutionStrategy = new AsyncExecutionStrategy (this .defaultExceptionHandler );
236+ }
237+ if (mutationExecutionStrategy == null ) {
238+ this .mutationExecutionStrategy = new AsyncSerialExecutionStrategy (this .defaultExceptionHandler );
239+ }
240+ if (subscriptionExecutionStrategy == null ) {
241+ this .subscriptionExecutionStrategy = new SubscriptionExecutionStrategy (this .defaultExceptionHandler );
242+ }
243+
244+ this .instrumentation = checkInstrumentationDefaultState (this .instrumentation , this .doNotAddDefaultInstrumentations );
245+ return new GraphQL (this );
290246 }
291247 }
292248
0 commit comments