Skip to content

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

Closed
@BenJeau

Description

@BenJeau

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/openapi-react-query@0.2.3_@tanstack+react-query@5.59.16_react@18.3.1__openapi-fetch@0.13.0/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions