Apollo-client: pollInterval with fetchPolicy of 'cache-first'

Created on 16 May 2017  路  7Comments  路  Source: apollographql/apollo-client

Use case: I want a query to load data from the cache, and only fetch from network if it's been more than 1 day. The API docs indicate that I need to use fetchPolicy: 'cache-first' and pollInterval: 1000*60*60*24.

But this gives me the error (attached) 'Queries that specify the cache-first and cache-only fetchPolicies cannot also be polling queries.'.

Why should that be the case? The expected behavior would be for the Apollo client to include a timestamp in its redux store of the last time the query was fetched. Then in setup it should check if it's been more than pollInterval since the last fetch. I believe that was how it worked before v1.

simulator screen shot may 16 2017 10 10 33 am

Most helpful comment

I had this error using fetchPolicy: 'network-only' with server-side rendering.

Need to check for window object e.g.
pollInterval: typeof window === 'undefined' ? null : 180 * 1000,

I guess the error message could be more clear 馃槃

All 7 comments

@StevePotter the way the cache-first fetchPolicy works is that every time that query is run, we check if the data is in the cache. If it is, we'll return it from there and hit the server. The way we conceived it, we assumed that polling queries would only be used if you know you want to fetch from the server.

I'm sure this is something we can change to support your use-case, but before we get too far into the weeds, can you describe how you are currently expiring stuff in the cache?

I'm going to close this for now because we're not going to change it in the current version of the API, but please add a comment to the use-cases and API wishlist issue on react-apollo so we can keep it in mind during a future refactor!

I had this error using fetchPolicy: 'network-only' with server-side rendering.

Need to check for window object e.g.
pollInterval: typeof window === 'undefined' ? null : 180 * 1000,

I guess the error message could be more clear 馃槃

Agreed - had it not been for @scf4's comment, I likely would've torn my hair out for ages on this.

I just want to add another use case for this:
We have an expensive query that, on page load, we would like to fetch, but on any subsequent requests, we want to grab it from the cache. So we use cache-first.
However this value can be updated by other users on the system, it's just a count so it's not _that_ important that it's up-to-date, but, we want it to update from the server every five minutes.
It's not that weird of a case, so it seems odd that it wouldn't be supported.

@helfer we are building a react native app and the workflow of hitting the cache while using a poll interval to update that cache seems like a standard use case to me. Are there any workarounds I'm not thinking of? Our app is for a sport event series where we are displaying match statistics that are changing regularly throughout the app. We'd like to hit the backend for new data every 5 minutes to ensure data is up to date while still not hammering the backend during these high-traffic times.

Was this page helpful?
0 / 5 - 0 ratings