Skip to content

Commit e419ab1

Browse files
jxomautofix-ci[bot]
andauthoredOct 9, 2024··
fix(query-core): new cache entry check (#8151)
* fix: queryKeyHashFn * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 332c526 commit e419ab1

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎packages/react-query/src/__tests__/useQuery.test.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -4971,6 +4971,29 @@ describe('useQuery', () => {
49714971
expect(renders).toBe(hashes)
49724972
})
49734973

4974+
it('should hash query keys that contain bigints given a supported query hash function', async () => {
4975+
const key = [queryKey(), 1n]
4976+
4977+
function queryKeyHashFn(x: any) {
4978+
return JSON.stringify(x, (_, value) => {
4979+
if (typeof value === 'bigint') return value.toString()
4980+
return value
4981+
})
4982+
}
4983+
4984+
function Page() {
4985+
useQuery({ queryKey: key, queryFn: () => 'test', queryKeyHashFn })
4986+
return null
4987+
}
4988+
4989+
renderWithClient(queryClient, <Page />)
4990+
4991+
await sleep(10)
4992+
4993+
const query = queryClient.getQueryCache().get(queryKeyHashFn(key))
4994+
expect(query?.state.data).toBe('test')
4995+
})
4996+
49744997
it('should refetch when changed enabled to true in error state', async () => {
49754998
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()
49764999
queryFn.mockImplementation(async () => {

‎packages/react-query/src/useBaseQuery.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ export function useBaseQuery<
7070
useClearResetErrorBoundary(errorResetBoundary)
7171

7272
// this needs to be invoked before creating the Observer because that can create a cache entry
73-
const isNewCacheEntry = !client.getQueryState(options.queryKey)
73+
const isNewCacheEntry = !client
74+
.getQueryCache()
75+
.get(defaultedOptions.queryHash)
7476

7577
const [observer] = React.useState(
7678
() =>

0 commit comments

Comments
 (0)
Please sign in to comment.