Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
GraphQL Queries  ›  Capstone Project

Directives

Directives 12 minDirectives such as @include and @skip attach to fields or fragments and, usually driven by a Boolean variable, decide whether that part of the query runs, while schema directives such as @deprecated describe the API itself.

Directives

The book page has a "Show reviews" toggle. When it is off, fetching reviews is wasted work. You could keep two nearly identical queries, one with reviews and one without, but a directive lets one query switch its own parts on and off.

The rule

@include keeps when true; @skip drops when true.

A directive is an @name with optional arguments, attached to part of a document to change how it is handled. Every GraphQL server supports two in queries:

  • @include(if: Boolean!): the field or fragment is kept only when if is true.
  • @skip(if: Boolean!): the field or fragment is dropped when if is true.

They are almost always fed by a variable:

query BookPage($id: ID!, $withReviews: Boolean!) {
  book(id: $id) {
    title
    price
    reviews(first: 5) @include(if: $withReviews) {
      rating
      text
    }
  }
}
{ "id": "b1", "withReviews": false }

With withReviews false, the server never runs the reviews resolver, and the response has no reviews key at all: not null, not an empty list, simply absent.

Directives can sit on a field, a fragment spread or an inline fragment:

...ReviewLine @skip(if: $compact)

If a field carries both, it is kept only when @skip is false and @include is true.

Directives also appear in schemas, where they describe the API rather than a request. The best-known is @deprecated:

type Book {
  isbn: String @deprecated(reason: "Use isbn13.")
  isbn13: String
}

Servers and tools can define their own directives too, but @include, @skip and @deprecated are the ones every GraphQL user meets.

The shortcut

To predict a field, evaluate the condition, then read the name: include + true = kept; skip + true = gone.

Rendering the toggle statesStep 1 of 4
  1. Toggle off

    $withReviews is false. @include(if: false) drops reviews, so the response has only title and price.

The trap is expecting null for a dropped field. A skipped field is missing from the response, so frontend code should check for the key, not for null.

Drills

Answer fast — trust the rule.

quick drill

With $withReviews false, what does reviews @include(if: $withReviews) { rating } produce?

quick drill

Which directive drops a field when its condition is true?

quick drill

A field has @skip(if: false) @include(if: false). Is it in the response?

quick drill

Where does @deprecated normally appear?

Remember

  • @include keeps when true; @skip drops when true.
  • Evaluate the condition, then read the name; with both, skip must be false and include true.
  • Trap: a dropped field is missing, not null.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA compact mode for the book card

The book card has a compact mode that hides the author's name. Write one query that fetches a book's title always and its author's name only when not compact, using a variable. Explain which directive you chose, what the response looks like in compact mode, and why one query beats two.