Skip to content

Commit d5c4330

Browse files
committed
refactor(core): derive meta from options
this simplifies code because we don't need to pass meta around manually; we can keep the same public interface with a getter
1 parent 97d9c13 commit d5c4330

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function infiniteQueryBehavior<
7676
const queryFnContext: QueryFunctionContext = {
7777
queryKey: context.queryKey,
7878
pageParam: param,
79-
meta: context.meta,
79+
meta: context.options.meta,
8080
}
8181

8282
addSignalProperty(queryFnContext)

packages/query-core/src/mutation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export class Mutation<
9191
state: MutationState<TData, TError, TVariables, TContext>
9292
options: MutationOptions<TData, TError, TVariables, TContext>
9393
mutationId: number
94-
meta: MutationMeta | undefined
9594

9695
private observers: MutationObserver<TData, TError, TVariables, TContext>[]
9796
private mutationCache: MutationCache
@@ -110,12 +109,15 @@ export class Mutation<
110109
this.logger = config.logger || defaultLogger
111110
this.observers = []
112111
this.state = config.state || getDefaultState()
113-
this.meta = config.meta
114112

115113
this.updateCacheTime(this.options.cacheTime)
116114
this.scheduleGc()
117115
}
118116

117+
get meta(): MutationMeta | undefined {
118+
return this.options.meta
119+
}
120+
119121
setState(state: MutationState<TData, TError, TVariables, TContext>): void {
120122
this.dispatch({ type: 'setState', state })
121123
}

packages/query-core/src/mutationCache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
101101
defaultOptions: options.mutationKey
102102
? client.getMutationDefaults(options.mutationKey)
103103
: undefined,
104-
meta: options.meta,
105104
})
106105

107106
this.add(mutation)

packages/query-core/src/query.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ interface QueryConfig<
3434
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
3535
defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
3636
state?: QueryState<TData, TError>
37-
meta: QueryMeta | undefined
3837
}
3938

4039
export interface QueryState<TData = unknown, TError = unknown> {
@@ -64,7 +63,6 @@ export interface FetchContext<
6463
options: QueryOptions<TQueryFnData, TError, TData, any>
6564
queryKey: TQueryKey
6665
state: QueryState<TData, TError>
67-
meta: QueryMeta | undefined
6866
}
6967

7068
export interface QueryBehavior<
@@ -152,7 +150,6 @@ export class Query<
152150
initialState: QueryState<TData, TError>
153151
revertState?: QueryState<TData, TError>
154152
state: QueryState<TData, TError>
155-
meta: QueryMeta | undefined
156153
isFetchingOptimistic?: boolean
157154

158155
private cache: QueryCache
@@ -176,16 +173,17 @@ export class Query<
176173
this.queryHash = config.queryHash
177174
this.initialState = config.state || getDefaultState(this.options)
178175
this.state = this.initialState
179-
this.meta = config.meta
176+
}
177+
178+
get meta(): QueryMeta | undefined {
179+
return this.options.meta
180180
}
181181

182182
private setOptions(
183183
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
184184
): void {
185185
this.options = { ...this.defaultOptions, ...options }
186186

187-
this.meta = options?.meta
188-
189187
this.updateCacheTime(this.options.cacheTime)
190188
}
191189

@@ -406,7 +404,6 @@ export class Query<
406404
queryKey: this.queryKey,
407405
state: this.state,
408406
fetchFn,
409-
meta: this.meta,
410407
}
411408

412409
addSignalProperty(context)

packages/query-core/src/queryCache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
103103
options: client.defaultQueryOptions(options),
104104
state,
105105
defaultOptions: client.getQueryDefaults(queryKey),
106-
meta: options.meta,
107106
})
108107
this.add(query)
109108
}

packages/query-core/src/tests/query.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,23 @@ describe('query', () => {
596596
expect(query.options.meta).toBeUndefined()
597597
})
598598

599+
test('can use default meta', async () => {
600+
const meta = {
601+
it: 'works',
602+
}
603+
604+
const key = queryKey()
605+
const queryFn = () => 'data'
606+
607+
queryClient.setQueryDefaults(key, { meta })
608+
609+
await queryClient.prefetchQuery(key, queryFn)
610+
611+
const query = queryCache.find(key)!
612+
613+
expect(query.meta).toBe(meta)
614+
})
615+
599616
test('provides meta object inside query function', async () => {
600617
const meta = {
601618
it: 'works',

0 commit comments

Comments
 (0)