reduxredux-toolkitrtk-query

RTK Query invalidateTags only after mutation "success"


Hello am using RTK query for API interaction but am facing some error, where I trigger mutation endpoint invalidation always executed if its error or success.

what i am searching for is Invalidate Tags on mutation success here is my code

Get Endpoints

getPosts: builder.query({
      query: () => `/posts`,
      providesTags: ['Posts'],
      transformResponse: (response, meta, arg) => response.data,
      transformErrorResponse: (response, meta, arg) =>  response.data?.message ?? response ,
    })

mutation endpoint

mutatePost: builder.mutation({   
      query: (data,id) => ({
        url: id?`/posts/${id}`: `/posts`,
        method: id?'PATCH':'POST',
        body: data,
      }),
    
      transformResponse: (response, meta, arg) => response.data,
      transformErrorResponse: (response, meta, arg) => response.data?.message ?? response , 
      invalidatesTags: ['Posts'],

    })

i tried all results available but not fit for my expection


Solution

  • You can use the callback notation

    invalidatesTags: (result, error) => error ? [] : ['Posts'],