Skip to content

Commit

Permalink
fix(query-core): ensureQueryData should not call fetchQuery for queri…
Browse files Browse the repository at this point in the history
…es with falsy data (#6261)

* fix(query-core): ensureQueryData call fetchQuery when the query actually does not exist

* fix: all the returned data covered by the test case to be falsy values

---------

Co-authored-by: goo <goo@tossbank.com>
  • Loading branch information
LineGu and goo committed Oct 29, 2023
1 parent 7a9d2d0 commit b671071
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/query-core/src/queryClient.ts
Expand Up @@ -132,7 +132,9 @@ export class QueryClient {
): Promise<TData> {
const cachedData = this.getQueryData<TData>(options.queryKey)

return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(options)
return cachedData !== undefined
? Promise.resolve(cachedData)
: this.fetchQuery(options)
}

getQueriesData<TQueryFnData = unknown>(
Expand Down
11 changes: 11 additions & 0 deletions packages/query-core/src/tests/queryClient.test.tsx
Expand Up @@ -433,6 +433,17 @@ describe('queryClient', () => {
).resolves.toEqual('bar')
})

test('should return the cached query data if the query is found and cached query data is falsey', async () => {
const key = queryKey()
const queryFn = () => Promise.resolve(0)

queryClient.setQueryData([key, 'id'], null)

await expect(
queryClient.ensureQueryData({ queryKey: [key, 'id'], queryFn }),
).resolves.toEqual(null)
})

test('should call fetchQuery and return its results if the query is not found', async () => {
const key = queryKey()
const queryFn = () => Promise.resolve('data')
Expand Down

0 comments on commit b671071

Please sign in to comment.