Delete an entry on client side

I am looking for the recommended way to delete an item and automatically update (modify or request again) all caches which might point to that item.

Let’s say for example I would like to delete the user “Alex”.
Alex wrote a blog post, liked another post, wrote a comment and is one of the most active users:

┌───────────────────────────────────────────────────────┐
│                                                       │
│   My fancy Blog                                       │
│                                                       │
│                                                       │
│  ┌──────────────────────────┐   ┌───────────────────┐ │
│  │                          │   │ Most active users │ │
│  │ My first Post by "Alex"  │   ├───────────────────┤ │
│  │                          │   │                   │ │
│  │                          │   │ "Alex"            │ │
│  └──────────────────────────┘   │                   │ │
│                                 │ Joe               │ │
│  ┌──────────────────────────┐   │                   │ │
│  │                          │   └───────────────────┘ │
│  │ Another post by Joe.     │                         │
│  │                          │                         │
│  │                          │                         │
│  ├┬────────────────────────┬┤                         │
│  ││ 1 like (by "Alex")     ││                         │
│  └┴────────────────────────┴┘                         │
│                                                       │
│  ┌──────────────────────────┐                         │
│  │                          │                         │
│  │ Another post by Sue.     │                         │
│  │                          │                         │
│  │                          │                         │
│  ├┬────────────────────────┬┤                         │
│  ││ 1 like (by Joe))       ││                         │
│  │┼────────────────────────┼│                         │
│  ││                        ││                         │
│  ││ Comment by "Alex"      ││                         │
│  ││                        ││                         │
│  ││ Thanks for sharing!    ││                         │
│  ││                        ││                         │
│  └┴────────────────────────┴┘                         │
│                                                       │
└───────────────────────────────────────────────────────┘

Now for some unknown reason the user Alex has to be deleted - is there a simple way to remove “Alex” and update all parts of the page?

Take a look at the cache.evict() function. You can evict User:Alex from the cache, and then all currently mounted queries which reference Alex will automatically refetch once their data becomes incomplete (unless you have returnPartialData: true on your queries). The downside to this approach is that all of your queries will return undefined while refetching, so pretty much everything on the screen will temporarily disappear. If you would like to leverage the cache instead of refetching then you’ll need to add some specific logic to each individual field where Alex might be referenced.

1 Like