Bird
Raised Fist0
GraphQLquery~30 mins

Over-fetching and under-fetching problems in GraphQL - Mini Project: Build & Apply

Choose your learning style10 modes available
Solving Over-fetching and Under-fetching Problems in GraphQL
📖 Scenario: You are building a simple social media app backend using GraphQL. Users have profiles with posts and comments. You want to fetch only the data needed for a user profile page without extra or missing information.
🎯 Goal: Build a GraphQL query step-by-step that fetches exactly the user's id, name, and their posts with only the title and comments count, avoiding over-fetching and under-fetching.
📋 What You'll Learn
Create a GraphQL query to fetch user data
Add a variable to select the user by id
Fetch only the user's id and name
Fetch the user's posts with only title and number of comments
Avoid fetching unnecessary fields (no over-fetching)
Ensure all needed fields for the profile page are included (no under-fetching)
💡 Why This Matters
🌍 Real World
GraphQL queries are used in modern web and mobile apps to fetch exactly the data needed, improving performance and user experience.
💼 Career
Understanding how to avoid over-fetching and under-fetching is essential for backend and frontend developers working with GraphQL APIs.
Progress0 / 4 steps
1
Create the base GraphQL query structure
Write a GraphQL query named GetUserProfile that takes a variable $userId of type ID! and fetches the user by id. Fetch only the user's id and name fields inside the query.
GraphQL
Hint

Start by defining the query with a variable $userId and fetch the user fields id and name.

2
Add fetching of user's posts with limited fields
Inside the user field, add fetching of posts. For each post, fetch only the title field.
GraphQL
Hint

Add the posts field inside user and fetch only the title for each post.

3
Add comment count for each post to avoid under-fetching
Inside each post, add fetching of the number of comments using the commentsCount field to avoid under-fetching data needed for the profile page.
GraphQL
Hint

Fetch the commentsCount field inside each post to get the number of comments.

4
Complete the query ensuring no over-fetching or under-fetching
Review the query and add any missing closing braces to complete the GraphQL query named GetUserProfile that fetches id, name, and posts with title and commentsCount fields.
GraphQL
Hint

Make sure all braces are closed properly to complete the query.

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