-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Hi, as discussed in #3248 , I integrated rtk-query with normy, a library, which allows automatic normalization and data updates for fetching libraries. There is already react-query and swr integration, now also rtk-query is supported. You can find it here - https://github.com/klis87/normy/tree/master/packages/normy-rtk-query#normyrtk-query
This is to solve problem mentioned here - https://redux-toolkit.js.org/rtk-query/comparison#no-normalized-or-deduplicated-cache and gives you apollo graphql like experience - relevant queries are automatically updated based on mutations results with no additional code.
I raised this issue not only as information, but also to mention several potential improvements, which could require some rtk-query changes
- the integration is implemented as middleware, which also listens to rtk-query actions. is it possible to get those action types somehow? for now I wrote them by hand - https://github.com/klis87/normy/blob/master/packages/normy-rtk-query/src/index.ts#L22 , but ofc it would be much better to reuse rtk-query types
- I could not manage to find a way to pass some meta attribute from query definition - like this - https://github.com/klis87/normy/blob/master/examples/rtk-query/src/api.js#L9 to the action here https://github.com/klis87/normy/blob/master/packages/normy-rtk-query/src/index.ts#L135 . This is problematic because without it I cannot allow an easy way to disable normalizations per specific queries and mutations. I added only global way as
normalizeQuery
andnormalizeMutation
options, but it is global, collocation style inside specific queries and mutations would be much better, like implemented in react-query addon - https://github.com/klis87/normy/tree/master/packages/normy-react-query#disabling-of-normalization-per-query-and-mutation-arrow_up - This library uses structural sharing, so if it works like I think, after each refetch it is checked that
data
is different. it would be cool to have this information in actions, so that middleware could give up normalization for a refetch which did not update data, which is a waste as it will end up with normalization store exactly the same as it was. This would be very nice optimization, especially that rtk-query has features like refetch on refocus, which potentially could cause multiple refetches at the same time.