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

Nik Graf
InstructorNik Graf

Share this video with your friends

Send Tweet

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.

Tomás Francisco
Tomás Francisco
~ 2 years ago

Great tutorial Nik! Render Props pattern seems interesting, but it doesn't seem to scale easily when multiple requests need to be made in the same component. How would you do that? Would HOC be more scalable and improve the code readability in this situation?

Nik Graf
Nik Grafinstructor
~ 2 years ago

Hey @Tomás, some people indeed like the hoc more, but other struggle with it. I think eventually Apollo will be available using React Hooks and that, from my perspective will be a nice option.

Learn Sifnos
Learn Sifnos
~ 2 years ago

@Nik, here you will find two solutions.

@Tomás, I'm scratching my head about this: how come this works: <button onClick={() => refetch()}>Refetch</button> and this doesn't: <button onClick={refetch}>Refetch</button>

chihab
chihab
~ 2 years ago

Thank you @Nik Source code link is broken (on other lessons too). It should be https://github.com/nikgraf/graphql-apollo-client-course/tree/master/lesson-6.

Nik Graf
Nik Grafinstructor
~ 2 years ago

@Sébastien cool, didn't know about this one. Thanks for the hint!

@chihab thanks, just informed someone from the Egghead team so they can fix it.

David Balatero
David Balatero
~ 2 years ago

@Sébastien

I'd imagine it has to do with the fact that onClick will pass an event as the first parameter to the onClick function. Since I'm not sure about the signature of refetch(), it may be that it is expecting a different argument as parameter 1. The best way to ensure that refetch() is cleanly called and not passed anything weird by the browser's onClick semantics is to wrap it in a 0-arg function () => refetch().

Alex Radulescu
Alex Radulescu
~ 2 years ago

Hi @Nik,

Great intro into Graph and Apollo, thanks for it. I would like to second @Alex's point, on updating this course for hooks. This would be super helpful as I believe hooks are the future for React.