Skip to content

Commit 33fcbc9

Browse files
committed
nextgen engine refactoring
1 parent a9d3776 commit 33fcbc9

2 files changed

Lines changed: 37 additions & 38 deletions

File tree

src/main/java/graphql/execution/nextgen/BatchedExecutionStrategy.java

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.concurrent.CompletableFuture;
23-
import java.util.stream.Collectors;
2423

25-
import static java.util.stream.Collectors.groupingBy;
26-
import static java.util.stream.Collectors.toList;
24+
import static graphql.util.FpKit.flatList;
25+
import static graphql.util.FpKit.map;
26+
import static graphql.util.FpKit.mapEntries;
27+
import static graphql.util.FpKit.transposeMatrix;
2728

2829
@Internal
2930
public class BatchedExecutionStrategy implements ExecutionStrategy {
@@ -37,7 +38,8 @@ public class BatchedExecutionStrategy implements ExecutionStrategy {
3738

3839

3940
public CompletableFuture<RootExecutionResultNode> execute(ExecutionContext executionContext, FieldSubSelection fieldSubSelection) {
40-
CompletableFuture<RootExecutionResultNode> rootCF = Async.each(util.fetchSubSelection(executionContext, fieldSubSelection)).thenApply(RootExecutionResultNode::new);
41+
CompletableFuture<RootExecutionResultNode> rootCF = Async.each(util.fetchSubSelection(executionContext, fieldSubSelection))
42+
.thenApply(RootExecutionResultNode::new);
4143

4244
return rootCF
4345
.thenCompose(rootNode -> {
@@ -66,55 +68,42 @@ private CompletableFuture<ExecutionResultMultiZipper> nextStepImpl(ExecutionCont
6668
CompletableFuture<List<List<ExecutionResultZipper>>> listListCF = Async.flatMap(unresolvedNodes,
6769
executionResultMultiZipper -> fetchAndAnalyze(executionContext, executionResultMultiZipper.getZippers()));
6870

69-
return FpKit.flatList(listListCF)
71+
return flatList(listListCF)
7072
.thenApply(zippers -> new ExecutionResultMultiZipper(commonRoot, zippers));
7173

7274
}
7375

7476
private List<ExecutionResultMultiZipper> groupNodesIntoBatches(ExecutionResultMultiZipper unresolvedZipper) {
75-
Map<Map<String, MergedField>, List<ExecutionResultZipper>> zipperBySubSelection = unresolvedZipper.getZippers().stream()
76-
.collect(groupingBy(executionResultZipper -> executionResultZipper.getCurNode().getFetchedValueAnalysis().getFieldSubSelection().getSubFields()));
77-
78-
return zipperBySubSelection
79-
.entrySet()
80-
.stream()
81-
.map(entry -> new ExecutionResultMultiZipper(unresolvedZipper.getCommonRoot(), entry.getValue()))
82-
.collect(Collectors.toList());
77+
Map<Map<String, MergedField>, List<ExecutionResultZipper>> zipperBySubSelection = FpKit.groupingBy(unresolvedZipper.getZippers(),
78+
(executionResultZipper -> executionResultZipper.getCurNode().getFetchedValueAnalysis().getFieldSubSelection().getSubFields()));
79+
80+
return mapEntries(zipperBySubSelection, (key, value) -> new ExecutionResultMultiZipper(unresolvedZipper.getCommonRoot(), value));
8381
}
8482

8583
//constrain: all fieldSubSelections have the same fields
8684
private CompletableFuture<List<ExecutionResultZipper>> fetchAndAnalyze(ExecutionContext executionContext, List<ExecutionResultZipper> unresolvedNodes) {
8785
Assert.assertTrue(unresolvedNodes.size() > 0, "unresolvedNodes can't be empty");
8886

89-
List<FieldSubSelection> fieldSubSelections = unresolvedNodes.stream()
90-
.map(zipper -> zipper.getCurNode().getFetchedValueAnalysis().getFieldSubSelection())
91-
.collect(Collectors.toList());
92-
List<Object> sources = fieldSubSelections.stream().map(fieldSubSelection -> fieldSubSelection.getSource()).collect(Collectors.toList());
87+
List<FieldSubSelection> fieldSubSelections = map(unresolvedNodes, zipper -> zipper.getCurNode().getFetchedValueAnalysis().getFieldSubSelection());
88+
List<Object> sources = map(fieldSubSelections, FieldSubSelection::getSource);
9389

9490
// each field in the subSelection has n sources as input
95-
List<CompletableFuture<List<FetchedValueAnalysis>>> fetchedValues = fieldSubSelections
96-
.get(0)
97-
.getSubFields()
98-
.entrySet()
99-
.stream()
100-
.map(entry -> {
101-
MergedField sameFields = entry.getValue();
102-
String name = entry.getKey();
103-
104-
List<ExecutionStepInfo> newExecutionStepInfos = fieldSubSelections.stream().map(executionResultNode -> {
105-
return executionInfoFactory.newExecutionStepInfoForSubField(executionContext, sameFields, executionResultNode.getExecutionStepInfo());
106-
}).collect(Collectors.toList());
107-
108-
CompletableFuture<List<FetchedValueAnalysis>> fetchedValueAnalyzis = valueFetcher
109-
.fetchBatchedValues(executionContext, sources, sameFields, newExecutionStepInfos)
110-
.thenApply(fetchValue -> analyseValues(executionContext, fetchValue, name, sameFields, newExecutionStepInfos));
111-
return fetchedValueAnalyzis;
112-
})
113-
.collect(toList());
91+
Map<String, MergedField> subFields = fieldSubSelections.get(0).getSubFields();
92+
93+
List<CompletableFuture<List<FetchedValueAnalysis>>> fetchedValues = mapEntries(subFields, (name, mergedField) -> {
94+
95+
List<ExecutionStepInfo> newExecutionStepInfos = map(fieldSubSelections,
96+
executionResultNode -> executionInfoFactory.newExecutionStepInfoForSubField(executionContext, mergedField, executionResultNode.getExecutionStepInfo()));
97+
98+
CompletableFuture<List<FetchedValueAnalysis>> fetchedValueAnalyzis = valueFetcher
99+
.fetchBatchedValues(executionContext, sources, mergedField, newExecutionStepInfos)
100+
.thenApply(fetchValue -> analyseValues(executionContext, fetchValue, name, mergedField, newExecutionStepInfos));
101+
return fetchedValueAnalyzis;
102+
});
114103

115104
return Async.each(fetchedValues).thenApply(fetchedValuesMatrix -> {
116105
List<ExecutionResultZipper> result = new ArrayList<>();
117-
List<List<FetchedValueAnalysis>> newChildsPerNode = FpKit.transposeMatrix(fetchedValuesMatrix);
106+
List<List<FetchedValueAnalysis>> newChildsPerNode = transposeMatrix(fetchedValuesMatrix);
118107

119108
for (int i = 0; i < newChildsPerNode.size(); i++) {
120109
ExecutionResultZipper unresolvedNodeZipper = unresolvedNodes.get(i);
@@ -134,7 +123,6 @@ private ExecutionResultZipper resolvedZipper(ExecutionResultZipper unresolvedNod
134123
}
135124

136125

137-
138126
private List<FetchedValueAnalysis> analyseValues(ExecutionContext executionContext, List<FetchedValue> fetchedValues, String name, MergedField field, List<ExecutionStepInfo> executionInfos) {
139127
List<FetchedValueAnalysis> result = new ArrayList<>();
140128
for (int i = 0; i < fetchedValues.size(); i++) {

src/main/java/graphql/util/FpKit.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import java.util.List;
1212
import java.util.Map;
1313
import java.util.concurrent.CompletableFuture;
14+
import java.util.function.BiFunction;
1415
import java.util.function.BinaryOperator;
1516
import java.util.function.Function;
1617
import java.util.stream.Collectors;
1718
import java.util.stream.IntStream;
1819

1920
import static java.util.Collections.singletonList;
2021
import static java.util.function.Function.identity;
22+
import static java.util.stream.Collectors.mapping;
2123

2224
@Internal
2325
public class FpKit {
@@ -33,6 +35,11 @@ public static <T> Map<String, T> getByName(List<T> namedObjects, Function<T, Str
3335
);
3436
}
3537

38+
// normal groupingBy but with LinkedHashMap
39+
public static <T, NewKey> Map<NewKey, List<T>> groupingBy(List<T> list, Function<T, NewKey> function) {
40+
return list.stream().collect(Collectors.groupingBy(function, LinkedHashMap::new, mapping(Function.identity(), Collectors.toList())));
41+
}
42+
3643
//
3744
// From a list of named things, get a map of them by name, merging them first one added
3845
public static <T> Map<String, T> getByName(List<T> namedObjects, Function<T, String> nameFn) {
@@ -113,6 +120,10 @@ public static <T, U> List<U> map(List<T> list, Function<T, U> function) {
113120
return list.stream().map(function).collect(Collectors.toList());
114121
}
115122

123+
public static <K, V, U> List<U> mapEntries(Map<K, V> map, BiFunction<K, V, U> function) {
124+
return map.entrySet().stream().map(entry -> function.apply(entry.getKey(), entry.getValue())).collect(Collectors.toList());
125+
}
126+
116127

117128
public static <T> List<List<T>> transposeMatrix(List<? extends List<T>> matrix) {
118129
int rowCount = matrix.size();

0 commit comments

Comments
 (0)