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

Method query

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

554 * ```
555 */
556 async query<
557 TQueryFnData,
558 TError = DefaultError,
559 TData = TQueryFnData,
560 TQueryData = TQueryFnData,
561 TQueryKey extends QueryKey = QueryKey,
562 TPageParam = never,
563 >(
564 options: QueryExecuteOptions<
565 TQueryFnData,
566 TError,
567 TData,
568 TQueryData,
569 TQueryKey,
570 TPageParam
571 >,
572 ): Promise<TData> {
573 const defaultedOptions = this.defaultQueryOptions(options)
574
575 // https://github.com/tannerlinsley/react-query/issues/652
576 if (defaultedOptions.retry === undefined) {
577 defaultedOptions.retry = false
578 }
579
580 const query = this.#queryCache.build(this, defaultedOptions)
581
582 const isStale = query.isStaleByTime(
583 resolveQueryValue(defaultedOptions.staleTime, query),
584 )
585
586 const queryData = isStale
587 ? await query.fetch(defaultedOptions)
588 : (query.state.data as TQueryData)
589
590 const select = defaultedOptions.select
591
592 if (select) {
593 return select(queryData)
594 }
595
596 return queryData as unknown as TData
597 }
598
599 /**
600 * @deprecated Use queryClient.query(options) instead. This method will be removed in the next major version.

Callers 9

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