Over-fetching and under-fetching happen when you get too much or too little data from a database. This can slow down apps or make them miss needed info.
When a mobile app loads a user profile but gets extra unused details, slowing it down.
When a website asks for only a user's name but the server sends the whole user record.
When a dashboard needs sales totals but the query returns every sale detail, making it slow.
When an app requests a list of products but only gets one product's info, missing others.
When a report needs customer emails but the query misses that field, causing errors.
Syntax
GraphQL
query {
entityName {
field1
field2
...
}
}
GraphQL queries specify exactly which fields you want.
This helps avoid over-fetching by not asking for extra fields.
Examples
This query fetches only the user's id and name, avoiding extra data.
GraphQL
query {
user {
id
name
}
}
Fetches only the title and content of posts, no extra fields.
GraphQL
query {
posts {
title
content
}
}
Fetches specific product details by id, avoiding unrelated data.
GraphQL
query {
product(id: "123") {
name
price
}
}
Sample Program
This query asks for only the id, name, and email of user with id 1, avoiding extra fields like address or phone.
GraphQL
query {
user(id: "1") {
id
name
email
}
}
OutputSuccess
Important Notes
Over-fetching wastes bandwidth and slows apps.
Under-fetching means you may need extra queries, causing delays.
GraphQL helps fix these by letting you ask for exactly what you need.
Summary
Over-fetching means getting more data than needed.
Under-fetching means getting less data than needed.
GraphQL queries solve these by letting you specify exact fields.
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
Step 1: Understand the term over-fetching
Over-fetching means the client receives more data than it requires, which can slow down the app.
Step 2: Compare with other options
Under-fetching is the opposite (less data), exact fetching matches needs, and multiple sources is unrelated.
Final Answer:
Getting more data than the client actually needs -> Option A
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
Step 1: Identify requested fields
{ user { name email } } requests only name and email, exactly what is needed.
Step 2: Check other options for extra fields
{ user { id name email } } includes id (extra), B and D include posts and comments (extra data).
Final Answer:
{ user { name email } } -> Option C
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
Step 1: Analyze the query fields
The query requests name and posts { title }, which includes posts data.
Step 2: Compare with client needs
The client only needs name, so fetching posts is extra data causing over-fetching.
Final Answer:
Over-fetching, fetching unnecessary posts data -> Option D
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
Step 1: Check requested fields
The query requests only name, missing email.
Step 2: Compare with client needs
The client needs both name and email, so missing email causes under-fetching.
Final Answer:
The query is missing the email field -> Option A
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
Step 1: Identify needed fields
The client needs only name and post titles, no id or comments.
Step 2: Modify query to include only needed fields
{ users { name posts { title } } } requests name and posts { title }, exactly matching needs without extra data.
Final Answer:
{ users { name posts { title } } } -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.