* 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
>,
)
| 597 | * ``` |
| 598 | */ |
| 599 | async query< |
| 600 | TQueryFnData, |
| 601 | TError = DefaultError, |
| 602 | TData = TQueryFnData, |
| 603 | TQueryData = TQueryFnData, |
| 604 | TQueryKey extends QueryKey = QueryKey, |
| 605 | TPageParam = never, |
| 606 | >( |
| 607 | options: QueryExecuteOptions< |
| 608 | TQueryFnData, |
| 609 | TError, |
| 610 | TData, |
| 611 | TQueryData, |
| 612 | TQueryKey, |
| 613 | TPageParam |
| 614 | >, |
| 615 | ): Promise<TData> { |
| 616 | const defaultedOptions = this.defaultQueryOptions(options) |
| 617 | |
| 618 | // https://github.com/tannerlinsley/react-query/issues/652 |
| 619 | if (defaultedOptions.retry === undefined) { |
| 620 | defaultedOptions.retry = false |
| 621 | } |
| 622 | |
| 623 | const query = this.#queryCache.build(this, defaultedOptions) |
| 624 | |
| 625 | const isStale = query.isStaleByTime( |
| 626 | resolveQueryValue(defaultedOptions.staleTime, query), |
| 627 | ) |
| 628 | |
| 629 | const queryData = isStale |
| 630 | ? await query.fetch(defaultedOptions) |
| 631 | : (query.state.data as TQueryData) |
| 632 | |
| 633 | const select = defaultedOptions.select |
| 634 | |
| 635 | if (select) { |
| 636 | return select(queryData) |
| 637 | } |
| 638 | |
| 639 | return queryData as unknown as TData |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Fetches and caches a query, or returns its cached data if it isn't stale. Unlike in observers, |
no test coverage detected