Skip to content

Commit

Permalink
do not nullify the entire query if the root operation is not present (#…
Browse files Browse the repository at this point in the history
…1674)

if the root operation was not returned by the subgraph (example: when
there's an error), we should not nullify the entire data objet. Instead,
it's the root operation field that should be null (unless it is not
nullable)

Co-authored-by: Martin Bonnin <[email protected]>
  • Loading branch information
Geal and martinbonnin authored Sep 1, 2022
1 parent 7e4b578 commit 6b83a74
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
9 changes: 9 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ The 2.1.1 release of the query planner fixes an issue with the `@defer` directiv

By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/1650 and https://github.com/apollographql/router/pull/1672

### Do not nullify the entire query if the root operation is not present ([PR #1674](https://github.com/apollographql/router/issues/1674))

if a root field was not returned by the subgraph (example: when
there's an error), we should not nullify the entire data objet. Instead,
it's the root field that should be null (unless it is non
nullable).

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

## 🛠 Maintenance

### Remove cache layer ([PR #1647](https://github.com/apollographql/router/pull/1647))
Expand Down
90 changes: 90 additions & 0 deletions apollo-router/src/spec/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ impl Query {
}
} else if field_type.is_non_null() {
return Err(InvalidValue);
} else {
output.insert(field_name.clone(), Value::Null);
}
}
Selection::InlineFragment {
Expand Down Expand Up @@ -5574,4 +5576,92 @@ mod tests {
}},
);
}

#[test]
fn query_operation_nullification() {
assert_format_response!(
"type Query {
get: Thing
}
type Thing {
name: String
}
",
"{
get {
name
}
}",
json! {{}},
None,
json! {{
"get": null,
}},
);

assert_format_response!(
"type Query {
get: Thing
}
type Thing {
name: String
}
",
"query {
...F
}
fragment F on Query {
get {
name
}
}
",
json! {{}},
None,
json! {{
"get": null,
}},
);

assert_format_response!(
"type Query {
get: Thing
}
type Thing {
name: String
}
",
"query {
... on Query {
get {
name
}
}
}",
json! {{}},
None,
json! {{
"get": null,
}},
);

assert_format_response!(
"type Query {
get: Thing!
}
type Thing {
name: String
}
",
"{
get {
name
}
}",
json! {{}},
None,
Value::Null,
);
}
}

0 comments on commit 6b83a74

Please sign in to comment.