Skip to content

Commit

Permalink
fix(query-core): new cache entry check (#8151)
Browse files Browse the repository at this point in the history
* fix: queryKeyHashFn

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
jxom and autofix-ci[bot] authored Oct 9, 2024
1 parent 332c526 commit e419ab1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4971,6 +4971,29 @@ describe('useQuery', () => {
expect(renders).toBe(hashes)
})

it('should hash query keys that contain bigints given a supported query hash function', async () => {
const key = [queryKey(), 1n]

function queryKeyHashFn(x: any) {
return JSON.stringify(x, (_, value) => {
if (typeof value === 'bigint') return value.toString()
return value
})
}

function Page() {
useQuery({ queryKey: key, queryFn: () => 'test', queryKeyHashFn })
return null
}

renderWithClient(queryClient, <Page />)

await sleep(10)

const query = queryClient.getQueryCache().get(queryKeyHashFn(key))
expect(query?.state.data).toBe('test')
})

it('should refetch when changed enabled to true in error state', async () => {
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()
queryFn.mockImplementation(async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function useBaseQuery<
useClearResetErrorBoundary(errorResetBoundary)

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

const [observer] = React.useState(
() =>
Expand Down

0 comments on commit e419ab1

Please sign in to comment.