@@ -7,7 +7,7 @@ use crate::graph::node::Node;
77use crate :: graph:: Graph ;
88use crate :: planner:: fetch:: fetch_step_data:: { FetchStepData , FetchStepKind } ;
99use crate :: planner:: fetch:: selections:: FetchStepSelections ;
10- use crate :: planner:: fetch:: state:: SingleTypeFetchStep ;
10+ use crate :: planner:: fetch:: state:: { MultiTypeFetchStep , SingleTypeFetchStep } ;
1111use crate :: planner:: plan_nodes:: { FetchNodePathSegment , FetchRewrite , ValueSetter } ;
1212use crate :: planner:: tree:: query_tree:: QueryTree ;
1313use crate :: planner:: tree:: query_tree_node:: { MutationFieldPosition , QueryTreeNode } ;
@@ -40,19 +40,48 @@ impl Default for FetchGraph<SingleTypeFetchStep> {
4040}
4141
4242impl FetchGraph < SingleTypeFetchStep > {
43+ pub fn to_multi_type ( self ) -> FetchGraph < MultiTypeFetchStep > {
44+ let new_graph = self
45+ . graph
46+ . map ( |_, w| w. clone ( ) . into_multi_type ( ) , |_, _| ( ) ) ;
47+
48+ FetchGraph {
49+ graph : new_graph,
50+ root_index : self . root_index ,
51+ }
52+ }
53+
4354 pub fn new ( ) -> Self {
4455 Self {
4556 graph : StableDiGraph :: new ( ) ,
4657 root_index : None ,
4758 }
4859 }
60+ }
4961
50- pub fn all_nodes ( & self ) -> NodeReferences < ' _ , FetchStepData < SingleTypeFetchStep > > {
62+ impl < State > FetchGraph < State > {
63+ pub fn all_nodes ( & self ) -> NodeReferences < ' _ , FetchStepData < State > > {
5164 self . graph . node_references ( )
5265 }
66+
67+ pub fn get_step_data_mut (
68+ & mut self ,
69+ index : NodeIndex ,
70+ ) -> Result < & mut FetchStepData < State > , FetchGraphError > {
71+ self . graph
72+ . node_weight_mut ( index)
73+ . ok_or ( FetchGraphError :: MissingStep (
74+ index. index ( ) ,
75+ String :: from ( "when getting mutable step data" ) ,
76+ ) )
77+ }
5378}
5479
55- impl FetchGraph < SingleTypeFetchStep > {
80+ impl < State > FetchGraph < State > {
81+ pub fn step_indices ( & self ) -> NodeIndices < FetchStepData < State > > {
82+ self . graph . node_indices ( )
83+ }
84+
5685 pub fn parents_of ( & self , index : NodeIndex ) -> petgraph:: stable_graph:: Edges < ' _ , ( ) , Directed > {
5786 self . graph . edges_directed ( index, Direction :: Incoming )
5887 }
@@ -65,14 +94,10 @@ impl FetchGraph<SingleTypeFetchStep> {
6594 has_path_connecting ( & self . graph , ancestor, descendant, None )
6695 }
6796
68- pub fn step_indices ( & self ) -> NodeIndices < FetchStepData < SingleTypeFetchStep > > {
69- self . graph . node_indices ( )
70- }
71-
7297 pub fn get_step_data (
7398 & self ,
7499 index : NodeIndex ,
75- ) -> Result < & FetchStepData < SingleTypeFetchStep > , FetchGraphError > {
100+ ) -> Result < & FetchStepData < State > , FetchGraphError > {
76101 self . graph
77102 . node_weight ( index)
78103 . ok_or ( FetchGraphError :: MissingStep (
@@ -81,51 +106,6 @@ impl FetchGraph<SingleTypeFetchStep> {
81106 ) )
82107 }
83108
84- pub fn get_step_data_mut (
85- & mut self ,
86- index : NodeIndex ,
87- ) -> Result < & mut FetchStepData < SingleTypeFetchStep > , FetchGraphError > {
88- self . graph
89- . node_weight_mut ( index)
90- . ok_or ( FetchGraphError :: MissingStep (
91- index. index ( ) ,
92- String :: from ( "when getting mutable step data" ) ,
93- ) )
94- }
95-
96- pub fn get_pair_of_steps_mut (
97- & mut self ,
98- index1 : NodeIndex ,
99- index2 : NodeIndex ,
100- ) -> Result <
101- (
102- & mut FetchStepData < SingleTypeFetchStep > ,
103- & mut FetchStepData < SingleTypeFetchStep > ,
104- ) ,
105- FetchGraphError ,
106- > {
107- // `index_twice_mut` panics when indexes are equal
108- if index1 == index2 {
109- return Err ( FetchGraphError :: SameNodeIndex ( index1. index ( ) ) ) ;
110- }
111-
112- // `index_twice_mut` panics when nodes do not exist
113- if self . graph . node_weight ( index1) . is_none ( ) {
114- return Err ( FetchGraphError :: MissingStep (
115- index1. index ( ) ,
116- String :: from ( "when checking existence" ) ,
117- ) ) ;
118- }
119- if self . graph . node_weight ( index2) . is_none ( ) {
120- return Err ( FetchGraphError :: MissingStep (
121- index2. index ( ) ,
122- String :: from ( "when checking existence" ) ,
123- ) ) ;
124- }
125-
126- Ok ( self . graph . index_twice_mut ( index1, index2) )
127- }
128-
129109 pub fn connect ( & mut self , parent_index : NodeIndex , child_index : NodeIndex ) -> EdgeIndex {
130110 self . graph . update_edge ( parent_index, child_index, ( ) )
131111 }
@@ -141,13 +121,13 @@ impl FetchGraph<SingleTypeFetchStep> {
141121 self . graph . remove_node ( index) . is_some_and ( |_| true )
142122 }
143123
144- pub fn add_step ( & mut self , data : FetchStepData < SingleTypeFetchStep > ) -> NodeIndex {
124+ pub fn add_step ( & mut self , data : FetchStepData < State > ) -> NodeIndex {
145125 self . graph . add_node ( data)
146126 }
147127
148128 pub fn bfs < F > ( & self , root_index : NodeIndex , mut visitor : F ) -> Option < NodeIndex >
149129 where
150- F : FnMut ( & NodeIndex , & FetchStepData < SingleTypeFetchStep > ) -> bool ,
130+ F : FnMut ( & NodeIndex , & FetchStepData < State > ) -> bool ,
151131 {
152132 self . graph . node_weight ( root_index) ?;
153133
@@ -167,7 +147,9 @@ impl FetchGraph<SingleTypeFetchStep> {
167147
168148 None
169149 }
150+ }
170151
152+ impl FetchGraph < MultiTypeFetchStep > {
171153 #[ instrument( level = "trace" , skip_all) ]
172154 pub fn collect_variable_usages ( & mut self ) -> Result < ( ) , FetchGraphError > {
173155 let nodes_idx = self . graph . node_indices ( ) . collect :: < Vec < _ > > ( ) ;
@@ -183,9 +165,42 @@ impl FetchGraph<SingleTypeFetchStep> {
183165
184166 Ok ( ( ) )
185167 }
168+
169+ pub fn get_pair_of_steps_mut (
170+ & mut self ,
171+ index1 : NodeIndex ,
172+ index2 : NodeIndex ,
173+ ) -> Result <
174+ (
175+ & mut FetchStepData < MultiTypeFetchStep > ,
176+ & mut FetchStepData < MultiTypeFetchStep > ,
177+ ) ,
178+ FetchGraphError ,
179+ > {
180+ // `index_twice_mut` panics when indexes are equal
181+ if index1 == index2 {
182+ return Err ( FetchGraphError :: SameNodeIndex ( index1. index ( ) ) ) ;
183+ }
184+
185+ // `index_twice_mut` panics when nodes do not exist
186+ if self . graph . node_weight ( index1) . is_none ( ) {
187+ return Err ( FetchGraphError :: MissingStep (
188+ index1. index ( ) ,
189+ String :: from ( "when checking existence" ) ,
190+ ) ) ;
191+ }
192+ if self . graph . node_weight ( index2) . is_none ( ) {
193+ return Err ( FetchGraphError :: MissingStep (
194+ index2. index ( ) ,
195+ String :: from ( "when checking existence" ) ,
196+ ) ) ;
197+ }
198+
199+ Ok ( self . graph . index_twice_mut ( index1, index2) )
200+ }
186201}
187202
188- impl Display for FetchGraph < SingleTypeFetchStep > {
203+ impl < State > Display for FetchGraph < State > {
189204 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
190205 writeln ! ( f, "Nodes:" ) ?;
191206 for node_index in self . graph . node_indices ( ) {
@@ -262,7 +277,7 @@ fn create_noop_fetch_step(
262277 variable_usages : None ,
263278 variable_definitions : None ,
264279 mutation_field_position : None ,
265- // internal_aliases_locations: Vec::new(),
280+ internal_aliases_locations : Vec :: new ( ) ,
266281 } )
267282}
268283
@@ -293,7 +308,7 @@ fn create_fetch_step_for_entity_call(
293308 variable_usages : None ,
294309 variable_definitions : None ,
295310 mutation_field_position : None ,
296- // internal_aliases_locations: Vec::new(),
311+ internal_aliases_locations : Vec :: new ( ) ,
297312 } )
298313}
299314
@@ -320,7 +335,7 @@ fn create_fetch_step_for_root_move(
320335 input_rewrites : None ,
321336 output_rewrites : None ,
322337 mutation_field_position,
323- // internal_aliases_locations: Vec::new(),
338+ internal_aliases_locations : Vec :: new ( ) ,
324339 } ) ;
325340
326341 fetch_graph. connect ( root_step_index, idx) ;
@@ -1574,7 +1589,7 @@ fn process_query_node(
15741589 }
15751590}
15761591
1577- pub fn find_graph_roots ( graph : & FetchGraph < SingleTypeFetchStep > ) -> Vec < NodeIndex > {
1592+ pub fn find_graph_roots < State > ( graph : & FetchGraph < State > ) -> Vec < NodeIndex > {
15781593 let mut roots = Vec :: new ( ) ;
15791594
15801595 // Iterate over all nodes in the graph
@@ -1598,7 +1613,7 @@ pub fn build_fetch_graph_from_query_tree(
15981613 supergraph : & SupergraphState ,
15991614 override_context : & PlannerOverrideContext ,
16001615 query_tree : QueryTree ,
1601- ) -> Result < FetchGraph < SingleTypeFetchStep > , FetchGraphError > {
1616+ ) -> Result < FetchGraph < MultiTypeFetchStep > , FetchGraphError > {
16021617 let mut fetch_graph = FetchGraph :: new ( ) ;
16031618
16041619 process_query_node (
@@ -1633,7 +1648,8 @@ pub fn build_fetch_graph_from_query_tree(
16331648
16341649 // fine to unwrap as we have already checked the length
16351650 fetch_graph. root_index = Some ( * root_indexes. first ( ) . unwrap ( ) ) ;
1636- // fetch_graph.optimize(supergraph)?;
1651+ let mut fetch_graph = fetch_graph. to_multi_type ( ) ;
1652+ fetch_graph. optimize ( supergraph) ?;
16371653 fetch_graph. collect_variable_usages ( ) ?;
16381654
16391655 trace ! ( "fetch graph after optimizations:" ) ;
0 commit comments