Skip to content

Claude Code CLAUDE.md template for react-query.gen #2571

@Andrew-Chen-Wang

Description

@Andrew-Chen-Wang

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs 📃Documentation updates.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions