Bird
Raised Fist0
GraphQLquery~20 mins

Over-fetching and under-fetching problems in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Fetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What data does this GraphQL query return?

Given the following GraphQL query requesting user details and their posts, what is the exact data returned?

GraphQL
query {
  user(id: "1") {
    id
    name
    posts {
      id
      title
    }
  }
}
A{"data":{"user":{"id":"1","name":"Alice","posts":[{"id":"101","title":"GraphQL Basics"},{"id":"102","title":"Advanced GraphQL"}]}}}
B{"data":{"user":{"id":"1","name":"Alice"}}}
C{"data":{"user":{"name":"Alice","posts":[{"id":"101","title":"GraphQL Basics"}]}}}
D{"data":{"user":{"id":"1","posts":[{"id":"101","title":"GraphQL Basics"}]}}}
Attempts:
2 left
💡 Hint

Check which fields are requested inside the user object.

🧠 Conceptual
intermediate
2:00remaining
Which problem does this GraphQL query cause?

This GraphQL query requests all fields of a user and all fields of their posts, including large content fields. What problem does this cause?

GraphQL
query {
  user(id: "1") {
    id
    name
    email
    posts {
      id
      title
      content
      comments {
        id
        text
      }
    }
  }
}
ANo problem: The query is perfectly optimized.
BUnder-fetching: The query misses some fields needed by the client.
CSyntax error: The query is invalid due to missing brackets.
DOver-fetching: The query requests more data than needed, causing slow responses.
Attempts:
2 left
💡 Hint

Think about the size and amount of data requested.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL query

Find the syntax error in the following GraphQL query that fetches user and posts data.

GraphQL
query {
  user(id: "1") {
    id
    name
    posts {
      id
      title
    }
  
}
AMissing closing brace for the user field.
BMissing comma between fields.
CUsing parentheses instead of braces for posts.
DMissing quotes around the id value.
Attempts:
2 left
💡 Hint

Count the opening and closing braces carefully.

❓ optimization
advanced
2:00remaining
How to fix under-fetching in this GraphQL query?

The client needs the user's email but the query only requests id and name. How to fix this under-fetching problem?

GraphQL
query {
  user(id: "1") {
    id
    name
  }
}
ARemove the id field from the query.
BAdd the email field inside the user selection set.
CAdd a separate query for email outside user.
DUse a mutation instead of a query.
Attempts:
2 left
💡 Hint

Think about what fields the client needs to see.

🧠 Conceptual
expert
2:00remaining
Why does GraphQL help solve over-fetching and under-fetching?

Choose the best explanation for how GraphQL addresses over-fetching and under-fetching problems compared to REST APIs.

AGraphQL automatically caches all data to prevent over-fetching.
BGraphQL forces clients to fetch all data in one request, preventing under-fetching.
CGraphQL lets clients specify exactly which fields they want, avoiding extra or missing data.
DGraphQL uses fixed endpoints that return fixed data sets.
Attempts:
2 left
💡 Hint

Think about how GraphQL queries differ from REST endpoints.

Practice

(1/5)
1. What does over-fetching mean in GraphQL?
easy
A. Getting more data than the client actually needs
B. Getting less data than the client actually needs
C. Getting exactly the data the client requests
D. Getting data from multiple sources at once

Solution

  1. Step 1: Understand the term over-fetching

    Over-fetching means the client receives more data than it requires, which can slow down the app.
  2. Step 2: Compare with other options

    Under-fetching is the opposite (less data), exact fetching matches needs, and multiple sources is unrelated.
  3. Final Answer:

    Getting more data than the client actually needs -> Option A
  4. Quick Check:

    Over-fetching = More data than needed [OK]
Hint: Over-fetching means extra data beyond need [OK]
Common Mistakes:
  • Confusing over-fetching with under-fetching
  • Thinking over-fetching means exact data
  • Mixing up data sources with data amount
2. Which GraphQL query syntax correctly avoids over-fetching by requesting only the user's name and email?
easy
A. { user { id name email } }
B. { user { name email posts } }
C. { user { name email } }
D. { user { id posts comments } }

Solution

  1. Step 1: Identify requested fields

    { user { name email } } requests only name and email, exactly what is needed.
  2. Step 2: Check other options for extra fields

    { user { id name email } } includes id (extra), B and D include posts and comments (extra data).
  3. Final Answer:

    { user { name email } } -> Option C
  4. Quick Check:

    Request only needed fields = { user { name email } } [OK]
Hint: Pick query with only needed fields, no extras [OK]
Common Mistakes:
  • Including extra fields causing over-fetching
  • Missing required fields causing under-fetching
  • Confusing field names or nesting
3. Given this GraphQL query:
{ user { name posts { title } } }

What problem might this query cause if the client only needs the user's name?
medium
A. No problem, it fetches exactly what is needed
B. Under-fetching, missing the posts data
C. Syntax error due to missing fields
D. Over-fetching, fetching unnecessary posts data

Solution

  1. Step 1: Analyze the query fields

    The query requests name and posts { title }, which includes posts data.
  2. Step 2: Compare with client needs

    The client only needs name, so fetching posts is extra data causing over-fetching.
  3. Final Answer:

    Over-fetching, fetching unnecessary posts data -> Option D
  4. Quick Check:

    Extra fields cause over-fetching = Over-fetching, fetching unnecessary posts data [OK]
Hint: Check if query requests more than needed fields [OK]
Common Mistakes:
  • Thinking missing posts is under-fetching
  • Assuming no problem if query runs
  • Confusing syntax errors with data issues
4. Identify the error in this GraphQL query that causes under-fetching:
{ user { name } }

The client needs the user's name and email.
medium
A. The query is missing the email field
B. The query has a syntax error in braces
C. The query requests too many fields
D. The query uses incorrect field names

Solution

  1. Step 1: Check requested fields

    The query requests only name, missing email.
  2. Step 2: Compare with client needs

    The client needs both name and email, so missing email causes under-fetching.
  3. Final Answer:

    The query is missing the email field -> Option A
  4. Quick Check:

    Missing needed fields = under-fetching [OK]
Hint: Check if all needed fields are requested [OK]
Common Mistakes:
  • Assuming syntax error when none exists
  • Thinking too many fields cause under-fetching
  • Ignoring missing fields as a problem
5. You have a GraphQL query fetching a list of users with their posts and comments:
{ users { id name posts { title comments { text } } } }

To avoid over-fetching when the client only needs user names and post titles, how should you modify the query?
hard
A. { users { id name posts { title comments { text } } } }
B. { users { name posts { title } } }
C. { users { name posts { comments { text } } } }
D. { users { posts { title } } }

Solution

  1. Step 1: Identify needed fields

    The client needs only name and post titles, no id or comments.
  2. Step 2: Modify query to include only needed fields

    { users { name posts { title } } } requests name and posts { title }, exactly matching needs without extra data.
  3. Final Answer:

    { users { name posts { title } } } -> Option B
  4. Quick Check:

    Remove unneeded fields to avoid over-fetching [OK]
Hint: Keep only fields client needs, remove nested extras [OK]
Common Mistakes:
  • Leaving comments in query causing over-fetching
  • Removing needed fields like user name
  • Removing posts entirely causing under-fetching
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