Skip to content

Commit

Permalink
Add the suspense option to rspc useQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavenVolkoff committed Oct 9, 2024
1 parent cc2edf5 commit 7c866ef
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/react/src/v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
UseMutationResult,
UseQueryOptions,
UseQueryResult,
UseSuspenseQueryResult,
} from '@tanstack/react-query'
import type { ReactElement } from 'react'

Expand All @@ -22,6 +23,7 @@ import {
useInfiniteQuery as __useInfiniteQuery,
useMutation as __useMutation,
useQuery as __useQuery,
useSuspenseQuery as __useSuspenseQuery,
hashKey,
QueryClient,
QueryClientProvider,
Expand Down Expand Up @@ -86,18 +88,20 @@ export function createReactQueryHooks<P extends ProceduresDef>(
UseQueryOptions<TQueryFnData, RSPCError, TData, [K, inferQueryInput<P, K>]>,
'queryKey' | 'queryFn'
> &
TBaseOptions
): UseQueryResult<TData, RSPCError> {
const { rspc, ...rawOpts } = opts ?? {}
TBaseOptions & { suspense?: boolean }
): typeof opts extends { suspense: true }
? UseSuspenseQueryResult<TData, RSPCError>
: UseQueryResult<TData, RSPCError> {
const { rspc, suspense, ...rawOpts } = opts ?? {}
const client = rspc?.client ?? useContext().client

return __useQuery({
return (suspense ? __useSuspenseQuery : __useQuery)({
queryKey: mapQueryKey(keyAndInput) as [K, inferQueryInput<P, K>],
queryFn: async () => {
return (await client.query(keyAndInput)) as TQueryFnData
},
...rawOpts,
})
}) as any
}

function useMutation<K extends P['mutations']['key'] & string, TContext = unknown>(
Expand Down

0 comments on commit 7c866ef

Please sign in to comment.