Repository navigation
Expand file tree
/
Copy pathAsyncSerialExecutionStrategy.java
More file actions
92 lines (77 loc) · 4.65 KB
/
Copy pathAsyncSerialExecutionStrategy.java
File metadata and controls
92 lines (77 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package graphql.execution;
import com.google.common.collect.ImmutableList;
import graphql.ExecutionResult;
import graphql.PublicApi;
import graphql.execution.instrumentation.Instrumentation;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters;
import graphql.introspection.Introspection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import static graphql.Assert.assertNotNull;
import static graphql.execution.instrumentation.SimpleInstrumentationContext.nonNullCtx;
/**
* Async non-blocking execution, but serial: only one field at the time will be resolved.
* See {@link AsyncExecutionStrategy} for a non-serial (parallel) execution of every field.
*/
@PublicApi
@NullMarked
public class AsyncSerialExecutionStrategy extends AbstractAsyncExecutionStrategy {
public AsyncSerialExecutionStrategy() {
super(new SimpleDataFetcherExceptionHandler());
}
public AsyncSerialExecutionStrategy(DataFetcherExceptionHandler exceptionHandler) {
super(exceptionHandler);
}
@Override
@SuppressWarnings({"TypeParameterUnusedInFormals", "FutureReturnValueIgnored"})
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
DataLoaderDispatchStrategy dataLoaderDispatcherStrategy = executionContext.getDataLoaderDispatcherStrategy();
Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
InstrumentationContext<ExecutionResult> executionStrategyCtx = nonNullCtx(instrumentation.beginExecutionStrategy(instrumentationParameters,
executionContext.getInstrumentationState())
);
MergedSelectionSet fields = parameters.getFields();
ImmutableList<String> fieldNames = ImmutableList.copyOf(fields.keySet());
// this is highly unlikely since Mutations cant do introspection BUT in theory someone could make the query strategy this code
// so belts and braces
Optional<ExecutionResult> isNotSensible = Introspection.isIntrospectionSensible(fields, executionContext);
if (isNotSensible.isPresent()) {
return CompletableFuture.completedFuture(isNotSensible.get());
}
CompletableFuture<List<Object>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, prevResults) -> {
MergedField currentField = assertNotNull(fields.getSubField(fieldName), "currentField must not be null");
ResultPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField));
ExecutionStrategyParameters newParameters = parameters.transform(currentField, fieldPath);
return resolveSerialField(executionContext, dataLoaderDispatcherStrategy, newParameters);
});
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
executionStrategyCtx.onDispatched();
resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult));
overallResult.whenComplete(executionStrategyCtx::onCompleted);
return overallResult;
}
private @Nullable Object resolveSerialField(ExecutionContext executionContext,
DataLoaderDispatchStrategy dataLoaderDispatcherStrategy,
ExecutionStrategyParameters newParameters) {
dataLoaderDispatcherStrategy.executionSerialStrategy(executionContext, newParameters);
Object fieldWithInfo = resolveFieldWithInfo(executionContext, newParameters);
dataLoaderDispatcherStrategy.finishedFetching(executionContext, newParameters);
if (fieldWithInfo instanceof CompletableFuture) {
//noinspection unchecked
return ((CompletableFuture<FieldValueInfo>) fieldWithInfo).thenCompose(fvi -> {
dataLoaderDispatcherStrategy.executionStrategyOnFieldValuesInfo(List.of(fvi), newParameters);
CompletableFuture<Object> fieldValueFuture = fvi.getFieldValueFuture();
return fieldValueFuture;
});
} else {
FieldValueInfo fvi = (FieldValueInfo) fieldWithInfo;
dataLoaderDispatcherStrategy.executionStrategyOnFieldValuesInfo(List.of(fvi), newParameters);
return fvi.getFieldValueObject();
}
}
}