Refetch Data with the Apollo Query Component either Manually or on Timed Intervals

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

In this lesson we will first explore how to manually refetch data for an already executed query to receive the latest state of our data.

Later we use polling to refetch the latest state on a timed interval. Polling can be a simple and effective tool to provide an almost real-time experience without the overhead of setting up a WebSocket solution.

Instructor: [00:00] So far, we used data, loading, error from the render prop function of a query component. There are a couple more utilities coming with this object. One of them is refetch. Once invoked, it will rerun the query. Let's give this a try by adding a button to refresh to recipes.

[00:29] Once implemented we add a new item in another browser, switch back to our previous one, and execute the refetch by pressing the button.

[00:48] As expected, we now see the new recipe being added. Other examples of useful utilities coming inside the render prop, a network status, fetch more for pagination, or start and stop polling.

[01:02] That said, the easiest way to achieve polling for new results is using the poll interval prop on the query component itself. By default, it's deactivated, but if you provide a number like 3,000, the component will rerun the query every third second. Again, you want to verify it by adding get another recipe.

[01:29] As you can see, the banana muffin was added. Without web sockets being available, polling can be a simple and effective tool to provide an almost real time experience.