Course contentsShow
← Web Development
Lesson 1563 of 2,98228. GraphQL FundamentalsFree lesson

Polling and Refetching

Configure polling intervals and trigger manual refetches to keep data fresh when needed.

Polling and Refetching

What You'll Learn

You'll learn how to configure automatic polling intervals and trigger manual refetches to ensure your application displays the freshest data when needed.

The Core Idea

Sometimes data changes on the server without your app knowing—think live sports scores, stock prices, or social media feeds. Polling means repeatedly fetching data at regular intervals (every few seconds, for example). Refetching means manually triggering a fresh data fetch, usually in response to user actions like pulling to refresh or clicking a reload button.

Apollo Client provides built-in support for both patterns, saving you from building timer logic manually.

Polling: Automatic Updates

Think of polling like checking your mailbox every 10 minutes to see if new letters arrived. You set a pollInterval (in milliseconds) on your useQuery hook:

const { data, loading } = useQuery(GET_LIVE_SCORES, {
  pollInterval: 5000 // fetch fresh data every 5 seconds
});

Apollo will automatically re-execute the query at this interval, keeping your UI synchronized with server changes. Set pollInterval to 0 to stop polling.

Manual Refetching

Sometimes users want control—a "Refresh" button or pull-to-refresh gesture. The useQuery hook returns a refetch function:

const { data, loading, refetch } = useQuery(GET_NOTIFICATIONS);

const handleRefresh = () => {
  refetch(); // manually trigger a fresh fetch
};

This bypasses the cache and fetches directly from the server, perfect for user-initiated updates.

Choosing Between Them

Use polling for data that changes frequently and users expect to see live (dashboards, monitoring tools). Use refetch when users explicitly want fresh data or after actions that might change server state.

Key Takeaway: Polling keeps data fresh automatically at intervals, while refetching gives users manual control—both ensure your GraphQL data stays current without rebuilding caching logic from scratch.