Skip to content

Commit 0cbfc5e

Browse files
authored
refactor(query-core): extract context creation to functions and move type assertions (#9210)
This refactor extracts the creation of the query function context and fetch context into dedicated helper functions (e.g., `createQueryFnContext`), improving code readability. This change: - Moves type assertions and type handling higher up, reducing inline type noise and making type boundaries clearer. - Replaces ad-hoc object construction with a function that does one thing (build query contexts). No functional behavior is changed; this is a structural and type-safety improvement.
1 parent 7c5a8b6 commit 0cbfc5e

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

‎packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,24 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
5454
return Promise.resolve(data)
5555
}
5656

57-
const queryFnContext: OmitKeyof<
58-
QueryFunctionContext<QueryKey, unknown>,
59-
'signal'
60-
> = {
61-
client: context.client,
62-
queryKey: context.queryKey,
63-
pageParam: param,
64-
direction: previous ? 'backward' : 'forward',
65-
meta: context.options.meta,
57+
const createQueryFnContext = () => {
58+
const queryFnContext: OmitKeyof<
59+
QueryFunctionContext<QueryKey, unknown>,
60+
'signal'
61+
> = {
62+
client: context.client,
63+
queryKey: context.queryKey,
64+
pageParam: param,
65+
direction: previous ? 'backward' : 'forward',
66+
meta: context.options.meta,
67+
}
68+
addSignalProperty(queryFnContext)
69+
return queryFnContext as QueryFunctionContext<QueryKey, unknown>
6670
}
6771

68-
addSignalProperty(queryFnContext)
72+
const queryFnContext = createQueryFnContext()
6973

70-
const page = await queryFn(
71-
queryFnContext as QueryFunctionContext<QueryKey, unknown>,
72-
)
74+
const page = await queryFn(queryFnContext)
7375

7476
const { maxPages } = context.options
7577
const addTo = previous ? addToStart : addToEnd

‎packages/query-core/src/query.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type {
1818
FetchStatus,
1919
InitialDataFunction,
2020
OmitKeyof,
21-
QueryFunction,
2221
QueryFunctionContext,
2322
QueryKey,
2423
QueryMeta,
@@ -431,48 +430,59 @@ export class Query<
431430
const queryFn = ensureQueryFn(this.options, fetchOptions)
432431

433432
// Create query function context
434-
const queryFnContext: OmitKeyof<
435-
QueryFunctionContext<TQueryKey>,
436-
'signal'
437-
> = {
438-
client: this.#client,
439-
queryKey: this.queryKey,
440-
meta: this.meta,
433+
const createQueryFnContext = (): QueryFunctionContext<TQueryKey> => {
434+
const queryFnContext: OmitKeyof<
435+
QueryFunctionContext<TQueryKey>,
436+
'signal'
437+
> = {
438+
client: this.#client,
439+
queryKey: this.queryKey,
440+
meta: this.meta,
441+
}
442+
addSignalProperty(queryFnContext)
443+
return queryFnContext as QueryFunctionContext<TQueryKey>
441444
}
442445

443-
addSignalProperty(queryFnContext)
446+
const queryFnContext = createQueryFnContext()
444447

445448
this.#abortSignalConsumed = false
446449
if (this.options.persister) {
447450
return this.options.persister(
448-
queryFn as QueryFunction<any>,
449-
queryFnContext as QueryFunctionContext<TQueryKey>,
451+
queryFn,
452+
queryFnContext,
450453
this as unknown as Query,
451454
)
452455
}
453456

454-
return queryFn(queryFnContext as QueryFunctionContext<TQueryKey>)
457+
return queryFn(queryFnContext)
455458
}
456459

457460
// Trigger behavior hook
458-
const context: OmitKeyof<
459-
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
460-
'signal'
461-
> = {
462-
fetchOptions,
463-
options: this.options,
464-
queryKey: this.queryKey,
465-
client: this.#client,
466-
state: this.state,
467-
fetchFn,
461+
const createFetchContext = (): FetchContext<
462+
TQueryFnData,
463+
TError,
464+
TData,
465+
TQueryKey
466+
> => {
467+
const context: OmitKeyof<
468+
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
469+
'signal'
470+
> = {
471+
fetchOptions,
472+
options: this.options,
473+
queryKey: this.queryKey,
474+
client: this.#client,
475+
state: this.state,
476+
fetchFn,
477+
}
478+
479+
addSignalProperty(context)
480+
return context as FetchContext<TQueryFnData, TError, TData, TQueryKey>
468481
}
469482

470-
addSignalProperty(context)
483+
const context = createFetchContext()
471484

472-
this.options.behavior?.onFetch(
473-
context as FetchContext<TQueryFnData, TError, TData, TQueryKey>,
474-
this as unknown as Query,
475-
)
485+
this.options.behavior?.onFetch(context, this as unknown as Query)
476486

477487
// Store state in case the current fetch needs to be reverted
478488
this.#revertState = this.state

0 commit comments

Comments
 (0)