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

Function ensureQueryFn

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

Source from the content-addressed store, hash-verified

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