Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
GraphQL Queries  ›  Lesson

Fragments

Fragments 14 minA fragment is a named selection set with a type condition that can be spread into any place selecting that type, so shared fields are defined once and every query that uses them stays in step.

Fragments

The bookshop shows a "book card" in three places: the Browse page, search results and an author's page. Each card needs the same title, price and author name. Copying those fields into three queries works until someone adds a cover image to the card and forgets one of them. Fragments keep one definition.

The rule

Name a selection once, spread it wherever that type appears.

A fragment is a named, reusable selection set tied to a type:

fragment BookCard on Book {
  title
  price
  author { name }
}
  • BookCard is its name.
  • on Book is its type condition: it may only be used where a Book is being selected.

Use it with a spread, three dots and the name:

query BrowsePage {
  books(first: 10) {
    ...BookCard
  }
}

query AuthorPage($id: ID!) {
  author(id: $id) {
    name
    books {
      ...BookCard
    }
  }
}

The spread is replaced by the fragment's fields, so the response has title, price and author keys directly. There is no BookCard key; fragments exist only in the query.

Rules the server checks:

  • A spread must fit its place: ...BookCard inside author { } fails, because an Author can never be a Book.
  • Every fragment defined in a document must be used.
  • Fragments cannot spread themselves, directly or in a loop.

There is also an inline fragment, ... on Book { title }, with a type condition but no name. It matters when a field can return several types, which the Schema section covers.

Frontend frameworks lean on fragments: each component declares the fragment it needs, and the page query spreads them. Change a component's fields and every page that shows it follows.

The shortcut

When you see repeated fields, extract, name, spread.

Sharing the book cardStep 1 of 4
  1. Extract

    The same title price author { name } sits under books and under author { books }. Both places select a Book.

The trap is spreading a fragment on the wrong type. Read the spread's surroundings: what type is being selected right here?

Drills

Answer fast — trust the rule.

quick drill

Which line uses the BookCard fragment?

quick drill

In fragment BookCard on Book, what does on Book do?

quick drill

Where does title appear in the response of { books { ...BookCard } }?

quick drill

Why is { book(id: "b1") { author { ...BookCard } } } rejected?

quick drill

A document defines fragment ReviewBits on Review { rating } but no operation spreads it. What happens?

Remember

  • Name a selection once, spread it wherever that type appears.
  • Extract, name, spread, then check each spread fits its type.
  • Trap: spreading on the wrong type, or defining a fragment nobody uses.

Practice — level up

0/1 solved

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

ExplainOne review line, many pages

Reviews appear on the book page and on a 'latest reviews' list, each showing the rating, the text and the book's title. Write a fragment for this and show it spread in one query. Explain what the type condition does and what the response looks like.