Skip to content

Commit b07aea3

Browse files
committed
revert delegations
1 parent c92b4a8 commit b07aea3

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

packages/query-core/src/queryClient.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,20 @@ export class QueryClient {
149149
): Promise<TData> {
150150
const defaultedOptions = this.defaultQueryOptions(options)
151151
const query = this.#queryCache.build(this, defaultedOptions)
152+
const cachedData = query.state.data
153+
154+
if (cachedData === undefined) {
155+
return this.fetchQuery(options)
156+
}
152157

153158
if (
154159
options.revalidateIfStale &&
155160
query.isStaleByTime(resolveStaleTime(defaultedOptions.staleTime, query))
156161
) {
157-
void this.query(options).catch(noop)
162+
void this.prefetchQuery(options)
158163
}
159164

160-
return this.query({ ...options, staleTime: 'static' })
165+
return Promise.resolve(cachedData)
161166
}
162167

163168
getQueriesData<
@@ -393,8 +398,22 @@ export class QueryClient {
393398
TPageParam
394399
>,
395400
): Promise<TData> {
396-
return this.query(options)
401+
const defaultedOptions = this.defaultQueryOptions(options)
402+
403+
// https://github.com/tannerlinsley/react-query/issues/652
404+
if (defaultedOptions.retry === undefined) {
405+
defaultedOptions.retry = false
406+
}
407+
408+
const query = this.#queryCache.build(this, defaultedOptions)
409+
410+
return query.isStaleByTime(
411+
resolveStaleTime(defaultedOptions.staleTime, query),
412+
)
413+
? query.fetch(defaultedOptions)
414+
: Promise.resolve(query.state.data as TData)
397415
}
416+
398417
prefetchQuery<
399418
TQueryFnData = unknown,
400419
TError = DefaultError,
@@ -403,7 +422,7 @@ export class QueryClient {
403422
>(
404423
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
405424
): Promise<void> {
406-
return this.query(options).then(noop).catch(noop)
425+
return this.fetchQuery(options).then(noop).catch(noop)
407426
}
408427

409428
infiniteQuery<
@@ -445,7 +464,7 @@ export class QueryClient {
445464
TPageParam
446465
>,
447466
): Promise<InfiniteData<TData, TPageParam>> {
448-
return this.infiniteQuery(options)
467+
return this.fetchQuery(options as any)
449468
}
450469

451470
prefetchInfiniteQuery<
@@ -463,7 +482,7 @@ export class QueryClient {
463482
TPageParam
464483
>,
465484
): Promise<void> {
466-
return this.infiniteQuery(options).then(noop).catch(noop)
485+
return this.fetchInfiniteQuery(options).then(noop).catch(noop)
467486
}
468487

469488
ensureInfiniteQueryData<

packages/query-core/src/types.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,17 @@ export interface FetchQueryOptions<
514514
TData = TQueryFnData,
515515
TQueryKey extends QueryKey = QueryKey,
516516
TPageParam = never,
517-
> extends Omit<
518-
QueryExecuteOptions<
519-
TQueryFnData,
520-
TError,
521-
TData,
522-
TData,
523-
TQueryKey,
524-
TPageParam
525-
>,
526-
'select'
527-
> {}
517+
> extends WithRequired<
518+
QueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
519+
'queryKey'
520+
> {
521+
initialPageParam?: never
522+
/**
523+
* The time in milliseconds after data is considered stale.
524+
* If the data is fresh it will be returned from the cache.
525+
*/
526+
staleTime?: StaleTimeFunction<TQueryFnData, TError, TData, TQueryKey>
527+
}
528528

529529
export interface EnsureQueryDataOptions<
530530
TQueryFnData = unknown,

0 commit comments

Comments
 (0)