Skip to content

Commit

Permalink
fix(federation): added the remove_introspection step in `normalize_…
Browse files Browse the repository at this point in the history
…operation` (#5580)

Co-authored-by: Renée <[email protected]>
  • Loading branch information
duckki and goto-bus-stop authored Jul 2, 2024
1 parent 27a3e1d commit 1a8e3b4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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 router 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.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
}
}
},
}
"###);
}
}

0 comments on commit 1a8e3b4

Please sign in to comment.