Skip to content
GraphQL

Directives

Conditionally include or skip fields.

By EZ4Code Team
directiveincludeskip

Code

query GetUser($id: ID!, $withPosts: Boolean!) {
  user(id: $id) {
    id
    name
    posts @include(if: $withPosts) {
      title
    }
    email @skip(if: true)
  }
}

# Custom directive declaration
directive @deprecated(reason: String) on FIELD_DEFINITION

type Query {
  oldField: String @deprecated(reason: "Use newField")
  newField: String
}

Explanation

Directives annotate fields or fragments with @name(args) to alter execution. The built-in @include and @skip directives conditionally include a field based on a Boolean variable. Custom directives declared with directive ... on ... are processed by the server or tooling, for example to mark deprecated fields.

More GraphQL Snippets