Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for arbitrary properties in RTK Query tags #4399

Open
kcrwfrd opened this issue May 7, 2024 · 0 comments
Open

Support for arbitrary properties in RTK Query tags #4399

kcrwfrd opened this issue May 7, 2024 · 0 comments

Comments

@kcrwfrd
Copy link

kcrwfrd commented May 7, 2024

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 }])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant