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

Method query

packages/query-core/src/queryClient.ts:599–640  ·  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

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

Callers 10

infiniteQueryMethod · 0.95
Devtools.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