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

createQueries.md

158 lines8.1 KB

1---
2id: createQueries
3title: createQueries
4---
5​
6```ts
7const createQueries: <T, TCombinedResult>(queriesOptions: Accessor<{
8 combine?: (result: T extends [] ? [] : T extends [Head] ? [GetResults<Head>] : T extends [Head, ...Tail[]] ? [...Tail[]] extends [] ? [] : [...(...)[]] extends [...] ? [..., ...] : ... extends ... ? ... : ... : { [K in string | number | symbol]: GetResults<(...)[(...)]> }) => TCombinedResult;
9 queries: | readonly [T extends [] ? [] : T extends [Head] ? [GetOptions<Head>] : T extends [Head, ...Tail[]] ? [...Tail[]] extends [] ? [] : [...(...)[]] extends [...] ? [..., ...] : ... extends ... ? ... : ... : readonly unknown[] extends T ? T : T extends ...[] ? ...[] : ...[]]
10 | readonly [{ [K in string | number | symbol]: GetOptions<T[K]> }];
11}>, queryClient?: Accessor<QueryClient>) => TCombinedResult = useQueries;
12```
13​
14Defined in: [packages/solid-query/src/index.ts:86](https://github.com/TanStack/query/blob/main/packages/solid-query/src/index.ts#L86)
15​
16The `useQueries` primitive can be used to fetch a variable number of queries.
17​
18The `queries` key accepts an array with query option objects mostly identical to `useQuery` β€” see
19`placeholderData` below for the one difference. A custom `QueryClient` is supplied once, as `useQueries`'
20own top-level second argument, rather than per query.
21​
22Having the same query key more than once in the array of query objects may cause some data to be shared
23between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
24structure.
25​
26The `combine` option can be used to combine the results of the queries into a single value. The result will
27be structurally shared to be as referentially stable as possible.
28​
29`placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
30previously rendered queries, because the number of queries can differ between renders.
31​
32## Type Parameters
33​
34### T
35​
36`T` *extends* `any`[]
37​
38### TCombinedResult
39​
40`TCombinedResult` *extends*
41 \| \[\]
42 \| \[
43 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `Error`\>
44 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `Error`\>
45 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `Error`\>
46 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `Error`\>
47 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `Error`\>
48 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `Error`\>
49 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `unknown`\>
50 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `unknown`\>
51 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `unknown`\>
52 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `unknown`\>
53 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `unknown`\>
54 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `unknown`\>\]
55 \| (
56 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `Error`\>
57 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `Error`\>
58 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `Error`\>
59 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `Error`\>
60 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `Error`\>
61 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `Error`\>
62 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `unknown`\>
63 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `unknown`\>
64 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `unknown`\>
65 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `unknown`\>
66 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `unknown`\>
67 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `unknown`\>)[] = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...Tail[]`\] *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>, `GetResults`\<`Head`\>\] : \[`...Tail[]`\] *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...Tail[]`\] *extends* \[`Head`\] ? \[`GetResults`\<...\>, `GetResults`\<...\>, `GetResults`\<...\>\] : \[`...(...)[]`\] *extends* \[..., `...(...)[]`\] ? ... *extends* ... ? ... : ... : \[`...(...)[]`\] : \[...\{ \[K in string \| number \| symbol\]: GetResults\<(...)\[(...)\]\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GetResults\<T\[K\]\> \}
68​
69## Parameters
70​
71### queriesOptions
72​
73`Accessor`\<\{
74 `combine?`: (`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ...\] : ... *extends* ... ? ... : ... : \{ \[K in string \| number \| symbol\]: GetResults\<(...)\[(...)\]\> \}) => `TCombinedResult`;
75 `queries`: \| readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetOptions`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ...\] : ... *extends* ... ? ... : ... : readonly `unknown`[] *extends* `T` ? `T` : `T` *extends* ...[] ? ...[] : ...[]\]
76 \| readonly \[\{ \[K in string \| number \| symbol\]: GetOptions\<T\[K\]\> \}\];
77\}\>
78​
79An accessor returning the `queries` array to run, and an optional `combine`
80function.
81​
82### queryClient?
83​
84`Accessor`\<[`QueryClient`](../classes/QueryClient.md)\>
85​
86An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
87will be used.
88​
89## Returns
90​
91`TCombinedResult`
92​
93The combined result. Without `combine`, this is an array with all the query results, in the same
94order as the input. When `combine` is provided, this is the value returned by `combine` instead.
95​
96## Examples
97​
98```tsx
99import { For } from 'solid-js'
100import { useQueries } from '@tanstack/solid-query'
101​
102function Posts(props: { ids: Array<number> }) {
103 const postQueries = useQueries(() => ({
104 queries: props.ids.map((id) => ({
105 queryKey: ['post', id],
106 queryFn: () => fetchPost(id),
107 staleTime: Infinity,
108 })),
109 }))
110​
111 return (
112 <ul>
113 <For each={postQueries}>
114 {(postQuery) => {
115 if (postQuery.isPending) return <li>Loading...</li>
116 if (postQuery.isError) return <li>Error: {postQuery.error.message}</li>
117 return <li>{postQuery.data.title}</li>
118 }}
119 </For>
120 </ul>
121 )
123```
124​
125Combining results into a single value:
126```tsx
127import { For, Match, Switch } from 'solid-js'
128import { useQueries } from '@tanstack/solid-query'
129​
130function Posts(props: { ids: Array<number> }) {
131 const combinedPostsQuery = useQueries(() => ({
132 queries: props.ids.map((id) => ({
133 queryKey: ['post', id],
134 queryFn: () => fetchPost(id),
135 })),
136 combine: (postQueries) => {
137 return {
138 data: postQueries.map((postQuery) => postQuery.data),
139 isPending: postQueries.some((postQuery) => postQuery.isPending),
140 isError: postQueries.some((postQuery) => postQuery.isError),
141 }
142 },
143 }))
144​
145 return (
146 <Switch
147 fallback={
148 <ul>
149 <For each={combinedPostsQuery.data}>{(post) => <li>{post?.title}</li>}</For>
150 </ul>
151 }
152 >
153 <Match when={combinedPostsQuery.isPending}>Loading...</Match>
154 <Match when={combinedPostsQuery.isError}>Error loading posts</Match>
155 </Switch>
156 )
158```