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