I have 2 endpoints. One for fetching a list of users (with all related data), and one for just fetching one of them. I would like to be able to share cached data between these 2 endpoints.
I need this because I have 2 pages, one with the list of users, and one that displays the data of a given user where I would like to be able to reuse the data that's already in the list, or in case it's not there, just fetch the data I need.
Here are the two endpoints:
getTeachers: builder.query({
query: ({ page, orderBy, orderDirection }) =>
buildQueryWithParams('teachers', {
limit: bigPaginationLimit,
offset: (page - 1) * bigPaginationLimit,
orderBy,
orderDirection
}),
providesTags: (res) =>
res.list
? [
res.list.map((item) => ({
type: 'teacher',
id: item.id
}))
]
: [{ type: 'teacher', id: 'list' }]
getTeacher: builder.query({
query: (id) => `/teachers/${id}`,
providesTags: (res, error, id) => [{ type: 'teacher', id }]
})
I was hoping that giving the objects the same tags would make it so that they share a cache, but I've since found out that RTK Query doesn't use the tags for the cache.
Is there any way of sharing caches between these two endpoints?
If not, how could I go about using the data from the list if it's there, and otherwise just fetching the single user.