RTK Query - Custom Tag-Invalidation Request to use tag invalidation with merge

55 views Asked by At

I'm currently trying to implement some sort of infinity scrolling using RTK query. For that I'm using the merge parameter to merge incoming chunks into the old ones. Every time the user scrolls to the bottom of the page I execute the endpoint again using a different offset.

However, the user should be able to delete specific Items. When deleting Items, the cache gets invalidated. However the invalidation doesn't do anything because of two problems:

  1. The request made by RTK Query on invalidation uses the arguments of the last request. I would like to use my own ones to refetch the whole list and not only the last chunk (offset=0)
  2. The result of the request just gets merged into my old data and doesn't replace it. This leads to duplicate items etc. Would be cool to just clear everything and load again on tag invalidation.

Is there a solution for this? Any help would be appreciated!

These are my two endpoints:

getSubjectIDChunk: build.query({
    query: ({offset, limit}) => ({url: `someUrl`, params: {offset: offset, limit: limit}}),
    transformResponse: (response) => response.data,
    merge: (currentCache, newIds, {arg}) => {
        for (let i = 0; i < newIds.length; i++) {
            currentCache[arg.offset+i] = newIds[i];
        }
    },
    providesTags: ["List"],
    serializeQueryArgs: ({ queryArgs }) => ({})
}),
deleteSubjectData: build.mutation({
    query: ({id}) => ({
        url: `someUrl/${id}`,
        method: "DELETE"
    }),
    invalidatesTags: ["List"]
})

Tag Invalidation should refetch the whole list and not only the last queried chunk.

0

There are 0 answers