Skip to content

Commit

Permalink
Fix handling of root query operation not named Query
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jan 23, 2023
1 parent 214b00d commit 83168c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
10 changes: 9 additions & 1 deletion NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,15 @@ is given the the error reporting library `miette`.
Instead, the Router now parses introspection types separately
and “concatenates” definitions at the AST level.

By [@SimonSapin](https://github.com/SimonSapin) in https://github.com/apollographql/router/issues/2448
By [@SimonSapin](https://github.com/SimonSapin) in https://github.com/apollographql/router/pull/2448

### Fix handling of root query operation not named `Query`

With such a schema, some parsing code in the Router would incorrectly
return an error because it was assuming the default name.
Similarly with a root mutation operation not named `Mutation`.

By [@SimonSapin](https://github.com/SimonSapin) in https://github.com/apollographql/router/pull/2459

## 🛠 Maintenance

Expand Down
9 changes: 4 additions & 5 deletions apollo-router/src/spec/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,10 @@ impl Operation {
})
.unwrap_or(OperationKind::Query);

let current_field_type = match kind {
OperationKind::Query => FieldType::Named("Query".to_string()),
OperationKind::Mutation => FieldType::Named("Mutation".to_string()),
OperationKind::Subscription => return Err(SpecError::SubscriptionNotSupported),
};
if kind == OperationKind::Subscription {
return Err(SpecError::SubscriptionNotSupported);
}
let current_field_type = FieldType::Named(schema.root_operation_name(kind).to_owned());

let selection_set = operation
.selection_set()
Expand Down
38 changes: 38 additions & 0 deletions apollo-router/src/spec/query/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5236,3 +5236,41 @@ fn test_error_path_works_across_inline_fragments() {
&Path::from("rootType/edges/0/node/subType/edges/0/node/myField")
));
}

#[test]
fn test_query_not_named_query() {
let config = Default::default();
let schema = Schema::parse(
r#"
schema
@core(feature: "https://specs.apollo.dev/core/v0.1")
@core(feature: "https://specs.apollo.dev/join/v0.1")
@core(feature: "https://specs.apollo.dev/inaccessible/v0.1")
{
query: TheOneAndOnlyQuery
}
directive @core(feature: String!) repeatable on SCHEMA
directive @join__graph(name: String!, url: String!) on ENUM_VALUE
directive @inaccessible on OBJECT | FIELD_DEFINITION | INTERFACE | UNION
enum join__Graph {
TEST @join__graph(name: "test", url: "http://localhost:4001/graphql")
}
type TheOneAndOnlyQuery { example: Boolean }
"#,
&config,
)
.unwrap();
let query = Query::parse("{ example }", &schema, &config).unwrap();
let selection = &query.operations[0].selection_set[0];
assert!(
matches!(
selection,
Selection::Field {
field_type: FieldType::Boolean,
..
}
),
"unexpected selection {selection:?}"
);
}

0 comments on commit 83168c6

Please sign in to comment.