Apollo client refetch inactive queries

Viewed 1116

I'm having a query that fetches a list with pagination. If I use cache-and-network, apollo first gets the cached list items, then gets the first page. It results in a flashing screen. If I use network-only, it stops, but every time I navigate, I get the first page of the list. I want to use the cached list. With cache-first it works fine.

However, when I'm trying to add a new item to the list, I obviously want to refetch it. Adding a new list item is in a separate component, and the list component is not rendered, and the list query isn't active.

Once the mutation promise resolves, I redirect to the list component, but it seems like refetchQueries runs before that.

I could make the list component be mounted to make the query active, but there must be an easier way to solve this - I mean it should be a common expected behaviour. Otherwise it would require to go with network-only, or implement some sort of state to know if a query should be refetched or not.

1 Answers

I just took a look at the apollo graphql document. There are twos functions (refetchQueries, especially reFetchObservableQueries) allow us to refresh active query. It might help you to solve your challenge.

You can access to these functions by using the useApolloClient hook inside your component.

You also can refresh standby query by using includeStandby option. Takes optional parameter includeStandby which will include queries in standby-mode when refetching.

https://www.apollographql.com/docs/react/api/core/ApolloClient/

Related