Bird
Raised Fist0
GraphQLquery~3 mins

Why Fragments for reusable selections in GraphQL? - Purpose & Use Cases

Choose your learning style10 modes available
The Big Idea

What if you could fix all your repeated query fields by changing just one piece of code?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
✗ Before
query GetUser { user { id name email picture } } query GetFriend { friend { id name email picture } }
✓ After
fragment UserDetails on User { id name email picture } query GetUser { user { ...UserDetails } } query GetFriend { friend { ...UserDetails } }
What It Enables

Fragments make your GraphQL queries simpler, reusable, and easier to maintain across your whole app.

Real Life Example

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.

Key Takeaways

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

(1/5)
1. What is the main purpose of using fragments in GraphQL queries?
easy
A. To define variables for queries
B. To reuse a set of fields in multiple queries or parts of a query
C. To create mutations for updating data
D. To sort the results of a query

Solution

  1. Step 1: Understand what fragments do

    Fragments allow you to save a group of fields so you can use them repeatedly without rewriting.
  2. Step 2: Identify the purpose in queries

    This helps keep queries clean and avoids repetition by reusing the same field selections.
  3. Final Answer:

    To reuse a set of fields in multiple queries or parts of a query -> Option B
  4. Quick 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
A. ...UserDetails
B. fragment UserDetails {...}
C. ...UserDetails()
D. include UserDetails

Solution

  1. Step 1: Recall how to use fragments in queries

    To include a fragment, you use three dots followed by the fragment name without parentheses.
  2. Step 2: Match the correct syntax

    ...UserDetails uses ...UserDetails, which is the correct way to include the fragment.
  3. Final Answer:

    ...UserDetails -> Option A
  4. Quick 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:
fragment UserInfo on User { id name }

and the query:
{ user(id: 1) { ...UserInfo email } }

What fields will the query return?
medium
A. { id, email }
B. { id, name }
C. { id, name, email }
D. { email }

Solution

  1. Step 1: Understand fragment fields

    The fragment UserInfo includes id and name fields.
  2. Step 2: Combine fragment with query fields

    The query requests ...UserInfo plus email, so all three fields are returned.
  3. Final Answer:

    { id, name, email } -> Option C
  4. Quick 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
A. Fragment spread should not have parentheses
B. Fragments cannot be used inside queries
C. Fragment name must be lowercase
D. Fragment must include an id field

Solution

  1. Step 1: Check fragment spread syntax

    Fragment spreads use three dots plus the fragment name without parentheses.
  2. Step 2: Identify the mistake

    The query uses ...PostFields() which incorrectly adds parentheses.
  3. Final Answer:

    Fragment spread should not have parentheses -> Option A
  4. Quick 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 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
A. query { books { title author { ...AuthorInfo() } } }
B. query { books { title author { AuthorInfo } } }
C. query { books { title author { ...AuthorInfo on Author } } }
D. query { books { title author { ...AuthorInfo } } }

Solution

  1. Step 1: Recall correct fragment usage inside nested fields

    Fragments are included with three dots and the fragment name without parentheses or extra keywords.
  2. Step 2: Check each option

    query { books { title author { ...AuthorInfo } } } correctly uses ...AuthorInfo inside author field. Others have syntax errors or extra invalid parts.
  3. Final Answer:

    query { books { title author { ...AuthorInfo } } } -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard