Bird
Raised Fist0
GraphQLquery~5 mins

Fragments for reusable selections in GraphQL - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Fragments for reusable selections
O(n)
Understanding Time Complexity

When using fragments in GraphQL, we want to understand how the time to get data changes as the data grows.

We ask: How does using fragments affect the work done when fetching repeated fields?

Scenario Under Consideration

Analyze the time complexity of the following GraphQL query using fragments.


query GetUsers {
  users {
    ...UserDetails
  }
}

fragment UserDetails on User {
  id
  name
  email
}
    

This query fetches a list of users and reuses the same set of fields for each user using a fragment.

Identify Repeating Operations

Look for repeated actions in the query execution.

  • Primary operation: Fetching the fields inside the fragment for each user.
  • How many times: Once for each user in the list.
How Execution Grows With Input

As the number of users grows, the work to fetch their details grows too.

Input Size (n)Approx. Operations
10Fetching details for 10 users
100Fetching details for 100 users
1000Fetching details for 1000 users

Pattern observation: The work grows directly with the number of users.

Final Time Complexity

Time Complexity: O(n)

This means the time to get data grows in a straight line as the number of users grows.

Common Mistake

[X] Wrong: "Using fragments makes the query run faster because it avoids repeating fields."

[OK] Correct: Fragments only help organize the query and avoid rewriting fields, but the server still fetches data for each user separately.

Interview Connect

Understanding how repeated data fetching scales helps you explain performance in real projects and shows you know how queries behave as data grows.

Self-Check

What if the fragment included a nested list of posts for each user? How would the time complexity change?

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