Skip to content

Commit 75fe340

Browse files
committed
fix exception handling,
naming
1 parent 7d5308a commit 75fe340

7 files changed

Lines changed: 37 additions & 27 deletions

‎src/main/java/graphql/execution/AbstractAsyncExecutionStrategy.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AbstractAsyncExecutionStrategy(DataFetcherExceptionHandler dataFetcherExc
2222
}
2323

2424
protected BiConsumer<List<Object>, Throwable> handleResults(ExecutionContext executionContext, List<String> fieldNames, CompletableFuture<ExecutionResult> overallResult) {
25-
return (List<Object> results, Throwable exception) -> executionContext.runnable(() -> {
25+
return (List<Object> results, Throwable exception) -> executionContext.run(() -> {
2626
if (exception != null) {
2727
handleNonNullException(executionContext, overallResult, exception);
2828
return;

‎src/main/java/graphql/execution/AsyncExecutionStrategy.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AsyncExecutionStrategy(DataFetcherExceptionHandler exceptionHandler) {
3838
@Override
3939
@SuppressWarnings("FutureReturnValueIgnored")
4040
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
41-
return executionContext.run(() -> {
41+
return executionContext.call(() -> {
4242
DataLoaderDispatchStrategy dataLoaderDispatcherStrategy = executionContext.getDataLoaderDispatcherStrategy();
4343
dataLoaderDispatcherStrategy.executionStrategy(executionContext, parameters);
4444
Instrumentation instrumentation = executionContext.getInstrumentation();
@@ -61,7 +61,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
6161
executionStrategyCtx.onDispatched();
6262

6363
futures.await().whenComplete((completeValueInfos, throwable) -> {
64-
executionContext.runnable(() -> {
64+
executionContext.run(() -> {
6565
List<String> fieldsExecutedOnInitialResult = deferredExecutionSupport.getNonDeferredFieldNames(fieldNames);
6666

6767
BiConsumer<List<Object>, Throwable> handleResultsConsumer = handleResults(executionContext, fieldsExecutedOnInitialResult, overallResult);
@@ -78,7 +78,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
7878
executionStrategyCtx.onFieldValuesInfo(completeValueInfos);
7979
fieldValuesFutures.await().whenComplete(handleResultsConsumer);
8080
});
81-
}).exceptionally((ex) -> executionContext.run(() -> {
81+
}).exceptionally((ex) -> executionContext.call(() -> {
8282
// if there are any issues with combining/handling the field results,
8383
// complete the future at all costs and bubble up any thrown exception so
8484
// the execution does not hang.

‎src/main/java/graphql/execution/AsyncSerialExecutionStrategy.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public AsyncSerialExecutionStrategy(DataFetcherExceptionHandler exceptionHandler
3232
@Override
3333
@SuppressWarnings({"TypeParameterUnusedInFormals", "FutureReturnValueIgnored"})
3434
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
35-
return executionContext.run(() -> {
35+
return executionContext.call(() -> {
3636
DataLoaderDispatchStrategy dataLoaderDispatcherStrategy = executionContext.getDataLoaderDispatcherStrategy();
3737

3838
Instrumentation instrumentation = executionContext.getInstrumentation();
@@ -50,7 +50,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
5050
return CompletableFuture.completedFuture(isNotSensible.get());
5151
}
5252

53-
CompletableFuture<List<Object>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, prevResults) -> executionContext.run(() -> {
53+
CompletableFuture<List<Object>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, prevResults) -> executionContext.call(() -> {
5454
MergedField currentField = fields.getSubField(fieldName);
5555
ResultPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField));
5656
ExecutionStrategyParameters newParameters = parameters
@@ -77,7 +77,7 @@ private Object resolveSerialField(ExecutionContext executionContext,
7777
Object fieldWithInfo = resolveFieldWithInfo(executionContext, newParameters);
7878
if (fieldWithInfo instanceof CompletableFuture) {
7979
//noinspection unchecked
80-
return ((CompletableFuture<FieldValueInfo>) fieldWithInfo).thenCompose(fvi -> executionContext.run(() -> {
80+
return ((CompletableFuture<FieldValueInfo>) fieldWithInfo).thenCompose(fvi -> executionContext.call(() -> {
8181
dataLoaderDispatcherStrategy.executionStrategyOnFieldValuesInfo(List.of(fvi));
8282
CompletableFuture<Object> fieldValueFuture = fvi.getFieldValueFuture();
8383
return fieldValueFuture;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package graphql.execution;
2+
3+
import graphql.Internal;
4+
import org.jspecify.annotations.NullMarked;
5+
6+
/**
7+
* We just want to be able to call a method that returns a value but doesn't throw any checked exceptions.
8+
*
9+
* @param <T>
10+
*/
11+
@Internal
12+
@NullMarked
13+
@FunctionalInterface
14+
public interface CallableWithoutException<T> {
15+
T call();
16+
}

‎src/main/java/graphql/execution/ExecutionContext.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Locale;
2929
import java.util.Map;
3030
import java.util.Set;
31-
import java.util.concurrent.Callable;
3231
import java.util.concurrent.atomic.AtomicInteger;
3332
import java.util.concurrent.atomic.AtomicReference;
3433
import java.util.function.Consumer;
@@ -363,24 +362,19 @@ public boolean isRunning() {
363362
return isRunning.get() > 0;
364363
}
365364

366-
public <T> T run(Callable<T> callable) {
365+
public <T> T call(CallableWithoutException<T> callable) {
367366
isRunning.incrementAndGet();
368367
try {
369368
return callable.call();
370-
} catch (Exception e) {
371-
throw new RuntimeException(e);
372369
} finally {
373370
isRunning.decrementAndGet();
374371
}
375372
}
376373

377-
public void runnable(Runnable runnable) {
374+
public void run(Runnable runnable) {
378375
isRunning.incrementAndGet();
379376
try {
380377
runnable.run();
381-
;
382-
} catch (Exception e) {
383-
throw new RuntimeException(e);
384378
} finally {
385379
isRunning.decrementAndGet();
386380
}

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static String mkNameForPath(List<Field> currentField) {
201201
@SuppressWarnings("unchecked")
202202
@DuckTyped(shape = "CompletableFuture<Map<String, Object>> | Map<String, Object>")
203203
protected Object executeObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
204-
return executionContext.run(() -> {
204+
return executionContext.call(() -> {
205205
DataLoaderDispatchStrategy dataLoaderDispatcherStrategy = executionContext.getDataLoaderDispatcherStrategy();
206206
dataLoaderDispatcherStrategy.executeObject(executionContext, parameters);
207207
Instrumentation instrumentation = executionContext.getInstrumentation();
@@ -226,7 +226,7 @@ protected Object executeObject(ExecutionContext executionContext, ExecutionStrat
226226
if (fieldValueInfosResult instanceof CompletableFuture) {
227227
CompletableFuture<List<FieldValueInfo>> fieldValueInfos = (CompletableFuture<List<FieldValueInfo>>) fieldValueInfosResult;
228228
fieldValueInfos.whenComplete((completeValueInfos, throwable) -> {
229-
executionContext.runnable(() -> {
229+
executionContext.run(() -> {
230230
if (throwable != null) {
231231
handleResultsConsumer.accept(null, throwable);
232232
return;
@@ -237,7 +237,7 @@ protected Object executeObject(ExecutionContext executionContext, ExecutionStrat
237237
resolveObjectCtx.onFieldValuesInfo(completeValueInfos);
238238
resultFutures.await().whenComplete(handleResultsConsumer);
239239
});
240-
}).exceptionally((ex) -> executionContext.run(() -> {
240+
}).exceptionally((ex) -> executionContext.call(() -> {
241241
// if there are any issues with combining/handling the field results,
242242
// complete the future at all costs and bubble up any thrown exception so
243243
// the execution does not hang.
@@ -280,7 +280,7 @@ protected Object executeObject(ExecutionContext executionContext, ExecutionStrat
280280

281281
private BiConsumer<List<Object>, Throwable> buildFieldValueMap(List<String> fieldNames, CompletableFuture<Map<String, Object>> overallResult, ExecutionContext executionContext) {
282282
return (List<Object> results, Throwable exception) -> {
283-
executionContext.runnable(() -> {
283+
executionContext.run(() -> {
284284
if (exception != null) {
285285
handleValueException(overallResult, exception, executionContext);
286286
return;
@@ -514,7 +514,7 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
514514
@SuppressWarnings("unchecked")
515515
CompletableFuture<Object> fetchedValue = (CompletableFuture<Object>) fetchedObject;
516516
return fetchedValue
517-
.handle((result, exception) -> executionContext.run(() -> {
517+
.handle((result, exception) -> executionContext.call(() -> {
518518
fetchCtx.onCompleted(result, exception);
519519
if (exception != null) {
520520
CompletableFuture<Object> handleFetchingExceptionResult = handleFetchingException(dataFetchingEnvironment.get(), parameters, exception);
@@ -560,7 +560,7 @@ protected Supplier<ExecutableNormalizedField> getNormalizedField(ExecutionContex
560560
protected FetchedValue unboxPossibleDataFetcherResult(ExecutionContext executionContext,
561561
ExecutionStrategyParameters parameters,
562562
Object result) {
563-
return executionContext.run(() -> {
563+
return executionContext.call(() -> {
564564
if (result instanceof DataFetcherResult) {
565565
DataFetcherResult<?> dataFetcherResult = (DataFetcherResult<?>) result;
566566

@@ -646,7 +646,7 @@ protected FieldValueInfo completeField(ExecutionContext executionContext, Execut
646646
}
647647

648648
private FieldValueInfo completeField(GraphQLFieldDefinition fieldDef, ExecutionContext executionContext, ExecutionStrategyParameters parameters, FetchedValue fetchedValue) {
649-
return executionContext.run(() -> {
649+
return executionContext.call(() -> {
650650
GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType();
651651
ExecutionStepInfo executionStepInfo = createExecutionStepInfo(executionContext, parameters, fieldDef, parentType);
652652

@@ -843,7 +843,7 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
843843
overallResult.whenComplete(completeListCtx::onCompleted);
844844

845845
resultsFuture.whenComplete((results, exception) -> {
846-
executionContext.runnable(() -> {
846+
executionContext.run(() -> {
847847
if (exception != null) {
848848
handleValueException(overallResult, exception, executionContext);
849849
return;

‎src/main/java/graphql/execution/SubscriptionExecutionStrategy.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public SubscriptionExecutionStrategy(DataFetcherExceptionHandler dataFetcherExce
5656

5757
@Override
5858
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
59-
return executionContext.run(() -> {
59+
return executionContext.call(() -> {
6060
Instrumentation instrumentation = executionContext.getInstrumentation();
6161
InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
6262
ExecutionStrategyInstrumentationContext executionStrategyCtx = ExecutionStrategyInstrumentationContext.nonNullCtx(instrumentation.beginExecutionStrategy(
@@ -69,7 +69,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
6969
//
7070
// when the upstream source event stream completes, subscribe to it and wire in our adapter
7171
CompletableFuture<ExecutionResult> overallResult = sourceEventStream.thenApply((publisher) -> {
72-
return executionContext.run(() -> {
72+
return executionContext.call(() -> {
7373
if (publisher == null) {
7474
ExecutionResultImpl executionResult = new ExecutionResultImpl(null, executionContext.getErrors());
7575
return executionResult;
@@ -112,7 +112,7 @@ private CompletableFuture<Publisher<Object>> createSourceEventStream(ExecutionCo
112112
ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(parameters);
113113

114114
CompletableFuture<FetchedValue> fieldFetched = Async.toCompletableFuture(fetchField(executionContext, newParameters));
115-
return fieldFetched.thenApply(fetchedValue -> executionContext.run(() -> {
115+
return fieldFetched.thenApply(fetchedValue -> executionContext.call(() -> {
116116
Object publisher = fetchedValue.getFetchedValue();
117117
if (publisher != null) {
118118
assertTrue(publisher instanceof Publisher, () -> "Your data fetcher must return a Publisher of events when using graphql subscriptions");
@@ -136,7 +136,7 @@ private CompletableFuture<Publisher<Object>> createSourceEventStream(ExecutionCo
136136
*/
137137

138138
private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object eventPayload) {
139-
return executionContext.run(() -> {
139+
return executionContext.call(() -> {
140140
Instrumentation instrumentation = executionContext.getInstrumentation();
141141

142142
ExecutionContext newExecutionContext = executionContext.transform(builder -> builder

0 commit comments

Comments
 (0)