* 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
>,
)
| 554 | * ``` |
| 555 | */ |
| 556 | async query< |
| 557 | TQueryFnData, |
| 558 | TError = DefaultError, |
| 559 | TData = TQueryFnData, |
| 560 | TQueryData = TQueryFnData, |
| 561 | TQueryKey extends QueryKey = QueryKey, |
| 562 | TPageParam = never, |
| 563 | >( |
| 564 | options: QueryExecuteOptions< |
| 565 | TQueryFnData, |
| 566 | TError, |
| 567 | TData, |
| 568 | TQueryData, |
| 569 | TQueryKey, |
| 570 | TPageParam |
| 571 | >, |
| 572 | ): Promise<TData> { |
| 573 | const defaultedOptions = this.defaultQueryOptions(options) |
| 574 | |
| 575 | // https://github.com/tannerlinsley/react-query/issues/652 |
| 576 | if (defaultedOptions.retry === undefined) { |
| 577 | defaultedOptions.retry = false |
| 578 | } |
| 579 | |
| 580 | const query = this.#queryCache.build(this, defaultedOptions) |
| 581 | |
| 582 | const isStale = query.isStaleByTime( |
| 583 | resolveQueryValue(defaultedOptions.staleTime, query), |
| 584 | ) |
| 585 | |
| 586 | const queryData = isStale |
| 587 | ? await query.fetch(defaultedOptions) |
| 588 | : (query.state.data as TQueryData) |
| 589 | |
| 590 | const select = defaultedOptions.select |
| 591 | |
| 592 | if (select) { |
| 593 | return select(queryData) |
| 594 | } |
| 595 | |
| 596 | return queryData as unknown as TData |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * @deprecated Use queryClient.query(options) instead. This method will be removed in the next major version. |
no test coverage detected