Search code examples
cachingapollo-client

How to remove nested object form apollo cache?


Official doc says remove cache data using cache.evict(), but there are no explanation about nested object. For example, let's think abount ROOT_QUERY like below.

ROOT_QUERY: {
  analysis: {
    receipts: { 
      ...
    }
  }
}

I can remove analysis field by using cache.evict().

cache.evict({ id: 'ROOT_QUERY', fieldName: 'analysis' });

But, how can I remove receipts field?


Solution

  • You can use cache.modify for nested object.

    cache.modify({
      id: 'ROOT_QUERY',
      fields: {
        analysis(existing) {
          return {
            ...existing,
            receipts: undefined
          };
        }
      }
    });