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

queryOptions.ts

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