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

Method fetchQuery

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

* Fetches and caches a query, or returns its cached data if it isn't stale. Unlike in observers, * the fetch isn't retried unless `retry` is set. * @param options - The query options, including the `queryKey` and the `queryFn` used if the * query needs to fetch. * @returns A promise that

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

Source from the content-addressed store, hash-verified

653 * @deprecated Use queryClient.query(options) instead. This method will be removed in the next major version.
654 */
655 fetchQuery<
656 TQueryFnData,
657 TError = DefaultError,
658 TData = TQueryFnData,
659 TQueryKey extends QueryKey = QueryKey,
660 TPageParam = never,
661 >(
662 options: FetchQueryOptions<
663 TQueryFnData,
664 TError,
665 TData,
666 TQueryKey,
667 TPageParam
668 >,
669 ): Promise<TData> {
670 const defaultedOptions = this.defaultQueryOptions(options)
671
672 // https://github.com/tannerlinsley/react-query/issues/652
673 if (defaultedOptions.retry === undefined) {
674 defaultedOptions.retry = false
675 }
676
677 const query = this.#queryCache.build(this, defaultedOptions)
678
679 return query.isStaleByTime(
680 resolveQueryValue(defaultedOptions.staleTime, query),
681 )
682 ? query.fetch(defaultedOptions)
683 : Promise.resolve(query.state.data as TData)
684 }
685
686 /**
687 * Fetches and caches a query like {@link QueryClient#fetchQuery}, but ignores the result and any

Callers 15

ensureQueryDataMethod · 0.95
prefetchQueryMethod · 0.95
fetchInfiniteQueryMethod · 0.95
WithKnownParametersFunction · 0.45
WithIdentifiersFunction · 0.45
WithKnownParametersFunction · 0.45
WithIdentifiersFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45

Calls 5

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

Tested by

no test coverage detected