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

Function ensureQueryFn

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

Source from the content-addressed store, hash-verified

638 * @returns The function to call for the fetch.
639 */
640export function ensureQueryFn<
641 TQueryFnData = unknown,
642 TQueryKey extends QueryKey = QueryKey,
643>(
644 options: {
645 queryFn?: QueryFunction<TQueryFnData, TQueryKey> | SkipToken
646 queryHash?: string
647 },
648 fetchOptions?: FetchOptions<TQueryFnData>,
649): QueryFunction<TQueryFnData, TQueryKey> {
650 if (process.env.NODE_ENV !== 'production') {
651 if (options.queryFn === skipToken) {
652 console.error(
653 `Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${options.queryHash}'`,
654 )
655 }
656 }
657
658 // if we attempt to retry a fetch that was triggered from an initialPromise
659 // when we don't have a queryFn yet, we can't retry, so we just return the already rejected initialPromise
660 // if an observer has already mounted, we will be able to retry with that queryFn
661 if (!options.queryFn && fetchOptions?.initialPromise) {
662 return () => fetchOptions.initialPromise!
663 }
664
665 if (!options.queryFn || options.queryFn === skipToken) {
666 return () =>
667 Promise.reject(new Error(`Missing queryFn: '${options.queryHash}'`))
668 }
669
670 return options.queryFn
671}
672
673/**
674 * Resolves a `throwOnError` option to a boolean.

Callers 4

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

Calls

no outgoing calls

Tested by

no test coverage detected