David Avikasis
David Avikasis

Reputation: 636

Apollo: How to handle cache after graphql error?

I know how to update the cache after successful mutation (using update). I know how to handle the errors themselves (using try/catch and a global onError config on the client).

How can I update the cache after error in mutation? I need this because I'm handling per-field loading field inside the cache (as local @client fields), and I want to set them to false after the mutation has failed (and also return the previous value of the field if needed). Do you have a better concept to handling per-field loading indicator?

I'm currently using withApollo to get the client and then using client.cache.writeData to manually manipulate the cache. it kinda works but I'm having rendering issues - I can see the change in the console/developer tools, but not in the actual element, and after another change in the element it takes the updated loading value.

Is there another way?

Upvotes: 0

Views: 678

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84817

You should be using client.writeData instead of cache.writeData -- the latter does not trigger reactive updates in the UI while the former does. Once you migrate to version 3.0 of the client, you'll need to use writeQuery or writeFragment instead because writeData will be removed so you might consider changing to one of those two methods instead.

Upvotes: 1

Related Questions