reduxjs/redux-toolkit

Support for arbitrary properties in RTK Query tags

kcrwfrd opened this issue · 0 comments

One feature that I like about Tanstack Query is that their equivalent for tags ("query keys") have support for arbitrary properties.

This would give us increased flexibility and granularity for invalidating tags. Imagine if we had a Posts tag type for an endpoint with an authorId query arg.

posts: builder.query<
  { id: string; authorId: string; body: string }[],
  string
>({
  query: (authorId) => `/author/${authorId}/posts`,
  providesTags: (result, _error, authorId) =>
    result
      ? result
          .map(({ id }) => ({ type: 'Posts', id }))
          .concat([
            {
              type: 'Posts',
              authorId,
            },
          ])
      : [{ type: 'Posts', authorId }],
}),

Then we could have some enhanced ergonomics for selecting which queries we want to invalidate

// All posts
apiSlice.util.invalidateTags(['Posts'])

// Only the posts for a specific author
apiSlice.util.invalidateTags([{ type: 'Posts', authorId }])

// Invalidate whatever query cache(s) a specific post belongs to
apiSlice.util.invalidateTags([{ type: 'Posts', id }])

This could perhaps afford for some cleaner implementations addressed by abstract tag IDs or tags to support pagination.

Especially imagine if that posts endpoint was paginated:

// Invalidate page 1 of posts for a specific author
apiSlice.util.invalidateTags([{ type: 'Posts', authorId, page: 1 }])