Skip to content

Commit

Permalink
Fix handling of root query operation not named Query (#2459)
Browse files Browse the repository at this point in the history
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`.

**Checklist**

Complete the checklist (and note appropriate exceptions) before a final
PR is raised.

- [x] Changes are compatible[^1]
- [x] Documentation[^2] completed
- [x] Performance impact assessed and acceptable
- Tests added and passing[^3]
    - [x] Unit Tests
    - [ ] Integration Tests
    - [ ] Manual Tests

**Exceptions**

*Note any exceptions here*

**Notes**

[^1]. It may be appropriate to bring upcoming changes to the attention
of other (impacted) groups. Please endeavour to do this before seeking
PR approval. The mechanism for doing this will vary considerably, so use
your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where
applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding
Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or
ask for it to be labeled) as `manual test`
  • Loading branch information
SimonSapin authored Jan 23, 2023
1 parent b80d6da commit 0a90e32
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 @@ -38,4 +38,12 @@ 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
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 0a90e32

Please sign in to comment.