Claude Code CLAUDE.md template for react-query.gen #2631
Replies: 5 comments
-
There aren’t any public CLAUDE.md or AGENT.md templates specifically for react-query.gen, but you can reduce LLM hallucinations by giving Claude explicit, example-driven instructions based on the actual generated hooks and usage patterns. For best results, include concrete code snippets showing how to use the generated hooks, clarify the naming conventions (e.g. # Claude Code Instructions for react-query.gen
When generating code for data fetching or mutations, use the hooks and functions generated by openapi-ts with the @tanstack/react-query plugin.
## Imports
Import hooks and functions directly from the generated client, e.g.:
import { useQuery, useMutation, getPetByIdInfiniteQueryKey, addPetMutation } from 'src/client';
## useQuery Example
const { data, error } = useQuery({
...getPetByIdInfiniteQueryKey({
path: { petId: 1 }
}),
});
## useMutation Example
const addPet = useMutation({
...addPetMutation(),
onError: (error) => { console.log(error); },
});
addPet.mutate({
body: { name: 'Kitty' }
});
## Guidelines
- Always use the generated functions for query keys and mutations.
- Pass parameters as objects with `path`, `body`, etc., matching the OpenAPI schema.
- Infer response types from the generated function signatures. This approach helps Claude avoid guessing imports or parameter structures and instead rely on the actual generated API. For more details and examples, see the plugin documentation. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
oops, this could probably go into Discussions rather than GitHub issues |
Beta Was this translation helpful? Give feedback.
-
Do you think this is something that should be provided by this package? |
Beta Was this translation helpful? Give feedback.
-
that's interesting/bold. It would be neat to have it generated for sure as an optional flag (maybe on by default?)! |
Beta Was this translation helpful? Give feedback.
-
Definitely not enabled by default, but I don't see why it wouldn't make sense as a feature request. Who else has all the context to create this file if not us! As long as we know what a good format looks like and don't introduce breaking changes somehow, this all sounds reasonable to me. What would happen if TanStack Query output lives scattered across multiple files? What if there are other, unrelated functions in those files? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Does anyone have a CLAUDE.md/AGENT.md file that helps their Claude Code properly write out useQuery and useMutation? Currently, my LLMs are hallucinating like crazy, importing the wrong code (see screenshot here upstash/context7#566 (comment)), unable to interpret the options (such as path parameters, body types, etc.), and unable to infer the response type either.
What I've written so far:
Using the API
Use Tanstack Query v5 for all API calls. You'll typically import from "@/services/client/@tanstack/react-query.gen"
To get what options to put in the body of mutations, you can look at the function parameters
such as
options
which will lead you to a file calledtypes.gen.ts
.To perform a GET request, you can use the
useQuery
anduseMutation
hook. For example, to fetch user data:An example of identifying the necessary types for passing in the parameters or body for a mutation:
DeleteApiV1FriendByIdDeleteSentInviteData
is intypes.gen.ts
and includes the path parametersin the path property. The body is also typically located in the body property for mutations.
The response object can be used for typing throughout the app (though with autocompletion, it's
not necessary to import the type unless it's necessary for a function signature).
DeleteApiV1FriendByIdDeleteSentInviteResponse
in this case leads to:Beta Was this translation helpful? Give feedback.
All reactions