Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ the `Accept` header means `*/*` when it is absent.

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

### Missing `@skip` and `@include` implementation for root operations ([Issue #2072](https://github.com/apollographql/router/issues/2072))

`@skip` and `@include` were not implemented for inline fragments and fragment spreads on top level operations.

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

## 🛠 Maintenance
## 📚 Documentation

Expand Down
37 changes: 35 additions & 2 deletions apollo-router/src/spec/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,8 @@ impl Query {
Selection::InlineFragment {
type_condition,
selection_set,
skip,
include,
..
} => {
// top level objects will not provide a __typename field
Expand All @@ -744,6 +746,14 @@ impl Query {
return Err(InvalidValue);
}

if skip.should_skip(parameters.variables).unwrap_or(false) {
continue;
}

if !include.should_include(parameters.variables).unwrap_or(true) {
continue;
}

self.apply_selection_set(
selection_set,
parameters,
Expand All @@ -756,10 +766,33 @@ impl Query {
Selection::FragmentSpread {
name,
known_type: _,
skip: _,
include: _,
skip,
include,
} => {
if skip.should_skip(parameters.variables).unwrap_or(false) {
continue;
}

if !include.should_include(parameters.variables).unwrap_or(true) {
continue;
}

if let Some(fragment) = self.fragments.get(name) {
if fragment
.skip
.should_skip(parameters.variables)
.unwrap_or(false)
{
continue;
}
if !fragment
.include
.should_include(parameters.variables)
.unwrap_or(true)
{
continue;
}

let operation_type_name =
parameters.schema.root_operation_name(operation.kind);
let is_apply = {
Expand Down
114 changes: 114 additions & 0 deletions apollo-router/src/spec/query/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4102,6 +4102,120 @@ fn include() {
},
}})
.test();

FormatTest::builder()
.schema(schema)
.query(
"query Example($shouldInclude: Boolean) {
get {
name
}
...test @include(if: $shouldInclude)
}

fragment test on Query {
get {
id
}
}",
)
.response(json! {{
"get": {
"name": "a",
},
}})
.operation("Example")
.variables(json! {{
"shouldInclude": false
}})
.expected(json! {{
"get": {
"name": "a",
},
}})
.test();

FormatTest::builder()
.schema(schema)
.query(
"query Example($shouldInclude: Boolean) {
get {
name
}
...test @include(if: $shouldInclude)
}

fragment test on Query {
get {
id
}
}",
)
.response(json! {{
"get": {
"name": "a",
},
}})
.operation("Example")
.expected(json! {{
"get": null,
}})
.test();

FormatTest::builder()
.schema(schema)
.query(
"query Example($shouldInclude: Boolean) {
get {
name
}
... @include(if: $shouldInclude) {
get {
id
}
}
}",
)
.response(json! {{
"get": {
"name": "a",
},
}})
.operation("Example")
.variables(json! {{
"shouldInclude": false
}})
.expected(json! {{
"get": {
"name": "a",
},
}})
.test();

FormatTest::builder()
.schema(schema)
.query(
"query Example($shouldInclude: Boolean) {
get {
name
}
... @include(if: $shouldInclude) {
get {
id
}
}
}",
)
.response(json! {{
"get": {
"name": "a",
},
}})
.operation("Example")
.expected(json! {{
"get": null,
}})
.test();
}

#[test]
Expand Down