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

Function ensureQueryFn

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

Source from the content-addressed store, hash-verified

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