|
5 | 5 | import graphql.ExecutionResultImpl; |
6 | 6 | import graphql.GraphQLException; |
7 | 7 | import graphql.PublicApi; |
| 8 | +import graphql.execution.Async; |
8 | 9 | import graphql.execution.DataFetcherExceptionHandler; |
9 | 10 | import graphql.execution.DataFetcherExceptionHandlerParameters; |
10 | 11 | import graphql.execution.ExecutionContext; |
|
47 | 48 | import java.util.Queue; |
48 | 49 | import java.util.concurrent.CompletableFuture; |
49 | 50 | import java.util.concurrent.CompletionException; |
50 | | -import java.util.concurrent.CompletionStage; |
51 | 51 | import java.util.function.BiFunction; |
52 | 52 | import java.util.stream.IntStream; |
53 | 53 |
|
@@ -160,6 +160,7 @@ private void executeImpl(ExecutionContext executionContext, |
160 | 160 |
|
161 | 161 | ExecutionNode finalCurNode = curNode; |
162 | 162 | Iterator<String> finalCurFieldNames = curFieldNames; |
| 163 | + |
163 | 164 | resolveField(executionContext, newParameters, fieldName, curNode) |
164 | 165 | .whenComplete((childNodes, exception) -> { |
165 | 166 | if (exception != null) { |
@@ -201,6 +202,52 @@ private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext exe |
201 | 202 |
|
202 | 203 | } |
203 | 204 |
|
| 205 | + private CompletableFuture<List<FetchedValue>> fetchData(ExecutionContext executionContext, |
| 206 | + ExecutionStrategyParameters parameters, |
| 207 | + String fieldName, |
| 208 | + ExecutionNode node, |
| 209 | + GraphQLFieldDefinition fieldDef) { |
| 210 | + GraphQLObjectType parentType = node.getType(); |
| 211 | + List<Field> fields = node.getFields().get(fieldName); |
| 212 | + List<MapOrList> parentResults = node.getParentResults(); |
| 213 | + |
| 214 | + Map<String, Object> argumentValues = valuesResolver.getArgumentValues( |
| 215 | + fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables()); |
| 216 | + |
| 217 | + GraphQLOutputType fieldType = fieldDef.getType(); |
| 218 | + DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields); |
| 219 | + |
| 220 | + DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) |
| 221 | + .source(node.getSources()) |
| 222 | + .arguments(argumentValues) |
| 223 | + .fieldDefinition(fieldDef) |
| 224 | + .fields(fields) |
| 225 | + .fieldType(fieldDef.getType()) |
| 226 | + .fieldTypeInfo(parameters.typeInfo()) |
| 227 | + .parentType(parentType) |
| 228 | + .selectionSet(fieldCollector) |
| 229 | + .build(); |
| 230 | + |
| 231 | + Instrumentation instrumentation = executionContext.getInstrumentation(); |
| 232 | + InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch( |
| 233 | + new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment) |
| 234 | + ); |
| 235 | + |
| 236 | + CompletableFuture<Object> fetchedValue; |
| 237 | + try { |
| 238 | + BatchedDataFetcher dataFetcher = getDataFetcher(fieldDef); |
| 239 | + Object fetchedValueRaw = dataFetcher.get(environment); |
| 240 | + fetchedValue = Async.toCompletableFuture(fetchedValueRaw); |
| 241 | + } catch (Exception e) { |
| 242 | + fetchedValue = new CompletableFuture<>(); |
| 243 | + fetchedValue.completeExceptionally(e); |
| 244 | + } |
| 245 | + return fetchedValue |
| 246 | + .thenApply((result) -> assertResult(parentResults, result)) |
| 247 | + .whenComplete(fetchCtx::onEnd) |
| 248 | + .handle(handleResult(executionContext, parameters, parentResults, fields, fieldDef, argumentValues, environment)); |
| 249 | + } |
| 250 | + |
204 | 251 | private List<ExecutionNode> completeValues(ExecutionContext executionContext, GraphQLObjectType parentType, |
205 | 252 | List<FetchedValue> fetchedValues, String fieldName, List<Field> fields, |
206 | 253 | GraphQLOutputType fieldType, ExecutionTypeInfo typeInfo, Map<String, Object> argumentValues) { |
@@ -373,56 +420,6 @@ private boolean isObject(GraphQLType type) { |
373 | 420 | type instanceof GraphQLUnionType; |
374 | 421 | } |
375 | 422 |
|
376 | | - @SuppressWarnings("unchecked") |
377 | | - private CompletableFuture<List<FetchedValue>> fetchData(ExecutionContext executionContext, |
378 | | - ExecutionStrategyParameters parameters, |
379 | | - String fieldName, |
380 | | - ExecutionNode node, |
381 | | - GraphQLFieldDefinition fieldDef) { |
382 | | - GraphQLObjectType parentType = node.getType(); |
383 | | - List<Field> fields = node.getFields().get(fieldName); |
384 | | - List<MapOrList> parentResults = node.getParentResults(); |
385 | | - |
386 | | - Map<String, Object> argumentValues = valuesResolver.getArgumentValues( |
387 | | - fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables()); |
388 | | - |
389 | | - GraphQLOutputType fieldType = fieldDef.getType(); |
390 | | - DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields); |
391 | | - |
392 | | - DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) |
393 | | - .source(node.getSources()) |
394 | | - .arguments(argumentValues) |
395 | | - .fieldDefinition(fieldDef) |
396 | | - .fields(fields) |
397 | | - .fieldType(fieldDef.getType()) |
398 | | - .fieldTypeInfo(parameters.typeInfo()) |
399 | | - .parentType(parentType) |
400 | | - .selectionSet(fieldCollector) |
401 | | - .build(); |
402 | | - |
403 | | - Instrumentation instrumentation = executionContext.getInstrumentation(); |
404 | | - InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch( |
405 | | - new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment) |
406 | | - ); |
407 | | - |
408 | | - CompletableFuture<Object> valuesFuture; |
409 | | - try { |
410 | | - Object rawValue = getDataFetcher(fieldDef).get(environment); |
411 | | - if (rawValue instanceof CompletionStage) { |
412 | | - valuesFuture = ((CompletionStage) rawValue).toCompletableFuture(); |
413 | | - } else { |
414 | | - valuesFuture = CompletableFuture.completedFuture(rawValue); |
415 | | - } |
416 | | - } catch (Exception e) { |
417 | | - valuesFuture = new CompletableFuture<>(); |
418 | | - valuesFuture.completeExceptionally(e); |
419 | | - } |
420 | | - return valuesFuture |
421 | | - .thenApply((result) -> assertResult(parentResults, result)) |
422 | | - .whenComplete(fetchCtx::onEnd) |
423 | | - .handle(handleResult(executionContext, parameters, parentResults, fields, fieldDef, argumentValues, environment)); |
424 | | - } |
425 | | - |
426 | 423 | private BiFunction<List<Object>, Throwable, List<FetchedValue>> handleResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters, List<MapOrList> parentResults, List<Field> fields, GraphQLFieldDefinition fieldDef, Map<String, Object> argumentValues, DataFetchingEnvironment environment) { |
427 | 424 | return (result, exception) -> { |
428 | 425 | if (exception != null) { |
|
0 commit comments