What if your app could update itself like magic, without you doing anything?
Why Refetching and polling in GraphQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a webpage showing live sports scores. Without automatic updates, you have to refresh the whole page manually every few seconds to see the latest scores.
Manually refreshing is slow and annoying. You might miss important updates between refreshes, and constantly reloading wastes data and makes the page flicker.
Refetching and polling let your app automatically ask the server for fresh data at set intervals. This keeps your page updated smoothly without you lifting a finger.
window.setInterval(() => window.location.reload(), 5000);useQuery(GET_SCORES, { pollInterval: 5000 });This makes your app feel alive and responsive, always showing the latest info without annoying reloads.
News apps use polling to update headlines every minute, so you see breaking news instantly without refreshing.
Manual refresh is slow and frustrating.
Refetching and polling automate data updates.
Your app stays fresh and user-friendly effortlessly.
Practice
refetching in GraphQL?Solution
Step 1: Understand refetching concept
Refetching means running the same query again to get fresh data when needed.Step 2: Compare with polling
Polling updates data automatically, but refetching is manual and on demand.Final Answer:
To manually update data by running the query again -> Option AQuick Check:
Refetching = manual update [OK]
- Confusing refetching with automatic polling
- Thinking refetching caches data
- Assuming refetching deletes data
Solution
Step 1: Recall Apollo Client polling syntax
Polling is enabled by settingpollIntervalin milliseconds.Step 2: Check options for correctness
Only useQuery(MY_QUERY, { pollInterval: 5000 }) uses the correct propertypollIntervalwith 5000 ms (5 seconds).Final Answer:
useQuery(MY_QUERY, { pollInterval: 5000 }) -> Option CQuick Check:
pollInterval = 5000 ms [OK]
- Using wrong property names like refetchInterval
- Confusing seconds with milliseconds
- Assuming autoPoll is a valid option
const { data, loading, refetch } = useQuery(GET_USERS, { pollInterval: 10000 });
setTimeout(() => refetch(), 5000);What will happen regarding data updates?
Solution
Step 1: Understand polling behavior
Polling updates data automatically every 10 seconds as set bypollInterval.Step 2: Understand manual refetch
ThesetTimeoutcallsrefetch()once after 5 seconds, triggering an immediate update.Step 3: Combine effects
So data updates at 5 seconds manually, then continues updating every 10 seconds automatically.Final Answer:
Data updates at 5 seconds and then every 10 seconds -> Option BQuick Check:
Manual refetch + polling = updates at 5s and every 10s [OK]
- Ignoring manual refetch timing
- Assuming polling stops after manual refetch
- Confusing polling interval with refetch delay
const { data, loading } = useQuery(GET_POSTS, { pollInterval: 3000 });But data is not updating automatically. What is the likely cause?
Solution
Step 1: Understand polling requirements
Polling runs only when the component is mounted and active.Step 2: Check common issues
If data is not updating, the component might be unmounted or React Suspense is pausing it.Step 3: Eliminate other options
Polling does not need manual refetch, and 3000 ms is valid.Final Answer:
The component is unmounted or paused -> Option AQuick Check:
Polling requires active component [OK]
- Thinking polling needs manual refetch
- Assuming pollInterval too low stops polling
- Ignoring component lifecycle
Solution
Step 1: Understand polling and manual refresh
Polling updates data automatically every 15 seconds.Step 2: Add manual refresh
Adding a button that callsrefetch()lets users update data on demand.Step 3: Evaluate options
Use polling withpollInterval: 15000and add a button that callsrefetch()combines both correctly; others either disable manual refresh or misuse refetching.Final Answer:
Use polling with pollInterval: 15000 and add a button that calls refetch() -> Option DQuick Check:
Polling + manual refetch button = best combo [OK]
- Using only manual refetch without polling
- Disabling manual refresh when polling is active
- Replacing polling with setInterval refetch
