What if you could get just the data you want, instantly and without waste?
Why Over-fetching and under-fetching problems in GraphQL? - Purpose & Use Cases
Imagine you want to get just a friend's phone number from a huge phone book. You flip through pages and copy the entire page with many names and numbers, even though you only need one number.
This manual way wastes time and effort. You get too much information you don't need (over-fetching), or sometimes you get too little and have to look again (under-fetching). It's slow and confusing.
GraphQL lets you ask exactly for the data you want, no more and no less. It's like telling the phone book to give you only the friend's phone number, saving time and avoiding extra clutter.
query { allUsers { id name email phone address } } # gets everything even if you want only phonequery { user(id: 1) { phone } } # gets only the phone number you needYou can fetch just the right data in one go, making apps faster and easier to build.
A social media app shows your friend's profile picture and name instantly without loading their entire post history or settings.
Manual data fetching often grabs too much or too little data.
Over-fetching wastes resources; under-fetching causes extra requests.
GraphQL solves this by letting you specify exactly what you need.
Practice
over-fetching mean in GraphQL?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 AQuick Check:
Over-fetching = More data than needed [OK]
- Confusing over-fetching with under-fetching
- Thinking over-fetching means exact data
- Mixing up data sources with data amount
Solution
Step 1: Identify requested fields
{ user { name email } }requests onlynameandemail, exactly what is needed.Step 2: Check other options for extra fields
{ user { id name email } }includesid(extra), B and D includepostsandcomments(extra data).Final Answer:
{ user { name email } } -> Option CQuick Check:
Request only needed fields ={ user { name email } }[OK]
- Including extra fields causing over-fetching
- Missing required fields causing under-fetching
- Confusing field names or nesting
{ user { name posts { title } } }What problem might this query cause if the client only needs the user's name?
Solution
Step 1: Analyze the query fields
The query requestsnameandposts { title }, which includes posts data.Step 2: Compare with client needs
The client only needsname, so fetching posts is extra data causing over-fetching.Final Answer:
Over-fetching, fetching unnecessary posts data -> Option DQuick Check:
Extra fields cause over-fetching = Over-fetching, fetching unnecessary posts data [OK]
- Thinking missing posts is under-fetching
- Assuming no problem if query runs
- Confusing syntax errors with data issues
{ user { name } }The client needs the user's name and email.
Solution
Step 1: Check requested fields
The query requests onlyname, missingemail.Step 2: Compare with client needs
The client needs bothnameandemail, so missingemailcauses under-fetching.Final Answer:
The query is missing theemailfield -> Option AQuick Check:
Missing needed fields = under-fetching [OK]
- Assuming syntax error when none exists
- Thinking too many fields cause under-fetching
- Ignoring missing fields as a problem
{ 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?
Solution
Step 1: Identify needed fields
The client needs onlynameandpost titles, noidor comments.Step 2: Modify query to include only needed fields
{ users { name posts { title } } }requestsnameandposts { title }, exactly matching needs without extra data.Final Answer:
{ users { name posts { title } } } -> Option BQuick Check:
Remove unneeded fields to avoid over-fetching [OK]
- Leaving comments in query causing over-fetching
- Removing needed fields like user name
- Removing posts entirely causing under-fetching
Ready to prove it?
Pass the test and this pattern moves unknown
