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

Generated query key functions don't allow for granular query invalidation, especially with pagination #174

Open
mmurto opened this issue Nov 29, 2024 · 0 comments

Comments

@mmurto
Copy link
Contributor

mmurto commented Nov 29, 2024

I'm using the generated query key functions with an API that has paginated queries. Here's an example of one generated query key function from our project:

export type SecretsServiceGetSecretsByOrganizationIdQueryResult<
  TData = SecretsServiceGetSecretsByOrganizationIdDefaultResponse,
  TError = unknown,
> = UseQueryResult<TData, TError>;
export const useSecretsServiceGetSecretsByOrganizationIdKey =
  'SecretsServiceGetSecretsByOrganizationId';
export const UseSecretsServiceGetSecretsByOrganizationIdKeyFn = (
  {
    limit,
    offset,
    organizationId,
    sort,
  }: {
    limit?: number;
    offset?: number;
    organizationId: number;
    sort?: string;
  },
  queryKey?: Array<unknown>
) => [
  useSecretsServiceGetSecretsByOrganizationIdKey,
  ...(queryKey ?? [{ limit, offset, organizationId, sort }]),
];

As seen above, the query key generated by the functions is an array with two items like ['SecretsServiceGetSecretsByOrganizationId', { limit: 10, offset, 2, organizationId: 7 }].

The situation where this is unwanted is where I often want to invalidate queries using one of the parameters and disregarding all pagination parameters. With the generated query key function, I can't do that as the parameter I want to use for cache invalidation (organizationId) is in the same object as the pagination parameters.

Best format for generated query key for me would be something where the pagination parameters are separated from rest, which for the above query would be something like ['SecretsServiceGetSecretsByOrganizationId', 7, { limit: 10, offset, 2 }]. I believe this would require knowledge of the names of pagination parameters at generation time, so a flat list without objects would also work.

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