Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Query Planner constructor and some strictness improvements #175

Merged
merged 28 commits into from
Mar 6, 2024

Conversation

goto-bus-stop
Copy link
Member

@goto-bus-stop goto-bus-stop commented Feb 13, 2024

This implements the constructor QueryPlanner::new(), which builds query graphs and does some minor precomputation, and:

  • fixes a few issues in subgraph extraction, were easy to miss since that code was not used before. also removed an old module that is obsolete.
  • introduce some use of ValidFederationSchema vs. FederationSchema in the API schema implementation, so there is less potential for misuse (this requires more cloning than i'd like--i'm not convinced that ValidFederationSchema being an Arc internally is the best thing to do, but understand the usefulness of its == implementation!)
  • move @inaccessible related functions onto the spec definition struct: this means you have to look up the inaccessible spec before using it, manually or from the schema, so there's less potential for misuse

Closes FED-22
Closes FED-82

For QP work, the API schema result needs to be a `FederationSchema`. We
also know that the API schema result is valid, so we make it a
`ValidFederationSchema`. API schema must also only be called on valid
schemas so the input is changed to a `ValidFederationSchema`.

Supergraphs are also federation schemas so this changes the constructors
to build a `ValidFederationSchema` early.

`ValidFederationSchema` contains an `Arc` while `FederationSchema`
doesn't, and that is relied upon in a few places. I add some clones of
the inner schema to convert from an immutable `ValidFederationSchema` to
a mutable `FederationSchema`, it maybe could be done more efficiently,
but it's OK.
The actual implementation of subgraph extraction is in the `query_graph`
module.
@goto-bus-stop goto-bus-stop marked this pull request as ready for review February 20, 2024 11:00
@goto-bus-stop
Copy link
Member Author

Most of the existing tests on the JS side are checking behaviours in Fed 1 supergraphs. I don't think I can port those over yet as Fed 1 supergraphs are out of scope. I will add some notes/questions to this PR about the differences between JS and Rust data structures

paths_limit: None,
}
}
}

pub struct QueryPlanner {
config: Arc<QueryPlannerConfig>,
// TODO(@goto-bus-stop) not convinced that these arcs are all necessary
config: QueryPlannerConfig,
federated_query_graph: Arc<QueryGraph>,
supergraph_schema: ValidFederationSchema,
api_schema: ValidFederationSchema,
subgraph_federation_spec_definitions: Arc<IndexMap<NodeStr, &'static FederationSpecDefinition>>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find this in the JS implementation. I do understand what it needs to do because of the type. But how is it going to be used?

@SimonSapin SimonSapin self-requested a review February 20, 2024 16:24
src/lib.rs Outdated

pub struct Supergraph {
pub schema: Valid<Schema>,
pub schema: ValidFederationSchema,
}

impl Supergraph {
pub fn new(schema_str: &str) -> Result<Self, FederationError> {
let schema = Schema::parse_and_validate(schema_str, "schema.graphql")?;
// TODO: federation-specific validation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this TODO still relevant?

src/lib.rs Outdated
@@ -141,7 +151,7 @@ mod tests {
"#;

let supergraph = Supergraph::new(schema).unwrap();
let _subgraphs = database::extract_subgraphs(&supergraph)
let _subgraphs = crate::query_graph::extract_subgraphs_from_supergraph::extract_subgraphs_from_supergraph(&supergraph.schema, None)
.expect("Should have been able to extract subgraphs");
// TODO: actual assertions on the subgraph once it's actually implemented.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it time to add assertions there? Does JS have corresponding tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT this test is not directly in JS, but we could add some that just assert the whole subgraph shape is as expected here.

I think this must only compare the fields that are selected, not the
parent type and the schema it's a part of. That's because the selections
are parsed in the context of a subgraph schema, so writing the same
`@key()` in subgraph A and B results in selection sets that don't
compare equal, but their actual selections do. And that is what key
resolution cares about.
src/query_plan/query_planner.rs Outdated Show resolved Hide resolved
src/query_plan/query_planner.rs Outdated Show resolved Hide resolved
@goto-bus-stop goto-bus-stop changed the title Query Planner constructor, closes #156 Query Planner constructor and some strictness improvements Mar 6, 2024
@goto-bus-stop goto-bus-stop merged commit 7e97e24 into main Mar 6, 2024
7 checks passed
@goto-bus-stop goto-bus-stop deleted the renee/qp-constructor branch March 6, 2024 09:16
SimonSapin pushed a commit to apollographql/router that referenced this pull request May 3, 2024
…phql/federation-next#175)

This implements the constructor `QueryPlanner::new()`, which builds
query graphs and does some minor precomputation, and:
- fixes a few issues in subgraph extraction, were easy to miss since
that code was not used before. also removed an old module that is
obsolete.
- introduce some use of `ValidFederationSchema` vs. `FederationSchema`
in the API schema implementation, so there is less potential for misuse
(this requires more cloning than i'd like--i'm not convinced that
ValidFederationSchema being an Arc internally is the best thing to do,
but understand the usefulness of its `==` implementation!)
- move `@inaccessible` related functions onto the spec definition
struct: this means you have to look up the inaccessible spec before
using it, manually or from the schema, so there's less potential for
misuse

Closes FED-22
Closes FED-82
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants