0

I am working with rtk query and I have 2 different sources of data:

  1. ongoing websocket connection that updates cache with updateCachedData whenever relevant notification is received,
  2. http requests to the api to get an existing data from db

The cached data is store inside of the api > queries > endpointname > data.

When I made a particular api call I want to update the cached data with that result. updateCachedData is not available for the query.mutation so I am not sure how this can be achieved.

Also, should I keep the copy of that cached data inside of the normal slice?

Thanks

I've tried researching the subject but its unclear. Some people state it's a good idea to keep the copy of that data inside of the normal slice and update it there but then this data will be there indefinitely.

mnbxx
  • 55
  • 5

1 Answers1

0

Hmm, that sounds like here you are trying to use a mutation to keep data around for longer - and mutations are just not meant for that. A mutation is meant to make a change on the server, not more.

So I would assume that you are just using a mutation for something that should rather be a query here - if you want to trigger it a little bit later, maybe also a lazy query or a query with a skip option.

phry
  • 35,762
  • 5
  • 67
  • 81
  • Thanks for your reply! How can I then trigger multiple queries e.g. array.forEach -> query? Is there an easy way to do this? Also regarding the mutations my specific use case is a chat where I initially query all chats and then on websocket message I want to update the chat. Do you recomment using something like creatyEntityAdapter to normalise chat data and easly add messages or there is a more efficient technique? – mnbxx Apr 20 '22 at 13:12
  • Just call `useMyChannelQuery` in multiple components with different arguments? I would assume you are not displaying all your chat messages in the same place altogether? – phry Apr 20 '22 at 13:20
  • thanks for help this makes sense! just regarding the entity adapter is this a recommended practise when you want easily edit the state? – mnbxx Apr 20 '22 at 13:46
  • that really depends on how you structure it. if you are just pushing to the end, an array makes sense, if you also want to edit older items, entityAdapter could be a good idea – phry Apr 20 '22 at 13:59