Search code examples
reactjsgraphqlreact-apolloapollo-cache-inmemory

How can I avoid "auto-update" cache when using `react-apollo-hooks` and `useSubscription` hook


I have some Apollo-Hooks code that uses useSubscription to listen for event changes in a subscription:

useSubscription<MySubscriptionUpdated>(MySubscription, {
    onSubscriptionData: async ({ client, subscriptionData: { data } }) => {
      if (!data) {
        return;
      }
      ...

This code automatically updates the cache on the response, which is great in most circumstances

However, I need to do some result-processing after the response is received, yet prior to the cache being updated.

Does anyone know of a way to use useSubscription hook, and not have the cache be automatically updated?

The response will ultimately always have an entity with __typename in it.


Solution

  • You can change fetchPolicy for each subscription. The default value is cache-first. To disable cache must set fetchPolicy to no-cache. For get more detail see apollo official document.

    useSubscription<MySubscriptionUpdated>(MySubscription, {
        fetchPolicy: "no-cache",
        onSubscriptionData: async ({ client, subscriptionData: { data } }) => {
          if (!data) {
            return;
          }
          ...