Skip to content

Commit 0b8c55c

Browse files
authored
fix: single query methods should be exact by default (#1756)
1 parent 2da342c commit 0b8c55c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/core/queryCache.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
109109
arg2?: QueryFilters
110110
): Query<TQueryFnData, TError, TData> | undefined {
111111
const [filters] = parseFilterArgs(arg1, arg2)
112+
113+
if (typeof filters.exact === 'undefined') {
114+
filters.exact = true
115+
}
116+
112117
return this.queries.find(query => matchQuery(filters, query))
113118
}
114119

src/core/tests/queryClient.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,25 @@ describe('queryClient', () => {
153153
})
154154
})
155155

156+
describe('getQueryData', () => {
157+
test('should return the query data if the query is found', () => {
158+
const key = queryKey()
159+
queryClient.setQueryData([key, 'id'], 'bar')
160+
expect(queryClient.getQueryData([key, 'id'])).toBe('bar')
161+
})
162+
163+
test('should return undefined if the query is not found', () => {
164+
const key = queryKey()
165+
expect(queryClient.getQueryData(key)).toBeUndefined()
166+
})
167+
168+
test('should match exact by default', () => {
169+
const key = queryKey()
170+
queryClient.setQueryData([key, 'id'], 'bar')
171+
expect(queryClient.getQueryData([key])).toBeUndefined()
172+
})
173+
})
174+
156175
describe('fetchQuery', () => {
157176
// https://github.com/tannerlinsley/react-query/issues/652
158177
test('should not retry by default', async () => {

0 commit comments

Comments
 (0)