Why have per-item tags, if edit causes the entire list to re-fetch #5009
-
Hi all, I'm just getting familiar with RTK Query, currently going through this part https://redux.js.org/tutorials/essentials/part-8-rtk-query-advanced#invalidating-specific-items and following along with my own project. I've got a For example: const itemsApiSlice = baseApi.injectEndpoints({
endpoints: (builder) => ({
// GET /api/items - Get all items
getItems: builder.query<ApiResponse<Item[]>, void>({
query: () => "/items",
// Per-item tags, allows individual item invalidation
// See more: https://redux.js.org/tutorials/essentials/part-8-rtk-query-advanced#invalidating-specific-items
providesTags: (result, _error, _arg) => [
SLICE_ENTITY_TYPES.Item,
...result?.data?.map(({ id }) => ({ type: SLICE_ENTITY_TYPES.Item, id })) ?? []
]
}),
// GET /api/items/:id - Get single item
getItem: builder.query<ApiResponse<Item>, string>({
query: (id) => `/items/${id}`,
providesTags: (_result, _error, id) => [{ type: SLICE_ENTITY_TYPES.Item, id }],
}),
// POST /api/items - Create new item
createItem: builder.mutation<ApiResponse<Item>, CreateItemRequest>({
query: (newItem) => ({
url: "/items",
method: "POST",
body: newItem,
}),
// Invalidate the entire list to refetch after creating
invalidatesTags: [{ type: SLICE_ENTITY_TYPES.Item }],
}),
// PUT /api/items/:id - Update existing item
updateItem: builder.mutation<ApiResponse<Item>, { id: string; updates: UpdateItemRequest }>({
query: ({ id, updates }) => ({
url: `/items/${id}`,
method: "PUT",
body: updates,
}),
// Invalidate both the specific item and the ENTIRE LIST
// See more: https://redux.js.org/tutorials/essentials/part-8-rtk-query-advanced#invalidating-specific-items
// ??? Why do we want per-item tags in getItems then? What's the point? ???
invalidatesTags: (_result, _error, { id }) => [
{ type: SLICE_ENTITY_TYPES.Item, id },
],
}),
// ...
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The job of RTK Query is to deal with all kinds of REST endpoints - and most REST endpoints are designed in a way that doesn't lend itself to things like normalization, so something like a "partial refetch" is impossible. Things like normalization work better in a client that can make very specific assumptions about the API, e.g. a GraphQL client like Apollo Client - and in those cases you can also do selective cache updates and/or refetches. But a general API client like RTK Query cannot make any of those assumptions, so it has to fall back to the most common behavior. So the point of per-item tags is really just so you can identify which cache entries will need refetching if something specific changes. |
Beta Was this translation helpful? Give feedback.
The job of RTK Query is to deal with all kinds of REST endpoints - and most REST endpoints are designed in a way that doesn't lend itself to things like normalization, so something like a "partial refetch" is impossible.
It's a deliberate design choice that RTK Query only tries to mirror the state of the server, and refetches endpoints it knows are outdated in the browser.
Things like normalization work better in a client that can make very specific assumptions about the API, e.g. a GraphQL client like Apollo Client - and in those cases you can also do selective cache updates and/or refetches.
But a general API client like RTK Query cannot make any of those assumptions, so it has to fall …