Document how to batch RTK Query mutations properly
See original GitHub issueI feel that the documentation needs to cover the following scenario.
I have an array of objects that I need to send to the API. The API only allows mutating one object per request. What is a good way to do this while:
- avoiding a race condition where the final cache invalidation is ignored because the second to last cache provide comes later.
- even better, firing just one cache invalidation after all mutations have been sent.
I understand mutations can probably use a queryFn
to do this, but the docs only cover how to do multiple fetches.
Issue Analytics
- State:
- Created a year ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Mutations | Redux Toolkit
RTK Query provides an option to share results across mutation hook instances using the fixedCacheKey option. Any useMutation hooks with the same ...
Read more >What is the recommended way to perform multiple mutations ...
Ultimately using unwrap made the trick. const performMutipleMuations = async () => { const data= await updateProfile(newData).unwrap(); ...
Read more >RTK Query Best Practices - Medium
- How to refetch data / invalidate cache properly? · refetch : A function returned by the query hooks. · initiate : A...
Read more >Multiple queries with RTK Query (possible solution) : r/reactjs
It's a 3rd party API, and I couldn't find any endpoint to request all or in a batch.
Read more >Insert, update, and delete data using mutations | Cloud Spanner
You write data using the InsertMutationBuilder() function. Client::Commit() adds new rows to a table. All inserts in a single batch are applied atomically....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks, that helped. I was able to get it working using this:
I still think it would be good to show this in the docs, though.
@phryneas just wanted to add that this is not a good solution when using an auto generated RTK api file. manually adding queries in this use case is beating the purpose. RTK Query needs a proper way to batch multiple mutations and merge their respective invalidation tags when they’re both done. otherwise there are use cases where race conditions occur.