Skip to main content
Version: s5

Fragments

Overview

Fragments are a reusable set of fields of a type that can be used in your queries. Fragments are extremely useful when multiple operations all need to fetch the same or overlapping data. This is because we can use our fragment in multiple operations, so we only need to declare what fields we want in one place.

note

Fragments are especially useful and common when using automation tools as discussed in implementation recommendations.

Define a fragment

To declare a fragment you must start with the keyword fragment followed by the given fragment name then the keyword on and the type that the fragment applies to. Finally the fields of the fragment are listed within curly brackets. For example:

fragment examplePostFragment on PostDetails {
id
title
}

Using a fragment

To use a fragment, simply type the spread operator ... followed by the fragment name within the type that the fragment applies to. It is also possible to request other fields or fragments within the same object. For example:

fragment examplePostFragment on PostDetails {
id
title
}

query exampleOperation {
getPostDetails {
...examplePostFragment
description
}
}

Fragments in a query