Feature-dependent fields
Request optional fields only when a client feature or display mode needs them.
GraphQL · Directive
@include conditionally includes a field or fragment when its required if argument evaluates to true. Use it to let runtime variables control whether optional selections are requested.
@include conditionally selects a field, fragment spread, or inline fragment during query execution. Its required Boolean argument controls inclusion: the selection is included when the argument evaluates to true and omitted otherwise.
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT| Name | Type | Description |
|---|---|---|
if | Boolean! | Controls whether the annotated selection is included; it must evaluate to true for inclusion. |
No standalone value; it controls whether the annotated field or fragment selection contributes to the query result.
query GetUser($showDetails: Boolean!) {
user {
id
name
...UserDetails @include(if: $showDetails)
}
}
fragment UserDetails on User {
email
createdAt
}The fragment is included in the response only when showDetails is true.
Request optional fields only when a client feature or display mode needs them.
Enable or omit a reusable group of selections with one runtime Boolean.
Choose whether type-specific selections are requested in an inline fragment.
@include conditionally includes a field or fragment based on its if argument. The selection is executed only when that argument evaluates to true.
Follow-up: Where can it be applied? It is valid on fields, fragment spreads, and inline fragments.
The directive takes a required Boolean argument: @include(if: Boolean!). A variable is commonly used so the client can choose the selection at runtime.
Follow-up: Why is the exclamation mark significant? It makes the argument non-null, so the operation must provide a Boolean value rather than null.
With both directives, the selection is included only when @skip is false and @include is true. Neither directive overrides the other.
Follow-up: What happens if @skip is true? The selection is omitted even when @include is true.
@skipfieldsfragment spreadsinline fragmentsdirectivesvariables