What if you could fix all your repeated query fields by changing just one piece of code?
Why Fragments for reusable selections in GraphQL? - Purpose & Use Cases
Imagine you are building a website that shows user profiles on many pages. Each page needs the same user details like name, email, and profile picture. You copy and paste the same list of fields everywhere in your GraphQL queries.
Copying the same fields again and again is slow and boring. If you want to add or change a field, you must update every query manually. This causes mistakes and wastes time.
Fragments let you write the list of fields once and reuse it in many queries. This keeps your code clean and easy to update. Change the fragment once, and all queries using it get updated automatically.
query GetUser { user { id name email picture } } query GetFriend { friend { id name email picture } }fragment UserDetails on User { id name email picture } query GetUser { user { ...UserDetails } } query GetFriend { friend { ...UserDetails } }Fragments make your GraphQL queries simpler, reusable, and easier to maintain across your whole app.
A social media app shows user info on profile pages, friend lists, and comments. Using fragments, it fetches user details consistently everywhere without repeating code.
Writing repeated fields manually is slow and error-prone.
Fragments let you define reusable field sets once.
Updating fragments updates all queries using them automatically.
Practice
fragments in GraphQL queries?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]
- Confusing fragments with variables
- Thinking fragments sort or filter data
- Using fragments to define mutations
UserDetails inside a query?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]
- Adding parentheses after fragment name
- Using 'include' keyword instead of ...
- Trying to redefine fragment inside query
fragment UserInfo on User { id name }and the query:
{ user(id: 1) { ...UserInfo email } }What fields will the query return?
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]
- Ignoring fields outside the fragment
- Thinking fragment replaces query fields
- Confusing fragment name with field name
fragment PostFields on Post { title content } query { posts { ...PostFields() } }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]
- Adding () after fragment name
- Using lowercase fragment names incorrectly
- Thinking id field is mandatory in fragments
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?
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]
- Adding parentheses after fragment name
- Using fragment name as a field
- Adding 'on Type' inside fragment spread
Ready to prove it?
Pass the test and this pattern moves unknown
