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

Function fetchFn

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

ensureQueryFnFunction · 0.90
fetchPageFunction · 0.85
getNextPageParamFunction · 0.85

Tested by

no test coverage detected