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

queryOptions.ts

128 lines4.2 KB

1import type {
2 DefaultError,
3 InitialDataFunction,
4 NonUndefinedGuard,
5 QueryKey,
6 QueryKeyWithDataTag,
7} from '@tanstack/query-core'
8import type { CreateQueryOptions } from './types.js'
9​
10export type UndefinedInitialDataOptions<
11 TQueryFnData = unknown,
12 TError = DefaultError,
13 TData = TQueryFnData,
14 TQueryKey extends QueryKey = QueryKey,
15> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
16 initialData?:
17 | undefined
18 | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
19 | NonUndefinedGuard<TQueryFnData>
21​
22export type DefinedInitialDataOptions<
23 TQueryFnData = unknown,
24 TError = DefaultError,
25 TData = TQueryFnData,
26 TQueryKey extends QueryKey = QueryKey,
27> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
28 initialData:
29 NonUndefinedGuard<TQueryFnData> | (() => NonUndefinedGuard<TQueryFnData>)
31​
32/**
33 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
34 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
35 * is required and is the query key to generate options for.
36 *
37 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
38 * a `select` changes `TData` to include `undefined`).
39 *
40 * @see {@link createQuery} to run a query with these options.
41 * @param options - The {@link DefinedInitialDataOptions} to use β€” everything you can pass to `createQuery`,
42 * with `initialData` set.
43 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
44 *
45 * @example
46 * ```svelte
47 * <script lang="ts">
48 * import { queryOptions, createQuery } from '@tanstack/svelte-query'
49 *
50 * const postsOptions = queryOptions({
51 * queryKey: ['posts'],
52 * queryFn: fetchPosts,
53 * initialData: [],
54 * })
55 *
56 * // `data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
57 * // so the list stays visible alongside the error.
58 * const query = createQuery(() => postsOptions)
59 * </script>
60 *
61 * {#if query.isError}
62 * <span>Error: {query.error.message}</span>
63 * {/if}
64 * <ul>
65 * {#each query.data as post (post.id)}
66 * <li>{post.title}</li>
67 * {/each}
68 * </ul>
69 * ```
70 */
71export function queryOptions<
72 TQueryFnData = unknown,
73 TError = DefaultError,
74 TData = TQueryFnData,
75 TQueryKey extends QueryKey = QueryKey,
76>(
77 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
78): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
79 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
80​
81/**
82 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
83 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
84 * is required and is the query key to generate options for.
85 *
86 * @see {@link createQuery} to run a query with these options.
87 * @param options - The {@link UndefinedInitialDataOptions} to use β€” everything you can pass to `createQuery`.
88 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
89 *
90 * @example
91 * A parameterized factory, so the same options object can be reused per `id`:
92 * ```svelte
93 * <script lang="ts">
94 * import { queryOptions, createQuery } from '@tanstack/svelte-query'
95 *
96 * let { id }: { id: string } = $props()
97 *
98 * const postOptions = (id: string) =>
99 * queryOptions({
100 * queryKey: ['post', id],
101 * queryFn: () => fetchPost(id),
102 * })
103 *
104 * const query = createQuery(() => postOptions(id))
105 * </script>
106 *
107 * {#if query.isPending}
108 * Loading...
109 * {:else if query.isError}
110 * <span>Error: {query.error.message}</span>
111 * {:else}
112 * <h1>{query.data.title}</h1>
113 * {/if}
114 * ```
115 */
116export function queryOptions<
117 TQueryFnData = unknown,
118 TError = DefaultError,
119 TData = TQueryFnData,
120 TQueryKey extends QueryKey = QueryKey,
121>(
122 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
123): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
124 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
125​
126export function queryOptions(options: unknown) {
127 return options