Skip to content

Commit d7329a9

Browse files
authored
fix(dual-query-planner): use semantic diff when comparing Defer nodes (#6110)
1 parent 3b3d823 commit d7329a9

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

apollo-router/src/query_planner/dual_query_planner.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,20 @@ fn operation_matches(
300300
this: &SubgraphOperation,
301301
other: &SubgraphOperation,
302302
) -> Result<(), MatchFailure> {
303-
let this_ast = match ast::Document::parse(this.as_serialized(), "this_operation.graphql") {
303+
document_str_matches(this.as_serialized(), other.as_serialized())
304+
}
305+
306+
// Compare operation document strings such as query or just selection set.
307+
fn document_str_matches(this: &str, other: &str) -> Result<(), MatchFailure> {
308+
let this_ast = match ast::Document::parse(this, "this_operation.graphql") {
304309
Ok(document) => document,
305310
Err(_) => {
306311
return Err(MatchFailure::new(
307312
"Failed to parse this operation".to_string(),
308313
));
309314
}
310315
};
311-
let other_ast = match ast::Document::parse(other.as_serialized(), "other_operation.graphql") {
316+
let other_ast = match ast::Document::parse(other, "other_operation.graphql") {
312317
Ok(document) => document,
313318
Err(_) => {
314319
return Err(MatchFailure::new(
@@ -319,6 +324,20 @@ fn operation_matches(
319324
same_ast_document(&this_ast, &other_ast)
320325
}
321326

327+
fn opt_document_string_matches(
328+
this: &Option<String>,
329+
other: &Option<String>,
330+
) -> Result<(), MatchFailure> {
331+
match (this, other) {
332+
(None, None) => Ok(()),
333+
(Some(this_sel), Some(other_sel)) => document_str_matches(this_sel, other_sel),
334+
_ => Err(MatchFailure::new(format!(
335+
"mismatched at opt_document_string_matches\nleft: {:?}\nright: {:?}",
336+
this, other
337+
))),
338+
}
339+
}
340+
322341
// The rest is calling the comparison functions above instead of `PartialEq`,
323342
// but otherwise behave just like `PartialEq`:
324343

@@ -494,8 +513,8 @@ fn plan_node_matches(this: &PlanNode, other: &PlanNode) -> Result<(), MatchFailu
494513
deferred: other_deferred,
495514
},
496515
) => {
497-
check_match!(defer_primary_node_matches(primary, other_primary));
498-
check_match!(vec_matches(deferred, other_deferred, deferred_node_matches));
516+
defer_primary_node_matches(primary, other_primary)?;
517+
vec_matches_result(deferred, other_deferred, deferred_node_matches)?;
499518
}
500519
(
501520
PlanNode::Subscription { primary, rest },
@@ -536,24 +555,30 @@ fn plan_node_matches(this: &PlanNode, other: &PlanNode) -> Result<(), MatchFailu
536555
Ok(())
537556
}
538557

539-
fn defer_primary_node_matches(this: &Primary, other: &Primary) -> bool {
558+
fn defer_primary_node_matches(this: &Primary, other: &Primary) -> Result<(), MatchFailure> {
540559
let Primary { subselection, node } = this;
541-
*subselection == other.subselection && opt_plan_node_matches(node, &other.node).is_ok()
560+
opt_document_string_matches(subselection, &other.subselection)
561+
.map_err(|err| err.add_description("under defer primary subselection"))?;
562+
opt_plan_node_matches(node, &other.node)
563+
.map_err(|err| err.add_description("under defer primary plan node"))
542564
}
543565

544-
fn deferred_node_matches(this: &DeferredNode, other: &DeferredNode) -> bool {
566+
fn deferred_node_matches(this: &DeferredNode, other: &DeferredNode) -> Result<(), MatchFailure> {
545567
let DeferredNode {
546568
depends,
547569
label,
548570
query_path,
549571
subselection,
550572
node,
551573
} = this;
552-
*depends == other.depends
553-
&& *label == other.label
554-
&& *query_path == other.query_path
555-
&& *subselection == other.subselection
556-
&& opt_plan_node_matches(node, &other.node).is_ok()
574+
575+
check_match_eq!(*depends, other.depends);
576+
check_match_eq!(*label, other.label);
577+
check_match_eq!(*query_path, other.query_path);
578+
opt_document_string_matches(subselection, &other.subselection)
579+
.map_err(|err| err.add_description("under deferred subselection"))?;
580+
opt_plan_node_matches(node, &other.node)
581+
.map_err(|err| err.add_description("under deferred node"))
557582
}
558583

559584
fn flatten_node_matches(this: &FlattenNode, other: &FlattenNode) -> Result<(), MatchFailure> {

0 commit comments

Comments
 (0)