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

Method ensureQueryData

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

* @deprecated Use queryClient.query({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.

(
    options: EnsureQueryDataOptions<TQueryFnData, TError, TData, TQueryKey>,
  )

Source from the content-addressed store, hash-verified

196 * @deprecated Use queryClient.query({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.
197 */
198 ensureQueryData<
199 TQueryFnData,
200 TError = DefaultError,
201 TData = TQueryFnData,
202 TQueryKey extends QueryKey = QueryKey,
203 >(
204 options: EnsureQueryDataOptions<TQueryFnData, TError, TData, TQueryKey>,
205 ): Promise<TData> {
206 const defaultedOptions = this.defaultQueryOptions(options)
207 const query = this.#queryCache.build(this, defaultedOptions)
208 const cachedData = query.state.data
209
210 if (cachedData === undefined) {
211 // eslint-disable-next-line no-restricted-syntax
212 return this.fetchQuery(options)
213 }
214
215 if (
216 options.revalidateIfStale &&
217 query.isStaleByTime(resolveQueryValue(defaultedOptions.staleTime, query))
218 ) {
219 // eslint-disable-next-line no-restricted-syntax
220 void this.prefetchQuery(defaultedOptions)
221 }
222
223 return Promise.resolve(cachedData)
224 }
225
226 /**
227 * Imperative (non-reactive) way to retrieve the cached data of multiple queries at once.

Callers 3

Calls 6

defaultQueryOptionsMethod · 0.95
fetchQueryMethod · 0.95
prefetchQueryMethod · 0.95
resolveQueryValueFunction · 0.90
isStaleByTimeMethod · 0.80
buildMethod · 0.45

Tested by

no test coverage detected