ggascoigne
ggascoigne
TtRPC
Created by ggascoigne on 3/31/2025 in #❓-help
Types question
I'll admit that this particular example doesn't really justify the hassle of wrapping the raw trpc api, but that's because I picked a non-distracting example. That said, I am beginning to question all of this, and perhaps I'll just stop wrapping the api. I've always wrapped the various react-query apis in the past, in large part to manage query keys, and trpc already does a great job of that gotcha, and what's left just feels like boiler plate in my code.
6 replies
TtRPC
Created by ggascoigne on 3/31/2025 in #❓-help
Types question
In case anyone else lands here, this is what I ended out with.
import { UseQueryOptions } from '@tanstack/react-query'
import { TRPCClientErrorLike } from '@trpc/client'
import { TRPCQueryKey } from '@trpc/tanstack-react-query'

export type LookupValuesQueryResult = RouterOutputs['lookups']['getLookupValues']
export type LookupValue = LookupValuesQueryResult[0]

type BaseQueryOptions<T> = UseQueryOptions<any, TRPCClientErrorLike<any>, T, TRPCQueryKey>
export type QueryOptions<T> = Omit<BaseQueryOptions<T>, 'queryKey' | 'queryFn' | 'initialData'>

export const useGetLookupValuesQuery = (realm: string, opts: QueryOptions<Lookup[]>) => {
const trpc = useTRPC()
return useQuery(trpc.lookups.getLookupValues.queryOptions({ realm }, opts))
}
import { UseQueryOptions } from '@tanstack/react-query'
import { TRPCClientErrorLike } from '@trpc/client'
import { TRPCQueryKey } from '@trpc/tanstack-react-query'

export type LookupValuesQueryResult = RouterOutputs['lookups']['getLookupValues']
export type LookupValue = LookupValuesQueryResult[0]

type BaseQueryOptions<T> = UseQueryOptions<any, TRPCClientErrorLike<any>, T, TRPCQueryKey>
export type QueryOptions<T> = Omit<BaseQueryOptions<T>, 'queryKey' | 'queryFn' | 'initialData'>

export const useGetLookupValuesQuery = (realm: string, opts: QueryOptions<Lookup[]>) => {
const trpc = useTRPC()
return useQuery(trpc.lookups.getLookupValues.queryOptions({ realm }, opts))
}
Not a fan of having to use any, but this seems like the simplest solution that I've found so far.
6 replies
TtRPC
Created by ggascoigne on 3/31/2025 in #❓-help
Types question
Yes, that might well be what I want. I was assuming that I should just reuse TRPCQueryOptions, but I might just want the input type (for the variables) and then the stock query options type from @tanstack/query. I think I was making it more complicated than it needed and trying to infer them from the resolver (whatever that actually is). Thanks, I'll try it.
6 replies