Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQuery.ts

54 lines1.9 KB

1import { QueryObserver } from '@tanstack/query-core'
2import { useBaseQuery } from './useBaseQuery'
3import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
4import type {
5 DefinedUseQueryResult,
6 UseQueryOptions,
7 UseQueryResult,
8} from './types'
9import type {
10 DefinedInitialDataOptions,
11 UndefinedInitialDataOptions,
12} from './queryOptions'
13​
14// Overloads match @tanstack/react-query's useQuery.ts: `initialData` narrows the
15// result to DefinedUseQueryResult. The untyped implementation signature also
16// accepts the compiler-injected trailing slot symbol (never visible to users).
17export function useQuery<
18 TQueryFnData = unknown,
19 TError = DefaultError,
20 TData = TQueryFnData,
21 TQueryKey extends QueryKey = QueryKey,
22>(
23 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
24 queryClient?: QueryClient,
25): DefinedUseQueryResult<NoInfer<TData>, TError>
26​
27export function useQuery<
28 TQueryFnData = unknown,
29 TError = DefaultError,
30 TData = TQueryFnData,
31 TQueryKey extends QueryKey = QueryKey,
32>(
33 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
34 queryClient?: QueryClient,
35): UseQueryResult<NoInfer<TData>, TError>
36​
37export function useQuery<
38 TQueryFnData = unknown,
39 TError = DefaultError,
40 TData = TQueryFnData,
41 TQueryKey extends QueryKey = QueryKey,
42>(
43 options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
44 queryClient?: QueryClient,
45): UseQueryResult<NoInfer<TData>, TError>
46​
47export function useQuery(options: any, ...rest: Array<any>): any {
48 // `[queryClient?, slot?]` β€” the slot (symbol) is the compiler-injected trailing
49 // arg; an explicit client is the first non-symbol arg.
50 const tail = rest[rest.length - 1]
51 const slot = typeof tail === 'symbol' ? tail : undefined
52 const queryClient = typeof rest[0] !== 'symbol' ? rest[0] : undefined
53 return useBaseQuery(options, QueryObserver, queryClient, slot)