Skip to content

Commit

Permalink
Merge pull request #3235 from graphql-java/avoid_cf_allocation_data_f…
Browse files Browse the repository at this point in the history
…etch

This avoids a CF allocation on data fetcher in the main case where there is no exception
  • Loading branch information
bbakerman committed Jun 7, 2023
2 parents 7b6a9ee + 2ba0d5d commit d4f1756
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,10 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC
.handle((result, exception) -> {
fetchCtx.onCompleted(result, exception);
if (exception != null) {
return handleFetchingException(executionContext, dataFetchingEnvironment.get(), exception);
return handleFetchingException(dataFetchingEnvironment.get(), exception);
} else {
return CompletableFuture.completedFuture(result);
// we can simply return the fetched value CF and avoid a allocation
return fetchedValue;
}
})
.thenCompose(Function.identity())
Expand Down Expand Up @@ -331,7 +332,7 @@ protected FetchedValue unboxPossibleDataFetcherResult(ExecutionContext execution
if (result instanceof DataFetcherResult) {
DataFetcherResult<?> dataFetcherResult = (DataFetcherResult<?>) result;
executionContext.addErrors(dataFetcherResult.getErrors());
addExtensionsIfPresent(executionContext,dataFetcherResult);
addExtensionsIfPresent(executionContext, dataFetcherResult);

Object localContext = dataFetcherResult.getLocalContext();
if (localContext == null) {
Expand Down Expand Up @@ -363,9 +364,9 @@ private void addExtensionsIfPresent(ExecutionContext executionContext, DataFetch
}
}

protected <T> CompletableFuture<T> handleFetchingException(ExecutionContext executionContext,
DataFetchingEnvironment environment,
Throwable e) {
protected <T> CompletableFuture<T> handleFetchingException(
DataFetchingEnvironment environment,
Throwable e) {
DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters.newExceptionParameters()
.dataFetchingEnvironment(environment)
.exception(e)
Expand Down Expand Up @@ -711,19 +712,6 @@ private Object handleCoercionProblem(ExecutionContext context, ExecutionStrategy
return null;
}

/**
* Converts an object that is known to should be an Iterable into one
*
* @param result the result object
*
* @return an Iterable from that object
*
* @throws java.lang.ClassCastException if it's not an Iterable
*/
protected Iterable<Object> toIterable(Object result) {
return FpKit.toIterable(result);
}

protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
// we can avoid a method call and type resolver environment allocation if we know it's an object type
if (fieldType instanceof GraphQLObjectType) {
Expand Down

0 comments on commit d4f1756

Please sign in to comment.