MCPcopy
hub / github.com/TanStack/query / createQueries

Function createQueries

packages/svelte-query/src/createQueries.ts:189–264  ·  view source on GitHub ↗
(
  {
    queries,
    ...options
  }: {
    queries:
      | StoreOrVal<[...QueriesOptions<T>]>
      | StoreOrVal<
          [...{ [K in keyof T]: GetQueryObserverOptionsForCreateQueries<T[K]> }]
        >
    combine?: (result: QueriesResults<T>) => TCombinedResult
  },
  queryClient?: QueryClient,
)

Source from the content-addressed store, hash-verified

187 : { [K in keyof T]: GetCreateQueryResult<T[K]> }
188
189export function createQueries<
190 T extends Array<any>,
191 TCombinedResult = QueriesResults<T>,
192>(
193 {
194 queries,
195 ...options
196 }: {
197 queries:
198 | StoreOrVal<[...QueriesOptions<T>]>
199 | StoreOrVal<
200 [...{ [K in keyof T]: GetQueryObserverOptionsForCreateQueries<T[K]> }]
201 >
202 combine?: (result: QueriesResults<T>) => TCombinedResult
203 },
204 queryClient?: QueryClient,
205): Readable<TCombinedResult> {
206 const client = useQueryClient(queryClient)
207 const isRestoring = useIsRestoring()
208
209 const queriesStore = isSvelteStore(queries) ? queries : readable(queries)
210
211 const defaultedQueriesStore = derived(
212 [queriesStore, isRestoring],
213 ([$queries, $isRestoring]) => {
214 return $queries.map((opts) => {
215 const defaultedOptions = client.defaultQueryOptions(
216 opts as QueryObserverOptions,
217 )
218 // Make sure the results are already in fetching state before subscribing or updating options
219 defaultedOptions._optimisticResults = $isRestoring
220 ? 'isRestoring'
221 : 'optimistic'
222 return defaultedOptions
223 })
224 },
225 )
226 const observer = new QueriesObserver<TCombinedResult>(
227 client,
228 get(defaultedQueriesStore),
229 options as QueriesObserverOptions<TCombinedResult>,
230 )
231
232 defaultedQueriesStore.subscribe(($defaultedQueries) => {
233 // Do not notify on updates because of changes in the options because
234 // these changes should already be reflected in the optimistic result.
235 observer.setQueries(
236 $defaultedQueries,
237 options as QueriesObserverOptions<TCombinedResult>,
238 )
239 })
240
241 const result = derived([isRestoring], ([$isRestoring], set) => {
242 const unsubscribe = $isRestoring
243 ? noop
244 : observer.subscribe(notifyManager.batchCalls(set))
245
246 return () => unsubscribe()

Callers 3

useCustomQueriesFunction · 0.85

Calls 9

setQueriesMethod · 0.95
getOptimisticResultMethod · 0.95
isSvelteStoreFunction · 0.85
defaultQueryOptionsMethod · 0.80
useQueryClientFunction · 0.70
useIsRestoringFunction · 0.70
getFunction · 0.50
unsubscribeFunction · 0.50
subscribeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…