question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document how to batch RTK Query mutations properly

See original GitHub issue

I 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:open
  • Created a year ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
torhovlandcommented, Apr 5, 2022

Thanks, that helped. I was able to get it working using this:

    updateAccessLevelRequests: builder.mutation<number, AccessLevelRequest[]>({
      async queryFn(accessLevelRequests, _queryApi, _extraOptions, baseQuery) {
        for (const accessLevelRequest of accessLevelRequests) {
          await baseQuery({
            url: `access-level-requests/${accessLevelRequest.id}/`,
            method: 'PUT',
            body: accessLevelRequest,
          });
        }
        return { data: accessLevelRequests.length };
      },
      invalidatesTags: ['AccessRequests'],
    }),

I still think it would be good to show this in the docs, though.

1reaction
giladvcommented, Jul 3, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found