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