@skip and @include Directives
@skip
and @include
directives can be applied to query fields.
They allow you to skip or include a field based on the value of the if
argument
that is passed to the directive.
@skip
In the query below, we fetch posts and decide whether to fetch the title for them or not
based on the skipTitle
GraphQL variable.
GraphQL query
query ($skipTitle: Boolean!) {
queryPost {
id
title @skip(if: $skipTitle)
text
}
}
GraphQL variables
{
"skipTitle": true
}
@include
Similarly, the @include
directive can be used to include a field based on the value of
the if
argument. The query below would only include the authors for a post if includeAuthor
GraphQL variable has value true.
GraphQL Query
query ($includeAuthor: Boolean!) {
queryPost {
id
title
text
author @include(if: $includeAuthor) {
id
name
}
}
}
GraphQL variables
{
"includeAuthor": false
}