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

Method fetch

packages/query-core/src/query.ts:397–627  ·  view source on GitHub ↗
(
    options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
    fetchOptions?: FetchOptions<TQueryFnData>,
  )

Source from the content-addressed store, hash-verified

395 }
396
397 async fetch(
398 options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
399 fetchOptions?: FetchOptions<TQueryFnData>,
400 ): Promise<TData> {
401 if (
402 this.state.fetchStatus !== 'idle' &&
403 // If the promise in the retryer is already rejected, we have to definitely
404 // re-start the fetch; there is a chance that the query is still in a
405 // pending state when that happens
406 this.#retryer?.status() !== 'rejected'
407 ) {
408 if (this.state.data !== undefined && fetchOptions?.cancelRefetch) {
409 // Silently cancel current fetch if the user wants to cancel refetch
410 this.cancel({ silent: true })
411 } else if (this.#retryer) {
412 // make sure that retries that were potentially cancelled due to unmounts can continue
413 this.#retryer.continueRetry()
414 // Return current promise if we are already fetching
415 return this.#retryer.promise
416 }
417 }
418
419 // Update config if passed, otherwise the config from the last execution is used
420 if (options) {
421 this.setOptions(options)
422 }
423
424 // Use the options from the first observer with a query function if no function is found.
425 // This can happen when the query is hydrated or created with setQueryData.
426 if (!this.options.queryFn) {
427 const observer = this.observers.find((x) => x.options.queryFn)
428 if (observer) {
429 this.setOptions(observer.options)
430 }
431 }
432
433 if (process.env.NODE_ENV !== 'production') {
434 if (!Array.isArray(this.options.queryKey)) {
435 console.error(
436 `As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`,
437 )
438 }
439 }
440
441 const abortController = new AbortController()
442
443 // Adds an enumerable signal property to the object that
444 // which sets abortSignalConsumed to true when the signal
445 // is read.
446 const addSignalProperty = (object: unknown) => {
447 Object.defineProperty(object, 'signal', {
448 enumerable: true,
449 get: () => {
450 this.#abortSignalConsumed = true
451 return abortController.signal
452 },
453 })
454 }

Callers 12

setupDevtoolsFunction · 0.45
handleRefetchFunction · 0.45
QueryDetailsFunction · 0.45
persisterFnFunction · 0.45
refetchQueriesMethod · 0.45
queryMethod · 0.45
fetchQueryMethod · 0.45
hydrateFunction · 0.45
fetchNextPageMethod · 0.45
fetchPreviousPageMethod · 0.45
query.test.tsxFile · 0.45

Calls 8

cancelMethod · 0.95
setOptionsMethod · 0.95
#dispatchMethod · 0.95
setStateMethod · 0.95
setDataMethod · 0.95
infiniteQueryBehaviorFunction · 0.90
createRetryerFunction · 0.90
findMethod · 0.45

Tested by

no test coverage detected