2020import java .util .List ;
2121import java .util .Map ;
2222import 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
2930public 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 ++) {
0 commit comments