Skip to content

Commit 7f172a0

Browse files
feat: reset query utils (#1375)
* feat: reset query utils * Update queryClient.ts * Reset query docs and tests (#1393) * docs: Add resetQueries * test: Add resetQueries tests * remove unnecessary queryCache.reset methods Co-authored-by: Aaron Jensen <aaronjensen@gmail.com>
1 parent 67f77ae commit 7f172a0

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

docs/src/pages/reference/QueryClient.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Its available methods are:
3232
- [`refetchQueries`](#queryclientrefetchqueries)
3333
- [`cancelQueries`](#queryclientcancelqueries)
3434
- [`removeQueries`](#queryclientremovequeries)
35+
- [`resetQueries`](#queryclientresetqueries)
3536
- [`isFetching`](#queryclientisfetching)
3637
- [`getDefaultOptions`](#queryclientsetdefaultoptions)
3738
- [`setDefaultOptions`](#queryclientgetdefaultoptions)
@@ -274,6 +275,30 @@ queryClient.removeQueries(queryKey, { exact: true })
274275

275276
This method does not return anything
276277

278+
## `queryClient.resetQueries`
279+
280+
The `resetQueries` method can be used to reset queries in the cache to their
281+
initial state based on their query keys or any other functionally accessible
282+
property/state of the query.
283+
284+
This will notify subscribers &mdash; unlike `clear`, which removes all
285+
subscribers &mdash; and reset the query to its pre-loaded state &mdash; unlike
286+
`invalidateQueries`. If a query has `initialData`, the query's data will be
287+
reset to that.
288+
289+
```js
290+
queryClient.resetQueries(queryKey, { exact: true })
291+
```
292+
293+
**Options**
294+
295+
- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
296+
- `filters?: QueryFilters`: [Query Filters](../guides/query-filters)
297+
298+
**Returns**
299+
300+
This method does not return anything
301+
277302
## `queryClient.isFetching`
278303

279304
This `isFetching` method returns an `integer` representing how many queries, if any, in the cache are currently fetching (including background-fetching, loading new pages, or loading more infinite query results)

src/core/query.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
124124
queryKey: QueryKey
125125
queryHash: string
126126
options!: QueryOptions<TData, TError, TQueryFnData>
127+
initialState: QueryState<TData, TError>
127128
state: QueryState<TData, TError>
128129
cacheTime!: number
129130

@@ -141,7 +142,8 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
141142
this.cache = config.cache
142143
this.queryKey = config.queryKey
143144
this.queryHash = config.queryHash
144-
this.state = config.state || this.getDefaultState(this.options)
145+
this.initialState = config.state || this.getDefaultState(this.options)
146+
this.state = this.initialState
145147
this.scheduleGc()
146148
}
147149

@@ -220,6 +222,11 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
220222
this.cancel()
221223
}
222224

225+
reset(): void {
226+
this.destroy()
227+
this.setState(this.initialState)
228+
}
229+
223230
isActive(): boolean {
224231
return this.observers.some(observer => observer.options.enabled !== false)
225232
}

src/core/queryClient.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ export class QueryClient {
132132
})
133133
}
134134

135+
resetQueries(filters?: QueryFilters): void
136+
resetQueries(queryKey?: QueryKey, filters?: QueryFilters): void
137+
resetQueries(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): void {
138+
const [filters] = parseFilterArgs(arg1, arg2)
139+
const queryCache = this.queryCache
140+
notifyManager.batch(() => {
141+
queryCache.findAll(filters).forEach(query => {
142+
query.reset()
143+
})
144+
})
145+
}
146+
135147
cancelQueries(filters?: QueryFilters, options?: CancelOptions): Promise<void>
136148
cancelQueries(
137149
queryKey?: QueryKey,

src/core/tests/queryCache.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,56 @@ describe('queryCache', () => {
948948
expect(queryFn2).toHaveBeenCalledTimes(1)
949949
})
950950

951+
test('should notify listeners when a query is reset', async () => {
952+
const key = queryKey()
953+
954+
const callback = jest.fn()
955+
956+
await queryClient.prefetchQuery(key, () => 'data')
957+
958+
queryCache.subscribe(callback)
959+
960+
queryClient.resetQueries(key)
961+
962+
expect(callback).toHaveBeenCalled()
963+
})
964+
965+
test('resetQueries should reset query', async () => {
966+
const key = queryKey()
967+
968+
await queryClient.prefetchQuery(key, () => 'data')
969+
970+
let state = queryClient.getQueryState(key)
971+
expect(state?.data).toEqual('data')
972+
expect(state?.status).toEqual('success')
973+
974+
queryClient.resetQueries(key)
975+
976+
state = queryClient.getQueryState(key)
977+
978+
expect(state).toBeTruthy()
979+
expect(state?.data).toBeUndefined()
980+
expect(state?.status).toEqual('idle')
981+
})
982+
983+
test('resetQueries should reset query data to initial data if set', async () => {
984+
const key = queryKey()
985+
986+
await queryClient.prefetchQuery(key, () => 'data', {
987+
initialData: 'initial',
988+
})
989+
990+
let state = queryClient.getQueryState(key)
991+
expect(state?.data).toEqual('data')
992+
993+
queryClient.resetQueries(key)
994+
995+
state = queryClient.getQueryState(key)
996+
997+
expect(state).toBeTruthy()
998+
expect(state?.data).toEqual('initial')
999+
})
1000+
9511001
test('find should filter correctly', async () => {
9521002
const key = queryKey()
9531003
const testCache = new QueryCache()

0 commit comments

Comments
 (0)