diff --git a/apollo-federation/src/query_plan/fetch_dependency_graph.rs b/apollo-federation/src/query_plan/fetch_dependency_graph.rs index 1b6bedb55b..d1360120c7 100644 --- a/apollo-federation/src/query_plan/fetch_dependency_graph.rs +++ b/apollo-federation/src/query_plan/fetch_dependency_graph.rs @@ -729,6 +729,7 @@ impl FetchDependencyGraph { /// Adds another node as a parent of `child`, /// meaning that this fetch should happen after the provided one. + /// Assumption: The parent node is not a descendant of the child. fn add_parent(&mut self, child_id: NodeIndex, parent_relation: ParentRelation) { let ParentRelation { parent_node_id, @@ -738,8 +739,8 @@ impl FetchDependencyGraph { return; } assert!( - !self.graph.contains_edge(child_id, parent_node_id), - "Node {parent_node_id:?} is a child of {child_id:?}: \ + !self.is_descendant_of(parent_node_id, child_id), + "Node {parent_node_id:?} is a descendant of {child_id:?}: \ adding it as parent would create a cycle" ); self.on_modification(); @@ -794,15 +795,7 @@ impl FetchDependencyGraph { } fn is_descendant_of(&self, node_id: NodeIndex, maybe_ancestor_id: NodeIndex) -> bool { - if node_id == maybe_ancestor_id { - return true; - } - for child_id in self.children_of(node_id) { - if self.is_descendant_of(child_id, maybe_ancestor_id) { - return true; - } - } - false + petgraph::algo::has_path_connecting(&self.graph, maybe_ancestor_id, node_id, None) } /// Returns whether `node_id` is both a child of `maybe_parent_id` but also if we can show that the