How can I make a custom query directive in Apollo

283 Views Asked by At

The GraphQL specification allows us to add custom query directives. I'm looking for some information on how can I do this. I want to create a @include like a directive, but with more elaborate logic.

Apollo guide describes only adding scheme (i.e. working on the server) directive.

1

There are 1 best solutions below

1
Ankit On

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 a value true.

GraphQL Query

query ($includeAuthor: Boolean!) {
    queryPost {
    id
    title
    text
    author @include(if: $includeAuthor) {
        id
        name
    }
  }
}

GraphQL variables

{
   "includeAuthor": false
}