Skip to content

Generate tanstack query factory functions #51

@nadavhames

Description

@nadavhames

Code of Conduct

  • I agree to follow this project's Code of Conduct

AI Policy

  • I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

Is your feature request related to a problem? Please describe.

For anyone using tanstack react query and are exposing procedures via ash_typescript to the frontend, only direct async functions are provided, so for every query the dev has to manually write and wire up their query hooks which takes time and is prone to mistakes.
Another frustration that can be mitigated is migrating an existing trpc app over to ash_typescript. It is currently more involved because the user needs to refactor a potentially large number of preexisting hooks.

Describe the solution you'd like

TRPC's latest redesigned tanstack query integration is simple and looks like this:

import { useQuery } from '@tanstack/react-query';
import { useTRPC } from './trpc';
export function Greeting() {
    const trpc = useTRPC();
    const myQuery = useQuery(trpc.path.to.query.queryOptions({ /** inputs */ }))
  }
}

See usage docs here.
For each RPC, it provides either a queryOptions({...}) or mutationOptions({...}) function that returns sensible args to useQuery or useMutation, including queryFn and cache key, while letting the user override whichever properties they need.

I think that ash_typescript should, for each rpc:

  1. Derive from the dsl if the rpc is a query or mutation
  2. Generate a queryOptions or mutationOptions function. This could be optional if for example the user does not care about react query and wants to reduce generated code size if size difference is significant.
    • To do this, examine how trpc provides the default queryFn and derives cache keys and if applicable, use a similar strategy here

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions