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 queryKey parameters should be optional since undefined is already handled #1840

Open
ritarock opened this issue Jan 21, 2025 · 1 comment
Labels
tanstack-query TanStack Query related issue

Comments

@ritarock
Copy link

ritarock commented Jan 21, 2025

Problem

The generated queryKey function requires parameters to be mandatory when the type has required fields, even though:

  1. The function already handles undefined cases internally with ...(params ? [params] : [])
  2. We need to pass undefined when invalidating TanStack Query cache

Current Behavior

export type ListPetsParams = {
  limit: string; // required
};

// Generated code - params is required despite handling undefined
export const getListPetsQueryKey = (params: ListPetsParams) => {
  return [`http://localhost:8000/pets`, ...(params ? [params] : [])] as const;
};

// Cannot invalidate cache like this
queryClient.invalidateQueries(getListPetsQueryKey());

Current Workaround

We are currently using type assertion as a workaround:

queryClient.invalidateQueries(getListPetsQueryKey({} as ListPetsParams));

However, this is not ideal as it bypasses TypeScript's type checking and could lead to runtime errors.

Expected Behavior

Parameters should be optional since undefined is handled:

export const getListPetsQueryKey = (params?: ListPetsParams) => {
  return [`http://localhost:8000/pets`, ...(params ? [params] : [])] as const;
};

// Should be able to invalidate cache
queryClient.invalidateQueries(getListPetsQueryKey());

orval version: "7.4.1"
Node.js version: "v22.13.0"
pnpm version: "9.12.2"

@melloware melloware added the tanstack-query TanStack Query related issue label Jan 21, 2025
@oTkPoBeHuE
Copy link

oTkPoBeHuE commented Jan 30, 2025

Could you please let me know when the issue might be fixed?
It is significantly affecting my work. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tanstack-query TanStack Query related issue
Projects
None yet
Development

No branches or pull requests

3 participants