Skip to content

Commit 7ac9238

Browse files
fix(queryCache): merge defaultConfig options (TanStack#774)
Providing a `defaultConfig` to `makeQueryCache` will merge `shared`, `queries`, and `mutations` objects instead of overwriting. This allows you to configure only some options.
1 parent 8b5a209 commit 7ac9238

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/core/queryCache.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ export function makeQueryCache({ frozen = false, defaultConfig } = {}) {
1717
const globalListeners = []
1818

1919
const configRef = defaultConfig
20-
? { current: { ...defaultConfigRef.current, ...defaultConfig } }
20+
? {
21+
current: {
22+
shared: {
23+
...defaultConfigRef.current.shared,
24+
...defaultConfig.shared,
25+
},
26+
queries: {
27+
...defaultConfigRef.current.queries,
28+
...defaultConfig.queries,
29+
},
30+
mutations: {
31+
...defaultConfigRef.current.mutations,
32+
...defaultConfig.mutations,
33+
},
34+
},
35+
}
2136
: defaultConfigRef
2237

2338
const queryCache = {

src/core/tests/queryCache.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { sleep } from './utils'
22
import { queryCache, queryCaches } from '../'
3+
import { makeQueryCache } from '../queryCache'
34

45
describe('queryCache', () => {
56
afterEach(() => {
@@ -184,7 +185,10 @@ describe('queryCache', () => {
184185
test('query interval is cleared when unsubscribed to a refetchInterval query', async () => {
185186
const queryKey = 'key'
186187
const fetchData = () => Promise.resolve('data')
187-
await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0, refetchInterval: 1 })
188+
await queryCache.prefetchQuery(queryKey, fetchData, {
189+
cacheTime: 0,
190+
refetchInterval: 1,
191+
})
188192
const query = queryCache.getQuery(queryKey)
189193
const instance = query.subscribe()
190194
instance.updateConfig(query.config)
@@ -224,4 +228,15 @@ describe('queryCache', () => {
224228
expect(newQuery.state.markedForGarbageCollection).toBe(false)
225229
expect(newQuery.state.data).toBe('data')
226230
})
231+
232+
test('makeQueryCache merges defaultConfig so providing a queryFn does not overwrite the default queryKeySerializerFn', async () => {
233+
const queryFn = () => 'data'
234+
const queryCache = makeQueryCache({
235+
defaultConfig: { queries: { queryFn } },
236+
})
237+
238+
expect(() => queryCache.buildQuery('test')).not.toThrow(
239+
'config.queryKeySerializerFn is not a function'
240+
)
241+
})
227242
})

0 commit comments

Comments
 (0)