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

Method ensureQueryData

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

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