Skip to content

Commit 18ab57f

Browse files
committed
refactor(query-core): extract context creation to functions and move type assertions
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 7a6733f commit 18ab57f

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
@@ -17,7 +17,6 @@ import type {
1717
FetchStatus,
1818
InitialDataFunction,
1919
OmitKeyof,
20-
QueryFunction,
2120
QueryFunctionContext,
2221
QueryKey,
2322
QueryMeta,
@@ -411,48 +410,59 @@ export class Query<
411410
const queryFn = ensureQueryFn(this.options, fetchOptions)
412411

413412
// Create query function context
414-
const queryFnContext: OmitKeyof<
415-
QueryFunctionContext<TQueryKey>,
416-
'signal'
417-
> = {
418-
client: this.#client,
419-
queryKey: this.queryKey,
420-
meta: this.meta,
413+
const createQueryFnContext = (): QueryFunctionContext<TQueryKey> => {
414+
const queryFnContext: OmitKeyof<
415+
QueryFunctionContext<TQueryKey>,
416+
'signal'
417+
> = {
418+
client: this.#client,
419+
queryKey: this.queryKey,
420+
meta: this.meta,
421+
}
422+
addSignalProperty(queryFnContext)
423+
return queryFnContext as QueryFunctionContext<TQueryKey>
421424
}
422425

423-
addSignalProperty(queryFnContext)
426+
const queryFnContext = createQueryFnContext()
424427

425428
this.#abortSignalConsumed = false
426429
if (this.options.persister) {
427430
return this.options.persister(
428-
queryFn as QueryFunction<any>,
429-
queryFnContext as QueryFunctionContext<TQueryKey>,
431+
queryFn,
432+
queryFnContext,
430433
this as unknown as Query,
431434
)
432435
}
433436

434-
return queryFn(queryFnContext as QueryFunctionContext<TQueryKey>)
437+
return queryFn(queryFnContext)
435438
}
436439

437440
// Trigger behavior hook
438-
const context: OmitKeyof<
439-
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
440-
'signal'
441-
> = {
442-
fetchOptions,
443-
options: this.options,
444-
queryKey: this.queryKey,
445-
client: this.#client,
446-
state: this.state,
447-
fetchFn,
441+
const createFetchContext = (): FetchContext<
442+
TQueryFnData,
443+
TError,
444+
TData,
445+
TQueryKey
446+
> => {
447+
const context: OmitKeyof<
448+
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
449+
'signal'
450+
> = {
451+
fetchOptions,
452+
options: this.options,
453+
queryKey: this.queryKey,
454+
client: this.#client,
455+
state: this.state,
456+
fetchFn,
457+
}
458+
459+
addSignalProperty(context)
460+
return context as FetchContext<TQueryFnData, TError, TData, TQueryKey>
448461
}
449462

450-
addSignalProperty(context)
463+
const context = createFetchContext()
451464

452-
this.options.behavior?.onFetch(
453-
context as FetchContext<TQueryFnData, TError, TData, TQueryKey>,
454-
this as unknown as Query,
455-
)
465+
this.options.behavior?.onFetch(context, this as unknown as Query)
456466

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

0 commit comments

Comments
 (0)