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