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

Function fetchFn

packages/query-core/src/infiniteQueryBehavior.ts:37–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

35 let currentPage = 0
36
37 const fetchFn = async () => {
38 let cancelled = false
39 const signalContext: FetchSignalContext = context
40 const addSignalProperty = (object: object) => {
41 addConsumeAwareSignal(
42 object,
43 () => context.signal,
44 () => (cancelled = true),
45 signalContext[fetchSignal],
46 )
47 }
48
49 const queryFn = ensureQueryFn(context.options, context.fetchOptions)
50
51 // Create function to fetch a page
52 const fetchPage = async (
53 data: InfiniteData<unknown>,
54 param: unknown,
55 previous?: boolean,
56 ): Promise<InfiniteData<unknown>> => {
57 if (cancelled) {
58 return Promise.reject(context.signal.reason)
59 }
60
61 if (param == null && data.pages.length) {
62 return Promise.resolve(data)
63 }
64
65 const createQueryFnContext = () => {
66 const queryFnContext: OmitKeyof<
67 QueryFunctionContext<QueryKey, unknown>,
68 'signal'
69 > = {
70 client: context.client,
71 queryKey: context.queryKey,
72 pageParam: param,
73 direction: previous ? 'backward' : 'forward',
74 meta: context.options.meta,
75 }
76 addSignalProperty(queryFnContext)
77 return queryFnContext as QueryFunctionContext<QueryKey, unknown>
78 }
79
80 const queryFnContext = createQueryFnContext()
81
82 const page = await queryFn(queryFnContext)
83
84 const { maxPages } = context.options
85 return {
86 pages: addToArray(data.pages, page, maxPages, previous),
87 pageParams: addToArray(data.pageParams, param, maxPages, previous),
88 }
89 }
90
91 // fetch next / previous page?
92 if (direction && oldPages.length) {
93 const previous = direction === 'backward'
94 const pageParamFn = previous ? getPreviousPageParam : getNextPageParam

Callers

nothing calls this directly

Calls 3

ensureQueryFnFunction · 0.90
fetchPageFunction · 0.85
getNextPageParamFunction · 0.70

Tested by

no test coverage detected