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

queryOptions.ts

155 lines5.3 KB

1import type {
2 DefaultError,
3 QueryKey,
4 QueryKeyWithDataTag,
5} from '@tanstack/query-core'
6import type { QueryOptions } from './types'
7import type { Accessor } from 'solid-js'
8​
9/**
10 * The options accepted by the `queryOptions` overload selected when no `initialData` is set β€” `data` may be
11 * `undefined` while the query is `pending`. `queryOptions` itself accepts and returns a plain object (its
12 * parameter type is `ReturnType<UndefinedInitialDataOptions<...>>`, i.e. this `Accessor` called); Solid's
13 * reactivity applies where the result is consumed instead, e.g. `useQuery(() => options)`.
14 *
15 * @template TQueryFnData - The type your `queryFn` resolves to.
16 * @template TError - The type of errors your `queryFn` may throw.
17 * @template TData - The type `data` ends up as after `select` runs.
18 * @template TQueryKey - The type of your `queryKey`.
19 */
20export type UndefinedInitialDataOptions<
21 TQueryFnData = unknown,
22 TError = DefaultError,
23 TData = TQueryFnData,
24 TQueryKey extends QueryKey = QueryKey,
25> = Accessor<
26 QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
27 initialData?: undefined
28 }
30​
31/**
32 * The options accepted by the `queryOptions` overload selected when `initialData` is set β€” `data` is never
33 * `undefined`.
34 *
35 * @template TQueryFnData - The type your `queryFn` resolves to.
36 * @template TError - The type of errors your `queryFn` may throw.
37 * @template TData - The type `data` ends up as after `select` runs.
38 * @template TQueryKey - The type of your `queryKey`.
39 */
40export type DefinedInitialDataOptions<
41 TQueryFnData = unknown,
42 TError = DefaultError,
43 TData = TQueryFnData,
44 TQueryKey extends QueryKey = QueryKey,
45> = Accessor<
46 QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
47 initialData: TQueryFnData | (() => TQueryFnData)
48 }
50​
51/**
52 * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
53 * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
54 * is the query key to generate options for.
55 *
56 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
57 *
58 * @see {@link useQuery} to run a query with these options.
59 * @param options - The {@link DefinedInitialDataOptions} to use β€” everything you can pass to `useQuery`, with `initialData` set.
60 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
61 *
62 * @example
63 * ```tsx
64 * import { For } from 'solid-js'
65 * import { queryOptions, useQuery } from '@tanstack/solid-query'
66 *
67 * const postsOptions = queryOptions({
68 * queryKey: ['posts'],
69 * queryFn: fetchPosts,
70 * initialData: [],
71 * })
72 *
73 * function Posts() {
74 * // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
75 * // so the list stays visible alongside the error.
76 * const postsQuery = useQuery(() => postsOptions)
77 *
78 * return (
79 * <div>
80 * {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
81 * <ul>
82 * <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
83 * </ul>
84 * </div>
85 * )
86 * }
87 * ```
88 */
89export function queryOptions<
90 TQueryFnData = unknown,
91 TError = DefaultError,
92 TData = TQueryFnData,
93 TQueryKey extends QueryKey = QueryKey,
94>(
95 options: ReturnType<
96 DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
97 >,
98): ReturnType<
99 DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
100> &
101 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
102​
103/**
104 * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
105 * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
106 * is the query key to generate options for.
107 *
108 * @see {@link useQuery} to run a query with these options.
109 * @param options - The {@link UndefinedInitialDataOptions} to use β€” everything you can pass to `useQuery`.
110 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
111 *
112 * @example
113 * A parameterized factory, so the same options object can be reused per `id`:
114 * ```tsx
115 * import { Match, Switch } from 'solid-js'
116 * import { queryOptions, useQuery } from '@tanstack/solid-query'
117 *
118 * const postOptions = (id: string) =>
119 * queryOptions({
120 * queryKey: ['post', id],
121 * queryFn: () => fetchPost(id),
122 * })
123 *
124 * function Post(props: { id: string }) {
125 * const postQuery = useQuery(() => postOptions(props.id))
126 *
127 * return (
128 * <Switch>
129 * <Match when={postQuery.isPending}>Loading...</Match>
130 * <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
131 * <Match when={postQuery.isSuccess}>
132 * <h1>{postQuery.data.title}</h1>
133 * </Match>
134 * </Switch>
135 * )
136 * }
137 * ```
138 */
139export function queryOptions<
140 TQueryFnData = unknown,
141 TError = DefaultError,
142 TData = TQueryFnData,
143 TQueryKey extends QueryKey = QueryKey,
144>(
145 options: ReturnType<
146 UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
147 >,
148): ReturnType<
149 UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
150> &
151 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
152​
153export function queryOptions(options: unknown) {
154 return options