Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FED-332: added the remove_introspection step in normalize_operation #5580

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apollo-federation/src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4901,6 +4901,7 @@ pub(crate) fn normalize_operation(
schema,
NormalizeSelectionOption::NormalizeRecursively,
)?;
remove_introspection(&mut normalized_selection_set);
normalized_selection_set.optimize_sibling_typenames(interface_types_with_interface_objects)?;

let normalized_operation = Operation {
Expand All @@ -4915,6 +4916,21 @@ pub(crate) fn normalize_operation(
Ok(normalized_operation)
}

// PORT_NOTE: This is a port of `withoutIntrospection` from JS version.
fn remove_introspection(selection_set: &mut SelectionSet) {
// Note that, because we only apply this to the top-level selections, we skip all
// introspection, including __typename. In general, we don't want to ignore __typename during
// query plans, but at top-level, we can let the gateway execution deal with it rather than
// querying some service for that.

Arc::make_mut(&mut selection_set.selections).retain(|_, selection| {
!matches!(selection,
Selection::Field(field_selection) if
field_selection.field.data().field_position.is_introspection_typename_field()
)
});
}

fn runtime_types_intersect(
type1: &CompositeTypeDefinitionPosition,
type2: &CompositeTypeDefinitionPosition,
Expand Down
47 changes: 47 additions & 0 deletions apollo-federation/src/query_plan/query_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,4 +1313,51 @@ type User
}
"###);
}

#[test]
fn drop_operation_root_level_typename() {
let subgraph1 = Subgraph::parse_and_expand(
"Subgraph1",
"https://Subgraph1",
r#"
type Query {
t: T
}

type T @key(fields: "id") {
id: ID!
x: Int
}
"#,
)
.unwrap();
let subgraphs = vec![&subgraph1];
let supergraph = Supergraph::compose(subgraphs).unwrap();
let planner = QueryPlanner::new(&supergraph, Default::default()).unwrap();
let document = ExecutableDocument::parse_and_validate(
planner.api_schema().schema(),
r#"
query {
__typename
t {
x
}
}
"#,
"operation.graphql",
)
.unwrap();
let plan = planner.build_query_plan(&document, None).unwrap();
insta::assert_snapshot!(plan, @r###"
QueryPlan {
Fetch(service: "Subgraph1") {
{
t {
x
}
}
},
}
"###);
}
}
Loading