|
1 | 1 | package graphql.execution; |
2 | 2 |
|
3 | 3 | import graphql.ExecutionResult; |
| 4 | +import graphql.execution.defer.DeferSupport; |
| 5 | +import graphql.execution.defer.DeferredCall; |
| 6 | +import graphql.execution.defer.DeferredErrorSupport; |
| 7 | +import graphql.execution.instrumentation.DeferredFieldInstrumentationContext; |
4 | 8 | import graphql.PublicApi; |
5 | 9 | import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext; |
6 | 10 | import graphql.execution.instrumentation.Instrumentation; |
| 11 | +import graphql.execution.instrumentation.parameters.InstrumentationDeferredFieldParameters; |
7 | 12 | import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters; |
| 13 | +import graphql.schema.GraphQLFieldDefinition; |
| 14 | +import graphql.schema.GraphQLObjectType; |
8 | 15 |
|
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.LinkedHashMap; |
9 | 18 | import java.util.List; |
| 19 | +import java.util.Map; |
10 | 20 | import java.util.concurrent.CompletableFuture; |
11 | 21 | import java.util.function.BiConsumer; |
| 22 | +import java.util.function.Supplier; |
| 23 | +import java.util.stream.Collectors; |
12 | 24 |
|
| 25 | +import static graphql.execution.MergedSelectionSet.newMergedSelectionSet; |
13 | 26 |
|
14 | 27 | /** |
15 | 28 | * The standard graphql execution strategy that runs fields asynchronously non-blocking. |
@@ -52,7 +65,14 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont |
52 | 65 | ExecutionStrategyParameters newParameters = parameters |
53 | 66 | .transform(builder -> builder.field(currentField).path(fieldPath).parent(parameters)); |
54 | 67 |
|
55 | | - CompletableFuture<FieldValueInfo> future = resolveFieldWithInfo(executionContext, newParameters); |
| 68 | + CompletableFuture<FieldValueInfo> future; |
| 69 | + |
| 70 | + if (isDeferred(executionContext, newParameters, currentField)) { |
| 71 | + executionStrategyCtx.onDeferredField(currentField); |
| 72 | + future = resolveFieldWithInfoToNull(executionContext, newParameters); |
| 73 | + } else { |
| 74 | + future = resolveFieldWithInfo(executionContext, newParameters); |
| 75 | + } |
56 | 76 | futures.add(future); |
57 | 77 | } |
58 | 78 | CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>(); |
@@ -83,4 +103,57 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont |
83 | 103 | overallResult.whenComplete(executionStrategyCtx::onCompleted); |
84 | 104 | return overallResult; |
85 | 105 | } |
| 106 | + |
| 107 | + private boolean isDeferred(ExecutionContext executionContext, ExecutionStrategyParameters parameters, MergedField currentField) { |
| 108 | + DeferSupport deferSupport = executionContext.getDeferSupport(); |
| 109 | + if (deferSupport.checkForDeferDirective(currentField, executionContext.getVariables())) { |
| 110 | + DeferredErrorSupport errorSupport = new DeferredErrorSupport(); |
| 111 | + |
| 112 | + // with a deferred field we are really resetting where we execute from, that is from this current field onwards |
| 113 | + Map<String, MergedField> fields = new LinkedHashMap<>(); |
| 114 | + fields.put(currentField.getName(), currentField); |
| 115 | + |
| 116 | + ExecutionStrategyParameters callParameters = parameters.transform(builder -> |
| 117 | + { |
| 118 | + MergedSelectionSet mergedSelectionSet = newMergedSelectionSet().subFields(fields).build(); |
| 119 | + builder.deferredErrorSupport(errorSupport) |
| 120 | + .field(currentField) |
| 121 | + .fields(mergedSelectionSet) |
| 122 | + .parent(null) // this is a break in the parent -> child chain - its a new start effectively |
| 123 | + .listSize(0) |
| 124 | + .currentListIndex(0); |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + DeferredCall call = new DeferredCall(parameters.getPath(), deferredExecutionResult(executionContext, callParameters), errorSupport); |
| 129 | + deferSupport.enqueue(call); |
| 130 | + return true; |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + @SuppressWarnings("FutureReturnValueIgnored") |
| 136 | + private Supplier<CompletableFuture<ExecutionResult>> deferredExecutionResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters) { |
| 137 | + return () -> { |
| 138 | + GraphQLFieldDefinition fieldDef = getFieldDef(executionContext, parameters, parameters.getField().getSingleField()); |
| 139 | + GraphQLObjectType fieldContainer = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); |
| 140 | + |
| 141 | + Instrumentation instrumentation = executionContext.getInstrumentation(); |
| 142 | + DeferredFieldInstrumentationContext fieldCtx = instrumentation.beginDeferredField( |
| 143 | + new InstrumentationDeferredFieldParameters(executionContext, parameters, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, fieldContainer)) |
| 144 | + ); |
| 145 | + CompletableFuture<ExecutionResult> result = new CompletableFuture<>(); |
| 146 | + fieldCtx.onDispatched(result); |
| 147 | + CompletableFuture<FieldValueInfo> fieldValueInfoFuture = resolveFieldWithInfo(executionContext, parameters); |
| 148 | + |
| 149 | + fieldValueInfoFuture.whenComplete((fieldValueInfo, throwable) -> { |
| 150 | + fieldCtx.onFieldValueInfo(fieldValueInfo); |
| 151 | + |
| 152 | + CompletableFuture<ExecutionResult> execResultFuture = fieldValueInfo.getFieldValue(); |
| 153 | + execResultFuture = execResultFuture.whenComplete(fieldCtx::onCompleted); |
| 154 | + Async.copyResults(execResultFuture, result); |
| 155 | + }); |
| 156 | + return result; |
| 157 | + }; |
| 158 | + } |
86 | 159 | } |
0 commit comments