Open History to see the last commit
1import type {
2 DefaultError,
3 InitialDataFunction,
4 NonUndefinedGuard,
5 OmitKeyof,
6 QueryFunction,
7 QueryKey,
8 QueryKeyWithDataTag,
9 SkipToken,
10} from '@tanstack/query-core'
11import type { CreateQueryOptions } from './types'
12β
13/**
14 * The options accepted by the `queryOptions` overload selected when no
15 * `initialData` is set β `data` may be `undefined` while the query is
16 * `pending`.
17 *
18 * @template TQueryFnData - The type your `queryFn` resolves to.
19 * @template TError - The type of errors your `queryFn` may throw.
20 * @template TData - The type `data` ends up as after `select` runs.
21 * @template TQueryKey - The type of your `queryKey`.
22 */
23export type UndefinedInitialDataOptions<
24 TQueryFnData = unknown,
25 TError = DefaultError,
26 TData = TQueryFnData,
27 TQueryKey extends QueryKey = QueryKey,
28> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
29 /**
30 * If set, this value will be used as the initial data for the query cache (as
31 * long as the query hasn't been created or cached yet). If set to a function,
32 * the function will be called **once** during the shared/root query
33 * initialization, and be expected to synchronously return the initial data.
34 * Initial data is considered stale by default unless a `staleTime` has been
35 * set. `initialData` **is persisted** to the cache.
36 */
37 initialData?:
38 | undefined
39 | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
40 | NonUndefinedGuard<TQueryFnData>
41}
42β
43/**
44 * The options accepted by the `queryOptions` overload selected when no
45 * `initialData` is set and `queryFn` is not `skipToken` β same as
46 * {@link UndefinedInitialDataOptions}, but `queryFn` may not be `skipToken`.
47 *
48 * @template TQueryFnData - The type your `queryFn` resolves to.
49 * @template TError - The type of errors your `queryFn` may throw.
50 * @template TData - The type `data` ends up as after `select` runs.
51 * @template TQueryKey - The type of your `queryKey`.
52 */
53export type UnusedSkipTokenOptions<
54 TQueryFnData = unknown,
55 TError = DefaultError,
56 TData = TQueryFnData,
57 TQueryKey extends QueryKey = QueryKey,
58> = OmitKeyof<
59 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
60 'queryFn'
61> & {
62 /**
63 * `skipToken` is not allowed as a value here β this overload is selected when
64 * no `initialData` is set. If you don't intend to run the query yet, set
65 * `enabled: false` β omitting `queryFn` alone still triggers a fetch that
66 * fails with "Missing queryFn" unless `enabled` is `false` or a default query
67 * function has been defined. A default query function only supplies
68 * `queryFn`; it doesn't defer the fetch on its own.
69 */
70 queryFn?: Exclude<
71 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
72 SkipToken | undefined
73 >
74}
75β
76/**
77 * The options accepted by the `queryOptions` overload selected when
78 * `initialData` is set β `data` is never `undefined` (unless a `select` changes
79 * `TData` to include `undefined`).
80 *
81 * @template TQueryFnData - The type your `queryFn` resolves to.
82 * @template TError - The type of errors your `queryFn` may throw.
83 * @template TData - The type `data` ends up as after `select` runs.
84 * @template TQueryKey - The type of your `queryKey`.
85 */
86export type DefinedInitialDataOptions<
87 TQueryFnData = unknown,
88 TError = DefaultError,
89 TData = TQueryFnData,
90 TQueryKey extends QueryKey = QueryKey,
91> = Omit<
92 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
93 'queryFn'
94> & {
95 /**
96 * If set, this value will be used as the initial data for the query cache (as
97 * long as the query hasn't been created or cached yet). If set to a function,
98 * the function will be called **once** during the shared/root query
99 * initialization, and be expected to synchronously return the initial data.
100 * Initial data is considered stale by default unless a `staleTime` has been
101 * set. `initialData` **is persisted** to the cache.
102 */
103 initialData:
104 | NonUndefinedGuard<TQueryFnData>
105 | (() => NonUndefinedGuard<TQueryFnData>)
106 /**
107 * Optional here, but omitting it is only safe when no fetch will be attempted
108 * β for example with `enabled: false`, or when a default query function has
109 * been defined. Otherwise, an enabled query with no `queryFn` still tries to
110 * fetch and fails with a "Missing queryFn" error; `initialData` does not
111 * prevent this.
112 */
113 queryFn?: QueryFunction<TQueryFnData, TQueryKey>
114}
115β
116/**
117 * You can generally pass everything to `queryOptions` that you can also pass to
118 * `injectQuery`. These options can be shared across functions and imperative
119 * APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is
120 * the query key to generate options for.
121 *
122 * This overload is selected when `initialData` is set, so the resulting `data`
123 * is never `undefined` (unless a `select` changes `TData` to include
124 * `undefined`).
125 *
126 * @example
127 * ```angular-ts
128 * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
129 *
130 * export const postsOptions = queryOptions({
131 * queryKey: ['posts'],
132 * queryFn: fetchPosts,
133 * initialData: [],
134 * })
135 *
136 * @Component({
137 * selector: 'posts',
138 * template: `
139 * <!-- `postsQuery.data()` is never `undefined`, thanks to `initialData` β even if a refetch
140 * fails, so the list stays visible alongside the error. -->
141 * @if (postsQuery.isError()) {
142 * <span>Error: {{ postsQuery.error()?.message }}</span>
143 * }
144 * <ul>
145 * @for (post of postsQuery.data(); track post.id) {
146 * <li>{{ post.title }}</li>
147 * }
148 * </ul>
149 * `,
150 * })
151 * export class Posts {
152 * readonly postsQuery = injectQuery(() => postsOptions)
153 * }
154 * ```
155 *
156 * @param options - The {@link DefinedInitialDataOptions} to use β everything
157 * you can pass to `injectQuery`, with `initialData` set.
158 * @returns The same options object, typed so that `queryKey` carries the
159 * inferred data type.
160 * @see {@link injectQuery} to run a query with these options.
161 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
162 */
163export function queryOptions<
164 TQueryFnData = unknown,
165 TError = DefaultError,
166 TData = TQueryFnData,
167 TQueryKey extends QueryKey = QueryKey,
168>(
169 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
170): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
171 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
172β
173/**
174 * You can generally pass everything to `queryOptions` that you can also pass to
175 * `injectQuery`. These options can be shared across functions and imperative
176 * APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is
177 * the query key to generate options for.
178 *
179 * @example
180 * A parameterized factory, so the same options object can be reused per `id`:
181 * ```angular-ts
182 * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
183 *
184 * export const postOptions = (id: string) =>
185 * queryOptions({
186 * queryKey: ['post', id],
187 * queryFn: () => fetchPost(id),
188 * })
189 *
190 * @Component({
191 * selector: 'post',
192 * template: `
193 * @if (postQuery.isPending()) {
194 * Loading...
195 * } @else if (postQuery.isError()) {
196 * <span>Error: {{ postQuery.error()?.message }}</span>
197 * } @else {
198 * <h1>{{ postQuery.data().title }}</h1>
199 * }
200 * `,
201 * })
202 * export class Post {
203 * readonly id = signal('1')
204 * readonly postQuery = injectQuery(() => postOptions(this.id()))
205 * }
206 * ```
207 *
208 * @param options - The {@link UnusedSkipTokenOptions} to use β everything you
209 * can pass to `injectQuery`.
210 * @returns The same options object, typed so that `queryKey` carries the
211 * inferred data type.
212 * @see {@link injectQuery} to run a query with these options.
213 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
214 */
215export function queryOptions<
216 TQueryFnData = unknown,
217 TError = DefaultError,
218 TData = TQueryFnData,
219 TQueryKey extends QueryKey = QueryKey,
220>(
221 options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>,
222): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> &
223 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
224β
225/**
226 * You can generally pass everything to `queryOptions` that you can also pass to
227 * `injectQuery`. These options can be shared across functions and imperative
228 * APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is
229 * the query key to generate options for.
230 *
231 * @remarks
232 * This is the only overload that accepts `queryFn: skipToken`, shown below.
233 * @example
234 * A parameterized factory, so the same options object can be reused per `id`:
235 * ```angular-ts
236 * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
237 *
238 * export const postOptions = (id: string) =>
239 * queryOptions({
240 * queryKey: ['post', id],
241 * queryFn: () => fetchPost(id),
242 * })
243 *
244 * @Component({
245 * selector: 'post',
246 * template: `
247 * @if (postQuery.isPending()) {
248 * Loading...
249 * } @else if (postQuery.isError()) {
250 * <span>Error: {{ postQuery.error()?.message }}</span>
251 * } @else {
252 * <h1>{{ postQuery.data().title }}</h1>
253 * }
254 * `,
255 * })
256 * export class Post {
257 * readonly id = signal('1')
258 * readonly postQuery = injectQuery(() => postOptions(this.id()))
259 * }
260 * ```
261 *
262 * @example
263 * A factory that disables the query, type safe, until `postId` is set:
264 * ```angular-ts
265 * import { queryOptions, skipToken, injectQuery } from '@tanstack/angular-query-experimental'
266 *
267 * export const postOptions = (postId: number | undefined) =>
268 * queryOptions({
269 * queryKey: ['post', postId],
270 * queryFn: postId != null ? () => fetchPost(postId) : skipToken,
271 * })
272 *
273 * @Component({
274 * selector: 'post',
275 * template: `
276 * @if (postId() == null) {
277 * Select a post
278 * } @else if (postQuery.isPending()) {
279 * Loading...
280 * } @else if (postQuery.isError()) {
281 * <span>Error: {{ postQuery.error()?.message }}</span>
282 * } @else {
283 * <h1>{{ postQuery.data().title }}</h1>
284 * }
285 * `,
286 * })
287 * export class Post {
288 * readonly postId = signal<number | undefined>(undefined)
289 * readonly postQuery = injectQuery(() => postOptions(this.postId()))
290 * }
291 * ```
292 *
293 * @param options - The {@link UndefinedInitialDataOptions} to use β everything
294 * you can pass to `injectQuery`.
295 * @returns The same options object, typed so that `queryKey` carries the
296 * inferred data type.
297 * @see {@link injectQuery} to run a query with these options.
298 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
299 */
300export function queryOptions<
301 TQueryFnData = unknown,
302 TError = DefaultError,
303 TData = TQueryFnData,
304 TQueryKey extends QueryKey = QueryKey,
305>(
306 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
307): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
308 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
309β
310export function queryOptions(options: unknown) {
311 return options
312}