Skip to content

Commit

Permalink
implemented basic correctness check of query plans
Browse files Browse the repository at this point in the history
- comparison of two response shapes
- query plans analysis, deriving plan's overall response shape
  • Loading branch information
duckki committed Jan 11, 2025
1 parent bcb23f4 commit ba4308c
Show file tree
Hide file tree
Showing 8 changed files with 1,649 additions and 214 deletions.
4 changes: 3 additions & 1 deletion apollo-federation/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ fn cmd_plan(
ExecutableDocument::parse_and_validate(planner.api_schema().schema(), query, query_path)?;
let query_plan = planner.build_query_plan(&query_doc, None, Default::default())?;
println!("{query_plan}");
// Use the supergraph schema, not the API schema, for correctness check,
// since the plan may access hidden fields.
apollo_federation::query_plan::correctness::check_plan(
planner.api_schema(),
&supergraph.schema,
&query_doc,
&query_plan,
)?;
Expand Down
20 changes: 16 additions & 4 deletions apollo-federation/src/query_plan/correctness/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
pub mod query_plan_analysis;
#[cfg(test)]
pub mod query_plan_analysis_test;
pub mod response_shape;
pub mod response_shape_compare;
#[cfg(test)]
pub mod response_shape_test;

use apollo_compiler::validation::Valid;
use apollo_compiler::ExecutableDocument;

use crate::internal_error;
use crate::query_plan::QueryPlan;
use crate::schema::ValidFederationSchema;
use crate::FederationError;
Expand All @@ -15,9 +20,16 @@ use crate::FederationError;
pub fn check_plan(
schema: &ValidFederationSchema,
operation_doc: &Valid<ExecutableDocument>,
_plan: &QueryPlan,
plan: &QueryPlan,
) -> Result<(), FederationError> {
let rs = response_shape::compute_response_shape(operation_doc, schema)?;
println!("\nResponse shape from operation:\n{rs}");
Ok(())
let op_rs = response_shape::compute_response_shape_for_operation(operation_doc, schema)?;

let root_type = response_shape::compute_the_root_type_condition_for_operation(operation_doc)?;
let plan_rs = query_plan_analysis::interpret_query_plan(schema, &root_type, plan)?;

response_shape_compare::compare_response_shapes(&op_rs, &plan_rs)
.map_err(|e| internal_error!(
"Response shape from query plan does not match response shape from input operation:\n{}",
e.description()
))
}
Loading

0 comments on commit ba4308c

Please sign in to comment.