Skip to content

About

TanStack Query codegen without OpenAPI: generates query keys and queryOptions, infiniteQueryOptions and mutationOptions from your existing TypeScript API functions. Try it in the browser playground; watch mode and a CI check included.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

React Query Helper logo

React Query Helper

TanStack Query codegen without OpenAPI: a query key factory and queryOptions generator for the TypeScript API functions you already have

npm version License CI status on main TanStack Query peer dependency range Node.js engine requirement Bundled TypeScript types

Playground · Architecture · npm · Changelog · 한국어

React Query Helper is a TanStack Query (React Query) v5 code generator: it reads your existing TypeScript API functions and writes the query keys and queryOptions, infiniteQueryOptions and mutationOptions factories for them. No OpenAPI or Swagger spec needed, and your fetch or axios layer stays as it is. When the API changes, watch regenerates the files and generate --check catches stale output in CI.

Typing an API function into the playground on the left; the generated query key and option factories update on the right

Try it in the browser → Paste an API function and see the generated keys and options. Nothing to install.

Version notice: This README describes 2.x. Check the npm badge above for the published version. If you use 1.x, read the 1.3.0 docs and migration guide. To try changes not yet published, install from source.

Why React Query Helper?

TanStack Query already provides queryOptions for reusable, type-safe query definitions. But you still connect each API function to those options by hand:

export const userOptions = (id: string) =>
  queryOptions({ queryKey: ["users", id], queryFn: () => getUser(id) });

Across hundreds of API functions and several developers, key naming, the link between functions and options, and keeping them in sync when arguments change become repetitive work. React Query Helper fixes that connection in a generator. TanStack Query still handles caching, retries and fetching; this tool generates only the code that connects your API to it.

How it differs

  • Your API layer stays. Generated files go to outputDir and only import your API functions, so existing fetch or axios wrappers with auth headers, interceptors, error handling and response transforms, and your business logic, stay as they are. Orval, Kubb and Hey API generate the API functions themselves from an OpenAPI spec, so hand-written API functions are replaced by generated ones (you can plug in your own fetch instance).
  • No spec required. The input is the TypeScript functions you already have, including projects whose OpenAPI or Swagger spec is missing or out of date.
  • One key convention, automatically. getUser in libs/users/api.ts becomes ["users", "api", "getUser"], so developers stop inventing their own key names. @lukemorales/query-key-factory needs the key structure declared by hand; this tool also generates the option factories.
  • No repeated type declarations. Arguments, return values and select results are inferred from the original function.
  • Generated code is ordinary TypeScript. Real .ts files, not runtime API discovery, so they show up in code review and you can jump to definitions in your IDE. There is no extra cache or query engine.
  • API and query layer stay in sync. watch regenerates on API changes, and generate --check catches stale generated files in CI.
  • Adapts to your project. Choose artifacts per function (artifactsByName), or replace the analyzer (customAnalyzerPath) and template (customTemplatePath).

Made for projects like these

  • An existing fetch or axios API layer with auth and error handling
  • A backend without an OpenAPI spec, or with one that does not match reality
  • Many API functions and several developers who need one query key convention
  • Teams standardizing TanStack Query usage without rewriting existing code

What works now

  • generate: for each top-level function in sourceDir, writes a key constant and query, infinite query and mutation option factories to *Options.ts.
  • The function-name prefix picks the artifacts: get, list, fetch and similar get query and infinite query; create, update, delete and similar get a mutation; other names get query and mutation. artifactsByName and enabledArtifacts set them explicitly.
  • Infinite query options take their paging rules from withOptions (initialPageParam, getNextPageParam); without them only the first page loads.
  • Mutations of functions with two or more arguments get a variables object type and mapVariablesToArgs.
  • withOptions overrides staleTime, select and others, with argument, return and select result types inferred.
  • generate --check writes nothing and exits with code 1 when output is missing, changed or stale.
  • watch regenerates when sources, the config or listed local plugins change.
  • An ownership header on the first line marks generated files; unmarked existing files are never overwritten.

Planned

  • Choose artifacts with JSDoc, such as /** @rqh mutation */ next to the function. Today artifactsByName in the config does this.
  • Infinite queries on request, generated only for functions that paginate. Today enabledArtifacts or artifactsByName turn them off.
  • Keys that survive file moves, with a namespace independent of the file path so refactoring does not change cache keys.
  • Wider analysis: re-export barrels, class and object methods, functions returned by higher-order functions.
  • Beyond React: TanStack Query for Vue, Solid and Svelte.
  • Optional OpenAPI input, generating new spec-based APIs and existing functions from one config.

Install and Use

npm install @uiwwsw/react-query-helper@^2 @tanstack/react-query react
npx @uiwwsw/react-query-helper init

Generated code imports this package, so install it as a regular dependency (do not use -D).

// rqh.config.ts
import type { AutoQueryConfig } from "@uiwwsw/react-query-helper/config";

export default {
  sourceDir: "./libs",
  outputDir: "./src/options",
  template: { artifactStrategy: "smart" },
} satisfies AutoQueryConfig;
// libs/users/api.ts
export const getUser = async (id: string) => ({ id, name: "Ada" });

Running npx @uiwwsw/react-query-helper generate writes src/options/users/apiOptions.ts.

// Generated by @uiwwsw/react-query-helper. DO NOT EDIT.
// Source root: "../../libs"
import { getUser } from "../../../libs/users/api";
import {
  queryOption as __rqh_query,
  infiniteOption as __rqh_infinite,
} from "@uiwwsw/react-query-helper";

export const getUserKey = ["users", "api", "getUser"] as const;
export const getUserQueryOption = __rqh_query(getUserKey, getUser);
export const getUserInfiniteQueryOption = __rqh_infinite(getUserKey, getUser);

Do not edit the generated file; change the source API instead. Usage:

// src/UserName.tsx
import { useQuery } from "@tanstack/react-query";
import { getUserQueryOption } from "./options/users/apiOptions";

export function UserName({ id }: { id: string }) {
  const user = useQuery(
    getUserQueryOption.withOptions(
      { staleTime: 60_000, select: (data) => data.name },
      id,
    ),
  );

  if (user.isPending) return <p>Loading...</p>;
  if (user.isError) return <p>{user.error.message}</p>;
  return <p>{user.data}</p>;
}
// With your existing QueryClient instance, client
await client.prefetchQuery(getUserQueryOption("u1"));
const cached = client.getQueryData(getUserQueryOption("u1").queryKey);
// { id: string; name: string } | undefined

API arguments become part of the cache key. Do not pass tokens or authorization headers as arguments.

Docs

License

MIT License

About

TanStack Query codegen without OpenAPI: generates query keys and queryOptions, infiniteQueryOptions and mutationOptions from your existing TypeScript API functions. Try it in the browser playground; watch mode and a CI check included.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages