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

Function ensureQueryFn

packages/query-core/src/utils.ts:446–477  ·  view source on GitHub ↗
(
  options: {
    queryFn?: QueryFunction<TQueryFnData, TQueryKey> | SkipToken
    queryHash?: string
  },
  fetchOptions?: FetchOptions<TQueryFnData>,
)

Source from the content-addressed store, hash-verified

444export type SkipToken = typeof skipToken
445
446export function ensureQueryFn<
447 TQueryFnData = unknown,
448 TQueryKey extends QueryKey = QueryKey,
449>(
450 options: {
451 queryFn?: QueryFunction<TQueryFnData, TQueryKey> | SkipToken
452 queryHash?: string
453 },
454 fetchOptions?: FetchOptions<TQueryFnData>,
455): QueryFunction<TQueryFnData, TQueryKey> {
456 if (process.env.NODE_ENV !== 'production') {
457 if (options.queryFn === skipToken) {
458 console.error(
459 `Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${options.queryHash}'`,
460 )
461 }
462 }
463
464 // if we attempt to retry a fetch that was triggered from an initialPromise
465 // when we don't have a queryFn yet, we can't retry, so we just return the already rejected initialPromise
466 // if an observer has already mounted, we will be able to retry with that queryFn
467 if (!options.queryFn && fetchOptions?.initialPromise) {
468 return () => fetchOptions.initialPromise!
469 }
470
471 if (!options.queryFn || options.queryFn === skipToken) {
472 return () =>
473 Promise.reject(new Error(`Missing queryFn: '${options.queryHash}'`))
474 }
475
476 return options.queryFn
477}
478
479export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
480 throwOnError: boolean | T | undefined,

Callers 3

fetchFnFunction · 0.90
fetchFnMethod · 0.90
utils.test.tsxFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected