Fragments let you save parts of your data requests to use again. This helps you avoid repeating the same code.
Fragments for reusable selections in GraphQL
Introduction
Syntax
GraphQL
fragment FragmentName on TypeName {
field1
field2
...
}
query {
someData {
...FragmentName
}
}Fragments start with the keyword fragment, followed by a name and the type they apply to.
Use ...FragmentName inside queries to include the fragment fields.
Examples
id, name, and email from a User. The query uses it to get these fields for user with id 1.GraphQL
fragment userFields on User {
id
name
email
}
query {
user(id: "1") {
...userFields
}
}title and content from Post. The query uses it to get these fields for recent posts.GraphQL
fragment postFields on Post {
title
content
}
query {
recentPosts {
...postFields
}
}Sample Program
This query gets a list of books with their titles and authors. The author fields come from the reusable fragment authorDetails.
GraphQL
fragment authorDetails on Author {
id
name
}
query {
books {
title
author {
...authorDetails
}
}
}Important Notes
Fragments must match the type they are defined on.
You can use fragments inside other fragments for more reuse.
Fragments help keep queries clean and easier to maintain.
Summary
Fragments save repeated field selections for reuse.
Use fragment keyword to define reusable parts.
Include fragments in queries with ...FragmentName.
Practice
1. What is the main purpose of using
fragments in GraphQL queries?easy
Solution
Step 1: Understand what fragments do
Fragments allow you to save a group of fields so you can use them repeatedly without rewriting.Step 2: Identify the purpose in queries
This helps keep queries clean and avoids repetition by reusing the same field selections.Final Answer:
To reuse a set of fields in multiple queries or parts of a query -> Option BQuick Check:
Fragments = reusable fields [OK]
Hint: Fragments save repeated fields for reuse in queries [OK]
Common Mistakes:
- Confusing fragments with variables
- Thinking fragments sort or filter data
- Using fragments to define mutations
2. Which of the following is the correct syntax to include a fragment named
UserDetails inside a query?easy
Solution
Step 1: Recall how to use fragments in queries
To include a fragment, you use three dots followed by the fragment name without parentheses.Step 2: Match the correct syntax
...UserDetails uses...UserDetails, which is the correct way to include the fragment.Final Answer:
...UserDetails -> Option AQuick Check:
Include fragment = ...FragmentName [OK]
Hint: Use three dots plus fragment name to include it [OK]
Common Mistakes:
- Adding parentheses after fragment name
- Using 'include' keyword instead of ...
- Trying to redefine fragment inside query
3. Given the fragment definition:
and the query:
What fields will the query return?
fragment UserInfo on User { id name }and the query:
{ user(id: 1) { ...UserInfo email } }What fields will the query return?
medium
Solution
Step 1: Understand fragment fields
The fragmentUserInfoincludesidandnamefields.Step 2: Combine fragment with query fields
The query requests...UserInfoplusemail, so all three fields are returned.Final Answer:
{ id, name, email } -> Option CQuick Check:
Fragment fields + extra fields = all returned [OK]
Hint: Fragment fields merge with query fields in results [OK]
Common Mistakes:
- Ignoring fields outside the fragment
- Thinking fragment replaces query fields
- Confusing fragment name with field name
4. Identify the error in this GraphQL query using a fragment:
fragment PostFields on Post { title content } query { posts { ...PostFields() } }medium
Solution
Step 1: Check fragment spread syntax
Fragment spreads use three dots plus the fragment name without parentheses.Step 2: Identify the mistake
The query uses...PostFields()which incorrectly adds parentheses.Final Answer:
Fragment spread should not have parentheses -> Option AQuick Check:
Fragment spread = ...FragmentName without () [OK]
Hint: Do not add parentheses when spreading fragments [OK]
Common Mistakes:
- Adding () after fragment name
- Using lowercase fragment names incorrectly
- Thinking id field is mandatory in fragments
5. You want to fetch a list of books with their
Which query correctly uses this fragment to get books with author info?
title and author details (name and email). You have a fragment:fragment AuthorInfo on Author { name email }Which query correctly uses this fragment to get books with author info?
hard
Solution
Step 1: Recall correct fragment usage inside nested fields
Fragments are included with three dots and the fragment name without parentheses or extra keywords.Step 2: Check each option
query { books { title author { ...AuthorInfo } } } correctly uses...AuthorInfoinsideauthorfield. Others have syntax errors or extra invalid parts.Final Answer:
query { books { title author { ...AuthorInfo } } } -> Option DQuick Check:
Use ...FragmentName inside nested fields [OK]
Hint: Use ...FragmentName exactly inside nested fields [OK]
Common Mistakes:
- Adding parentheses after fragment name
- Using fragment name as a field
- Adding 'on Type' inside fragment spread
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
