Skip to content

Commit 07f996e

Browse files
mrdon-atlassianbbakerman
authored andcommitted
Address PR feedback
* Moved java unit test to groovy * Added more tests * Moved result processing to fetchField * Fixed issue with missing path
1 parent 2d00806 commit 07f996e

File tree

6 files changed

+473
-121
lines changed

6 files changed

+473
-121
lines changed

src/main/java/graphql/execution/DataFetcherResult.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.execution;
22

33
import graphql.GraphQLError;
4+
import graphql.PublicApi;
45
import graphql.schema.DataFetcher;
56

67
import java.util.List;
@@ -14,6 +15,7 @@
1415
*
1516
* @param <T> The type of the data fetched
1617
*/
18+
@PublicApi
1719
public class DataFetcherResult<T> {
1820

1921
private final T data;

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package graphql.execution;
22

3-
import graphql.ErrorType;
43
import graphql.ExecutionResult;
54
import graphql.ExecutionResultImpl;
6-
import graphql.GraphQLError;
75
import graphql.PublicSpi;
86
import graphql.SerializationError;
97
import graphql.TypeResolutionEnvironment;
@@ -14,7 +12,6 @@
1412
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters;
1513
import graphql.introspection.Introspection;
1614
import graphql.language.Field;
17-
import graphql.language.SourceLocation;
1815
import graphql.schema.CoercingSerializeException;
1916
import graphql.schema.DataFetcher;
2017
import graphql.schema.DataFetchingEnvironment;
@@ -40,7 +37,6 @@
4037
import java.util.Optional;
4138
import java.util.concurrent.CompletableFuture;
4239
import java.util.concurrent.CompletionException;
43-
import java.util.function.Function;
4440
import java.util.stream.IntStream;
4541

4642
import static graphql.execution.ExecutionTypeInfo.newTypeInfo;
@@ -230,15 +226,34 @@ protected CompletableFuture<Object> fetchField(ExecutionContext executionContext
230226
fetchedValue = new CompletableFuture<>();
231227
fetchedValue.completeExceptionally(e);
232228
}
233-
return fetchedValue.handle((result, exception) -> {
234-
fetchCtx.onEnd(result, exception);
235-
if (exception != null) {
236-
handleFetchingException(executionContext, parameters, field, fieldDef, argumentValues, environment, exception);
237-
return null;
238-
} else {
239-
return result;
240-
}
241-
});
229+
return fetchedValue
230+
.thenApply(result -> processPossibleDataFetcherResult(executionContext, parameters, result))
231+
.handle((result, exception) -> {
232+
fetchCtx.onEnd(result, exception);
233+
if (exception != null) {
234+
handleFetchingException(executionContext, parameters, field, fieldDef, argumentValues, environment, exception);
235+
return null;
236+
} else {
237+
return result;
238+
}
239+
});
240+
}
241+
242+
Object processPossibleDataFetcherResult(ExecutionContext executionContext,
243+
ExecutionStrategyParameters parameters,
244+
Object result) {
245+
if (result != null && result instanceof DataFetcherResult) {
246+
//noinspection unchecked
247+
DataFetcherResult<?> dataFetcherResult = (DataFetcherResult)result;
248+
dataFetcherResult.getErrors().stream()
249+
.map(relError -> new AbsoluteGraphQLError(parameters, relError))
250+
.forEach(e -> executionContext.addError(e, Optional.ofNullable(e.getPath())
251+
.map(path -> ExecutionPath.fromList(e.getPath()))
252+
.orElse(parameters.path())));
253+
return dataFetcherResult.getData();
254+
} else {
255+
return result;
256+
}
242257
}
243258

244259
private void handleFetchingException(ExecutionContext executionContext,
@@ -332,15 +347,6 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
332347
Object result = unboxPossibleOptional(parameters.source());
333348
GraphQLType fieldType = typeInfo.getType();
334349

335-
if (result != null && result instanceof DataFetcherResult) {
336-
//noinspection unchecked
337-
DataFetcherResult<Object> dataFetcherResult = (DataFetcherResult)result;
338-
result = dataFetcherResult.getData();
339-
dataFetcherResult.getErrors().stream()
340-
.map(relError -> new AbsoluteGraphQLError(parameters, relError))
341-
.forEach(e -> executionContext.addError(e, ExecutionPath.fromList(e.getPath())));
342-
}
343-
344350
if (result == null) {
345351
return completedFuture(new ExecutionResultImpl(parameters.nonNullFieldValidator().validate(parameters.path(), null), null));
346352
} else if (fieldType instanceof GraphQLList) {

0 commit comments

Comments
 (0)