skip to Main Content

I have 2 api reducers,boardApi and taskApi.

boardApi consist of 3 query getBoards , getBoardById, & addBoard.

taskApi has 2 mutation, addTask & deleteTask.

how to refetch getBoardById every time task added or deleted

boardApi

export const apiBoardSlice = createApi({
  reducerPath: 'apiBoardSlice',
  baseQuery: fetchBaseQuery({
    baseUrl: API_URL,
    prepareHeaders: headers => headers.set('Authorization', `Bearer ${token}`)
  }),
  tagTypes: ['Boards'],
  endpoints: (builder) => ({
    getBoards: builder.query({
      query: () => 'board',
      providesTags: ['Boards'],
    }),
    getBoardById: builder.query({
      query: (id) => `board/${id}`,
      providesTags: ['Boards'],
    }),
    addBoard: builder.mutation({
      query: payload => ({
        url: 'board',
        method: 'POST',
        body: payload,
      }),
      invalidatesTags: ['Boards'],
    }),
  }),
})

task api

export const apiTaskSlice = createApi({
  reducerPath: 'apiTaskSlice',
  baseQuery: fetchBaseQuery({
    baseUrl: API_URL,
    prepareHeaders: headers => headers.set('Authorization', `Bearer ${token}`)
  }),
  endpoints: (builder) => ({
    addCard: builder.mutation({
      query: ({ listId, title, boardId }) => ({
        url: `task/${boardId}`,
        method: 'POST',
        body: { title, listId },
      }),
    }),
  }),
})

2

Answers


  1. Those should not be two apis. Generally, there are very few situations where you ever have two different apis in your application – this is not one of them.

    Both even have the same base url and application logic – they should be multiple endpoints on the same api.

    As for your question: you cannot do automatic cross-api invalidation. If you want to invalidate one endpoint from another api, it’s a clear sign that both endpoints belong onto the same api.

    Login or Signup to reply.
  2. Don’t use createApi twice.

    With combineReducers
    you can use all declared tags in all combined reducers.

    So you can simply use in both task mutations:

    invalidatesTags: ['Boards']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search