MCPcopy Create free account
hub / github.com/TanStack/query / query

Method query

packages/query-core/src/queryClient.ts:603–644  ·  view source on GitHub ↗

* Asynchronous method to fetch and cache a query, resolving with the data or throwing with * the error. * * If the query already exists in the cache and its data is not stale (per the given * `staleTime`), the cached data is returned without fetching. Otherwise, the query is fetched *

(
    options: QueryExecuteOptions<
      TQueryFnData,
      TError,
      TData,
      TQueryData,
      TQueryKey,
      TPageParam
    >,
  )

Source from the content-addressed store, hash-verified

601 * ```
602 */
603 async query<
604 TQueryFnData,
605 TError = DefaultError,
606 TData = TQueryFnData,
607 TQueryData = TQueryFnData,
608 TQueryKey extends QueryKey = QueryKey,
609 TPageParam = never,
610 >(
611 options: QueryExecuteOptions<
612 TQueryFnData,
613 TError,
614 TData,
615 TQueryData,
616 TQueryKey,
617 TPageParam
618 >,
619 ): Promise<TData> {
620 const defaultedOptions = this.defaultQueryOptions(options)
621
622 // https://github.com/tannerlinsley/react-query/issues/652
623 if (defaultedOptions.retry === undefined) {
624 defaultedOptions.retry = false
625 }
626
627 const query = this.#queryCache.build(this, defaultedOptions)
628
629 const isStale = query.isStaleByTime(
630 resolveQueryValue(defaultedOptions.staleTime, query),
631 )
632
633 const queryData = isStale
634 ? await query.fetch(defaultedOptions)
635 : (query.state.data as TQueryData)
636
637 const select = defaultedOptions.select
638
639 if (select) {
640 return select(queryData)
641 }
642
643 return queryData as unknown as TData
644 }
645
646 /**
647 * Fetches and caches a query, or returns its cached data if it isn't stale. Unlike in observers,

Callers 11

infiniteQueryMethod · 0.95
Devtools.test.tsxFile · 0.45
cacheGc.test.tsxFile · 0.45
query.test.tsxFile · 0.45
hydration.test.tsxFile · 0.45

Calls 6

defaultQueryOptionsMethod · 0.95
resolveQueryValueFunction · 0.90
isStaleByTimeMethod · 0.80
selectFunction · 0.50
buildMethod · 0.45
fetchMethod · 0.45

Tested by

no test coverage detected