Skip to content

Inconsistent cache key - queryOptions requires third parameter and not for useQuery/useSuspenceQuery #1972

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

Closed
2 tasks done
BenJeau opened this issue Oct 26, 2024 · 1 comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@BenJeau
Copy link

BenJeau commented Oct 26, 2024

Description

I'm using this library in conjunction with @tanstack/router and I'm using their loaders to load data at the route level. This works, but the API is different based on if I use useQuery/useSuspenceQuery vs queryOptions.

src/routes/index.tsx:12:37 - error TS2554: Expected 3-4 arguments, but got 2.

12     queryClient.ensureQueryData(api.queryOptions("get", "/users")),
                                       ~~~~~~~~~~~~

  node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/openapi-react-query/dist/index.d.ts:8:494
    8 export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>, Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, Options extends Omit<UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>, "queryKey" | "queryFn">>(method: Method, path: Path, ...[init, options]: RequiredKeysOf<Init> extends never ? [InitWithUnknowns<Init>?, Options?] : [InitWithUnknowns<Init>, Options?]) => UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>;

Ultimately, I need both to have the same third parameter for the query cache key to be the same, because if I add a third parameter in the queryOptions such as {} and only have two parameters for my useQuery/useSuspenceQuery, I will have two different query cache keys, e.g.:

Screenshot 2024-10-26 at 10 05 01 AM

And if I don't add a third parameter to queryOptions:

Screenshot 2024-10-26 at 10 05 31 AM
Name Version
openapi-fetch 0.13.0
openapi-react-query 0.2.3
@tanstack/react-query 5.59.16
Node.js 20.11.0
OS + version macOS 15.0.1

Reproduction

For example, I have the following code snippet:

import { createFileRoute } from "@tanstack/react-router";
import { api } from "@/api";

const IndexComponent: React.FC = () => {
  const users = api.useSuspenseQuery("get", "/users");
  return <pre>{JSON.stringify(users.data, null, 4)}</pre>;
};

export const Route = createFileRoute("/")({
  component: IndexComponent,
  loader: async ({ context: { queryClient } }) =>
    queryClient.ensureQueryData(api.queryOptions("get", "/users")),
});

Which gives the error above, adding a third parameter to queryOptions satisfies TypeScript, but creates duplicate cache keys.

Expected result

To not need to provide a third parameter to queryOptions to be consistent to with useQuery/useSuspenceQuery

Checklist

  • My OpenAPI schema passes the Redocly validator (npx @redocly/cli@latest lint)
  • I’m willing to open a PR (see CONTRIBUTING.md) - but I'm unsure how to fix this...

Upon looking further, I see that queryOptions is being used for useQuery/useSuspenceQuery, which makes sense, and are dismissing the TS error by casting the type of the third parameter via as InitWithUnknowns<typeof init> - https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-react-query/src/index.ts#L135

@BenJeau BenJeau added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Oct 26, 2024
@BenJeau BenJeau changed the title Inconsistent cache key - queryOptions requires third parameter while it isn't for useQuery/useSuspenceQuery Inconsistent cache key - queryOptions requires third parameter and not for useQuery/useSuspenceQuery Oct 26, 2024
@BenJeau
Copy link
Author

BenJeau commented Oct 26, 2024

Just saw that there's a new version of openapi-react-query which fixes this issue - #1952 fixes this

@BenJeau BenJeau closed this as completed Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant