In the previous post, we have discussed the @skip directive.
Now, we will see @include
directive to allow conditional inclusion for fields, fragment spreads, and inline fragments to get appropriate results…
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
- Use @skip directive to exclude results during execution of query
- if argument sets the condition for exclusion
- @skip may be used for fields, fragment spreads, and inline fragments
- @skip should be used while executing query in GraphQL editor and not in the schema editor like other directives
We will work with the below schema.
type IncludeDirective {
str: String
int: Int
}
Insert few values in the str
and int
fields.
Sample Query
In the below example, the str
and int
field values will be retrieved only if the respective test variable (test1 and test2) is true. If the value is false in any of the variables,the corresponding field will not be included in the query execution.
query($test1: Boolean!,$test2: Boolean! ){
find(type: IncludeDirective, arcql: "*") {
edges {
node {
... on IncludeDirective {
str @include(if:$test1)
int @include(if:$test2)
}
}
cursor
}
}
}
#input
{
"test1": true,
"test2": false
}
#result
{
"data": {
"find": {
"edges": [
{
"node": {
"str": "easy to use"
},
"cursor": "01FPEH5FHQMGAS78JW3A179MMJ"
}
]
}
}
}