According to the docs on local state management, one can use both writeData and writeQuery for appending data to the cache.
What are the best practices here? When to one over the other?
From the documentation:
To write the data to the cache, you can use either
cache.writeQueryorcache.writeData. The only difference between the two is thatcache.writeQueryrequires that you pass in a query to validate that the shape of the data you're writing to the cache is the same as the shape of the data required by the query. Under the hood,cache.writeDataautomatically constructs a query from thedataobject you pass in and callscache.writeQuery.
The only difference is: do you construct the query that is passed (by calling cache.writeQuery directly), or do you let Apollo handle it (by calling cache.writeData)?
From looking at the source of cache.writeData on GitHub, it seems that Apollo will perform some analysis on the data you give it to determine the best way to use it, while cache.writeQuery will straight up take the query you pass it and use that.
So, to summarise:
cache.writeQuery
cache.writeData
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With