Introduction
Refetching and polling help keep your data fresh by asking the server for updates regularly or on demand.
Jump into concepts and practice - no test required
Refetching and polling help keep your data fresh by asking the server for updates regularly or on demand.
query YourQueryName {
yourData {
field1
field2
}
}
// Refetching: Call the query again manually when needed.
// Polling example (in Apollo Client):
useQuery(YOUR_QUERY, { pollInterval: 5000 })Refetching means running the query again to get fresh data.
Polling means running the query automatically at set time intervals.
query GetUsers {
users {
id
name
}
}refetch() to manually update the data.// Refetch example in Apollo Client const { data, refetch } = useQuery(GET_USERS); // Call refetch() when you want fresh data
// Polling example in Apollo Client const { data } = useQuery(GET_USERS, { pollInterval: 10000 });
This query fetches messages and automatically updates every 3 seconds.
query GetMessages {
messages {
id
text
sender
}
}
// In Apollo Client React hook:
const { data, loading, error } = useQuery(GET_MESSAGES, { pollInterval: 3000 });Polling can increase server load; use it only when needed.
Refetching is good for manual updates, like after a user action.
Always handle loading and error states when fetching data.
Refetching updates data on demand by running the query again.
Polling updates data automatically at regular intervals.
Use these to keep your app's data fresh and responsive.
refetching in GraphQL?pollInterval in milliseconds.pollInterval with 5000 ms (5 seconds).const { data, loading, refetch } = useQuery(GET_USERS, { pollInterval: 10000 });
setTimeout(() => refetch(), 5000);pollInterval.setTimeout calls refetch() once after 5 seconds, triggering an immediate update.const { data, loading } = useQuery(GET_POSTS, { pollInterval: 3000 });refetch() lets users update data on demand.pollInterval: 15000 and add a button that calls refetch() combines both correctly; others either disable manual refresh or misuse refetching.