GraphQL Fragments, Built Live
A GraphQL fragment is a named, reusable set of fields for one type, written as fragment Name on Type and pulled into a query with ...Name. An inline fragment, written ... on Type, has no name and picks fields only when the result is that concrete type, which is how you query unions and interfaces.
| Form | Syntax | Use it for |
|---|---|---|
| Named fragment | fragment UserCard on User { ... } | Sharing one field list across queries |
| Spread | ...UserCard | Including a named fragment's fields |
| Inline fragment | ... on Photo { url } | Fields of one member of a union or interface |
| Inline without type | ... @include(if: $full) { bio } | Applying a directive to a group of fields |
| Type name | __typename | Telling union members apart in the response |
Updated
Tick the fields you want in the builder and it writes the query for you, once with a named fragment and once with the fields typed inline, so you can see they ask for the same data. Turn on the search results option to add inline fragments over a union.
Fragment query builder
- id
- name
- avatarUrl
- createdAt
query Profile($id: ID!) {
user(id: $id) {
...UserCard
}
}
fragment UserCard on User {
id
name
avatarUrl
}Schema used by the builder
A union for search results
SearchResult can be a User or a Photo, so a selection on it needs inline fragments to reach fields of either member.
type User {
id: ID!
name: String!
avatarUrl: String
email: String
createdAt: String!
}
type Photo {
id: ID!
url: String!
width: Int!
}
union SearchResult = User | Photo
type Query {
user(id: ID!): User
search(text: String!): [SearchResult!]!
}Reading a union response
What the search returns
Each item carries only the fields of its own branch, plus __typename.
{
"data": {
"search": [
{ "__typename": "User", "id": "42", "name": "Ada" },
{ "__typename": "Photo", "url": "/p/lake.jpg", "width": 1200 }
]
}
}Branching on __typename
The type name is the safe way to tell the members apart; checking for a field can break when a type later gains it.
for (const item of data.search) {
switch (item.__typename) {
case "User":
renderUser(item);
break;
case "Photo":
renderPhoto(item);
break;
}
}
Named fragments for reuse
Suppose a profile header, a comment and a member list all show a name and an avatar. Typing those fields three times invites drift: one copy gains a field the others lack. A fragment on User holds the list once and each query spreads it.
Component-based apps take this further. Each component declares the fragment it needs, and the page query spreads the fragments of the components it renders, so data needs live beside the code that uses them.
Inline fragments and type conditions
The on Type part is the type condition. For a named fragment it says which type the fields belong to. For an inline fragment inside a union field, it makes those fields apply only when the object is that type.
A search that returns users, posts and photos is the classic case. The selection lists ... on User, ... on Post and ... on Photo, each with its own fields, and __typename tells the client which branch each result took.
How it works
- Tick the User fields the card on your screen shows.
- Choose whether the fields go into a named fragment or straight into the query.
- Turn on search results to add a union field with an inline fragment per member type.
- Copy the query and run it against a schema that has these types.
Tips
- Name fragments after the component that uses them, such as UserCard or PostRow, so each part of the screen states its own data needs.
- Always add __typename when you select from a union or interface; your code needs it to know which member it got.
- Fragments can read the operation's variables, so a fragment field may take an argument like size: $avatarSize.
- A fragment may not spread itself, directly or through another fragment. Validation rejects the cycle before anything runs.
Frequently asked questions
Do fragments make responses smaller?
No. A fragment is a writing aid for the query. The server expands it, and the response looks exactly as if you had typed the fields out. If two spreads select the same field, it appears once.
When must I use an inline fragment?
When a field returns a union or an interface and you need fields that only some member types have. graphql.org shows this with a hero that may be a Droid with primaryFunction or a Human with height.
Can a fragment be used on a different type?
Only where it could apply. A fragment on an interface can be spread inside any object type that implements it, but a fragment on User spread inside a Post selection fails validation.
Where do fragments live in the document?
At the top level of the document, next to the operations, in any order. Every fragment defined must be used, or the server rejects the document as invalid.
Related: GraphQL vs REST, GraphQL mutations, GraphQL subscriptions, JavaScript async and fetch.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- GraphQL Mutations, With Working Examples
- GraphQL Subscriptions, Step by Step
- GraphQL vs REST: How They Really Differ
- Hard Bounce vs Soft Bounce
- Headless CMS Compared: Six Options, One Blog
- High Contrast Color Palettes, With Ratios Checked
- Build a Home Care Website: Pages, Costs and a Working Example | How-to Guide
- Hosts File Location: Find and Edit It on Any System
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App