Set automated invalidation in the mutation definition, but skip the invalidation for the focused query #2361
Replies: 3 comments
-
In short, I guess the answer for now would be a "no", just because adding something such as this is a lot of extra code and logic and I don't think the use case is actually as common. Instead, honestly, I would focus on this part:
And here I have to ask: why is the whole page refreshing? Usually something like a refetch would happen pretty "unnoticed by the user" - are you intentionally making it very visible to the user due to some reason? |
Beta Was this translation helpful? Give feedback.
-
While I say the whole page is refreshing, I would mean the data table / data list is refreshing.
Yes. There is a progress bar and text indicator that tells the user <ProgressBar
id="progress-bar"
show={responseGetAuthusers.isFetching}
/>
Actually it is very common. Think about the favorite button for the menu; favorite button for the tweet; pin button for the product; like button for the song; upvote/downvote for the comment; disable/enable checkbox for the user etc. in a list / data table of menus, tweets, products, songs, comments, users etc.. The problem is that when you update only a field of a record in the list / table by clicking an icon-button or a checkbox, this mutation requires existing query is being re-fetched when we use automated invalidation, either you show the progress indicators to the users or not; this eventually occurs. If you use In order to prevent the whole list / table is being re-fetched as a result of a piece of data is updated,
Yes, it may require a lot of extra code and logic. You are the author and you know better than us. But I think that the requested feature is so powerful for the common use cases, and is going to give a better experience for the developers who use the RTK Query package. Thank you for the quick response. |
Beta Was this translation helpful? Give feedback.
-
We have a similar situation as explained by @talatkuyuk . Our use case is that we have a query which pulls data to display in a Grid. Inside the grid we have a star/unstar button. On pressing Star, the mutation is fired which also invalidates the tag, hence the original get query also fires up and users see loading spinner and page refresh, which we don't want. As we are using RTK query auto generated code from our swagger, and our queries are auto generated, we don't want to manually change it every time. Our workaround was to opt out of that particular query and override it like: In generated code:
Override it in another file: (Just remove providesTags)
Its not at all ideal, as we have to copy paste the query and remove providesTags, (we can remove it from swagger also, but it will not trigger in all cases) Definitely this feature is required and having a flag to opt out of providesTag, something like:
and same for Mutations |
Beta Was this translation helpful? Give feedback.
-
I have a page that shows the
Authusers
in a list. RTK Query provides the data with the endpoint "getAuthusers
". Each row in the table represents the record (Authuser
). The page has filter form that can request the data coming from the API, using field filters and pagination (pageIndex, pageSize, pageCount) etc. So, the RTK Query caches all the queries result with the filtered arguments. So far so good.I implement a user interface that
each Authuser row
has checkboxes for the fieldsisDisabled
andisEmailVerified
. The checkboex represents the data accordingly, checked or not. Whenever the user clicks a checkbox, an API request goes to server and update the data in the database (simply toggles the field data as true to false; or false to true), return only {success: true} response. It uses a RTK Query mutation for that process.Now, I have two options
1. Use Automated Re-fetching with the tags: This cause that related query caches are invalidated and re-fetched. It is the most simplest approach. I implement this, and works perfectly, but cause less User Experience since even I change a small piece of data, the existing query is triggered and the whole page is refreshing.
selectInvalidatedBy
, it is so error-prone because you have to update the record (Authuser) in some query caches, or you have to delete the record in some query caches, or you have to add it into some query caches; and think that the query caches have different arguments, filter fields, pagination and different sorting arguments. It is almost impossible how to decide in which order you add the data for example. As a result, the optimistic/pessimistic update could be so error-prone especially for the pages having a data table with a server-side filter form.At this point, I would like to say:
Here is my implementation for the pessimistic cache update for the focused query:
For conclusion, what I would like to do: I would like to provide both
invalidatesTags
(for automated re-fetching) andonQueryStarted
(for the focused query) at the same time, requesting from RTK Query to not do re-fetching for the focused query and skip this.My suggestion would be like below.
for the focused query
.I suppose, the use case is so suitable for all the developers. All of us may encounter the use case that I've tried to explain above.
Is there any developer has a work-around for the use case?
Beta Was this translation helpful? Give feedback.
All reactions