|
1 | 1 | package graphql.execution; |
2 | 2 |
|
3 | | -import graphql.ErrorType; |
4 | 3 | import graphql.ExecutionResult; |
5 | 4 | import graphql.ExecutionResultImpl; |
6 | | -import graphql.GraphQLError; |
7 | 5 | import graphql.PublicSpi; |
8 | 6 | import graphql.SerializationError; |
9 | 7 | import graphql.TypeResolutionEnvironment; |
|
14 | 12 | import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters; |
15 | 13 | import graphql.introspection.Introspection; |
16 | 14 | import graphql.language.Field; |
17 | | -import graphql.language.SourceLocation; |
18 | 15 | import graphql.schema.CoercingSerializeException; |
19 | 16 | import graphql.schema.DataFetcher; |
20 | 17 | import graphql.schema.DataFetchingEnvironment; |
|
40 | 37 | import java.util.Optional; |
41 | 38 | import java.util.concurrent.CompletableFuture; |
42 | 39 | import java.util.concurrent.CompletionException; |
43 | | -import java.util.function.Function; |
44 | 40 | import java.util.stream.IntStream; |
45 | 41 |
|
46 | 42 | import static graphql.execution.ExecutionTypeInfo.newTypeInfo; |
@@ -230,15 +226,34 @@ protected CompletableFuture<Object> fetchField(ExecutionContext executionContext |
230 | 226 | fetchedValue = new CompletableFuture<>(); |
231 | 227 | fetchedValue.completeExceptionally(e); |
232 | 228 | } |
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 | + } |
242 | 257 | } |
243 | 258 |
|
244 | 259 | private void handleFetchingException(ExecutionContext executionContext, |
@@ -332,15 +347,6 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec |
332 | 347 | Object result = unboxPossibleOptional(parameters.source()); |
333 | 348 | GraphQLType fieldType = typeInfo.getType(); |
334 | 349 |
|
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 | | - |
344 | 350 | if (result == null) { |
345 | 351 | return completedFuture(new ExecutionResultImpl(parameters.nonNullFieldValidator().validate(parameters.path(), null), null)); |
346 | 352 | } else if (fieldType instanceof GraphQLList) { |
|
0 commit comments