apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

How can i filter an array of references after calling cache.evict?

Open kyeo76 opened this issue 3 years ago • 3 comments

For example i have a cache that looks like that:

Item:1: {
  id: '1',
  title: 'item 1',
  __typename: 'Item'
},
Item:2: {
  id: '2',
  title: 'item 2',
  __typename: 'Item'
},
User:1: {
  id: '1',
  items: [{ __ref: 'Item:1' }, { __ref: 'Item:2' }],
  __typename: 'User'
}

After calling cache.evict({ id: 'Item:1' }) (and cache.gc() after eviction) the Item:1 object is being evicted as expected but the array of references still includes Item:1 in the items field inside User:1

Item:2: {
  id: '2',
  title: 'item 2',
  __typename: 'Item'
},
User:1: {
  id: '1',
  items: [{ __ref: 'Item:1' }, { __ref: 'Item:2' }],
  __typename: 'User'
}

how can i filter items field of evicted references using typePolicies? i'm using "@apollo/client": "^3.3.21".

kyeo76 avatar Jul 26 '21 19:07 kyeo76

the only solution i came up with looks like this. am i doing something wrong?

cache.evict({ id: 'Item:1' });
cache.modify({
  id: 'User:',
  fields: {
    items: (items, { canRead }) => items.filter(canRead),
  }
});
cache.gc();

kyeo76 avatar Jul 26 '21 20:07 kyeo76

have the same problem

ghost avatar Nov 26 '21 13:11 ghost

I have the same problem. However, if you optimistically mutate an item and evict the item afterwards, the query to get the list of items will not include the optimistic version of the item since the referenced object has been evicted.

songhobby avatar Aug 23 '22 22:08 songhobby

I also still have the same issue hmm it would be better after evict and calling cache.gc() remove a ref even in array instead of using must calling filter ..!! version 3.3

is there any update for this?!

hoon2hooni avatar Jun 04 '23 07:06 hoon2hooni