How to merge cached values using RTK query?

24 Views Asked by At

I have an endpoint which depending on the request parameter may return only part of data.

Request:

{
    name: string,
    requestC: boolean
}

And depending on requestC, response may vary:

{
    a: "Lorem",
    b: "ipsum",
    c: "dolor"
}

{
    a: "Lorem",
    b: "ipsum"
}

I want to be able to merge values in cache so I don't have to always fetch with requestC. I tried doing it like this:

export const searchApi = createApi({
    baseQuery: fetchBaseQuery({baseUrl: '/'}),
    tagTypes: ['/abc'],
    endpoints: (build) => ({
        getABC: build.query({
            query: (body) => ({
                url: '/api/abc',
                method: 'POST',
                body,
            }),
            merge(currentCacheData, responseData) {
                return  {
                    ...currentCacheData,
                    a: responseData.a,
                    b: responseData.b
                }
            },
            serializeQueryArgs: ({ queryArgs }) => {
                const { requestC, ...significantFields } = queryArgs;
                return significantFields;
            },
            providesTags: (result) => result ? [{id: 'LIST', type: '/abc'}] : []
        }),
    }),
});

But it doesn't seem like merge is being called at all.

0

There are 0 best solutions below