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 called types.gen.ts.
To perform a GET request, you can use the useQuery and useMutation hook. For example, to fetch user data:
import {
getApiV1FeedFeedOptions,
postApiV1FriendFindFriendsMutation,
} from "@/services/client/@tanstack/react-query.gen"
import { useMutation, useQuery } from "@tanstack/react-query";
import { postApiV1FriendFindFriendsMutation } from "./react-query.gen";
useQuery({
...getApiV1FeedFeedOptions()
})
useMutation({
...postApiV1FriendFindFriendsMutation(),
onSuccess: (data) => {
// custom logic
}
})
An example of identifying the necessary types for passing in the parameters or body for a mutation:
export const deleteApiV1FriendByIdDeleteSentInviteMutation = (
options?: Partial<Options<DeleteApiV1FriendByIdDeleteSentInviteData>>,
): UseMutationOptions<
DeleteApiV1FriendByIdDeleteSentInviteResponse,
DeleteApiV1FriendByIdDeleteSentInviteError,
Options<DeleteApiV1FriendByIdDeleteSentInviteData>
> => {}
DeleteApiV1FriendByIdDeleteSentInviteData is in types.gen.ts and includes the path parameters
in the path property. The body is also typically located in the body property for mutations.
export type DeleteApiV1FriendByIdDeleteSentInviteData = {
body?: never
path: {
id: string
}
query?: never
url: "/api/v1/friend/{id}/delete-sent-invite"
}
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:
export type DeleteApiV1FriendByIdDeleteSentInviteResponses = {
/**
* Invite deleted successfully
*/
200: {
[key: string]: unknown
}
}
export type DeleteApiV1FriendByIdDeleteSentInviteResponse =
DeleteApiV1FriendByIdDeleteSentInviteResponses[keyof DeleteApiV1FriendByIdDeleteSentInviteResponses]
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
optionswhich will lead you to a file calledtypes.gen.ts.To perform a GET request, you can use the
useQueryanduseMutationhook. For example, to fetch user data:An example of identifying the necessary types for passing in the parameters or body for a mutation:
DeleteApiV1FriendByIdDeleteSentInviteDatais intypes.gen.tsand 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).
DeleteApiV1FriendByIdDeleteSentInviteResponsein this case leads to: