Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/core/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ export function makeQueryCache({ frozen = false, defaultConfig } = {}) {
const globalListeners = []

const configRef = defaultConfig
? { current: { ...defaultConfigRef.current, ...defaultConfig } }
? {
current: {
shared: {
...defaultConfigRef.current.shared,
...defaultConfig.shared,
},
queries: {
...defaultConfigRef.current.queries,
...defaultConfig.queries,
},
mutations: {
...defaultConfigRef.current.mutations,
...defaultConfig.mutations,
},
},
}
: defaultConfigRef

const queryCache = {
Expand Down
17 changes: 16 additions & 1 deletion src/core/tests/queryCache.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sleep } from './utils'
import { queryCache, queryCaches } from '../'
import { makeQueryCache } from '../queryCache'

describe('queryCache', () => {
afterEach(() => {
Expand Down Expand Up @@ -184,7 +185,10 @@ describe('queryCache', () => {
test('query interval is cleared when unsubscribed to a refetchInterval query', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve('data')
await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0, refetchInterval: 1 })
await queryCache.prefetchQuery(queryKey, fetchData, {
cacheTime: 0,
refetchInterval: 1,
})
const query = queryCache.getQuery(queryKey)
const instance = query.subscribe()
instance.updateConfig(query.config)
Expand Down Expand Up @@ -224,4 +228,15 @@ describe('queryCache', () => {
expect(newQuery.state.markedForGarbageCollection).toBe(false)
expect(newQuery.state.data).toBe('data')
})

test('makeQueryCache merges defaultConfig so providing a queryFn does not overwrite the default queryKeySerializerFn', async () => {
const queryFn = () => 'data'
const queryCache = makeQueryCache({
defaultConfig: { queries: { queryFn } },
})

expect(() => queryCache.buildQuery('test')).not.toThrow(
'config.queryKeySerializerFn is not a function'
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to suggestions on a better way to validate this -- I started by writing this test expecting the exception because that is the current behavior, switched to .not.toThrow and applied my code changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either. But we can make it better in the future if we need to. If tests pass and you think this is good enough, then let me know and I'll merge :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're happy with it, the tests pass and it resolves the issue I was having :)
I had also considered exposing configRef on the queryCache but wasn't sure if increasing the public api to support testing was a good idea 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not expose more api lol. I'm doing too much of that as it is.

})
})