Skip to content

Commit 2616dcc

Browse files
authored
docs: add refetch documentation (#1043)
1 parent 979527b commit 2616dcc

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

docs/src/pages/docs/api.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ Its available properties and methods are:
361361
- [`prefetchQuery`](#querycacheprefetchquery)
362362
- [`getQueryData`](#querycachegetquerydata)
363363
- [`setQueryData`](#querycachesetquerydata)
364+
- [`refetchQueries`](#querycacherefetchqueries)
364365
- [`invalidateQueries`](#querycacheinvalidatequeries)
365366
- [`cancelQueries`](#querycachecancelqueries)
366367
- [`removeQueries`](#querycacheremovequeries)
@@ -490,6 +491,52 @@ For convenience in syntax, you can also pass an updater function which receives
490491
setQueryData(queryKey, oldData => newData)
491492
```
492493
494+
## `queryCache.refetchQueries`
495+
496+
The `refetchQueries` method can be used to refetch queries based on certain conditions.
497+
498+
Examples:
499+
500+
```js
501+
// refetch all queries:
502+
await queryCache.refetchQueries()
503+
504+
// refetch all stale queries:
505+
await queryCache.refetchQueries([], { stale: true })
506+
507+
// refetch all stale and active queries:
508+
await queryCache.refetchQueries([], { stale: true, active: true })
509+
510+
// refetch all queries partially matching a query key:
511+
await queryCache.refetchQueries(['posts'])
512+
513+
// refetch all queries exactly matching a query key:
514+
await queryCache.refetchQueries(['posts', 1], { exact: true })
515+
```
516+
517+
**Options**
518+
519+
- `queryKeyOrPredicateFn` can either be a [Query Key](#query-keys) or a `Function`
520+
- `queryKey: QueryKey`
521+
- If a query key is passed, queries will be filtered to those where this query key is included in the existing query's query key. This means that if you passed a query key of `'todos'`, it would match queries with the `todos`, `['todos']`, and `['todos', 5]`. See [Query Keys](./guides/queries#query-keys) for more information.
522+
- `query => boolean`
523+
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
524+
- The `exact` option has no effect when using a function
525+
- `exact?: boolean`
526+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Remember to destructure it out of the array!
527+
- `active?: boolean`
528+
- When set to `true` it will refetch active queries.
529+
- When set to `false` it will refetch inactive queries.
530+
- `stale?: boolean`
531+
- When set to `true` it will match on stale queries.
532+
- When set to `false` it will match on fresh queries.
533+
- `throwOnError?: boolean`
534+
- When set to `true`, this method will throw if any of the query refetch tasks fail.
535+
536+
**Returns**
537+
538+
This function returns a promise that will resolve when all of the queries are done being refetched. By default, it **will not** throw an error if any of those queries refetches fail, but this can be configured by setting the `throwOnError` option to `true`
539+
493540
## `queryCache.invalidateQueries`
494541
495542
The `invalidateQueries` method can be used to invalidate and refetch single or multiple queries in the cache based on their query keys or any other functionally accessible property/state of the query. By default, all matching queries are immediately marked as stale and active queries are refetched in the background.

src/core/tests/queryCache.test.tsx

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '../../react/tests/utils'
88
import { QueryCache, queryCache as defaultQueryCache } from '../..'
99
import { isCancelledError, isError } from '../utils'
10+
import { QueryObserver } from '../queryObserver'
1011

1112
describe('queryCache', () => {
1213
test('setQueryData does not crash if query could not be found', () => {
@@ -230,6 +231,89 @@ describe('queryCache', () => {
230231
)
231232
})
232233

234+
test('refetchQueries should refetch all queries when no arguments are given', async () => {
235+
const key1 = queryKey()
236+
const key2 = queryKey()
237+
const queryFn1 = jest.fn()
238+
const queryFn2 = jest.fn()
239+
const cache = new QueryCache()
240+
await cache.fetchQuery(key1, queryFn1)
241+
await cache.fetchQuery(key2, queryFn2)
242+
await cache.refetchQueries()
243+
cache.clear()
244+
expect(queryFn1).toHaveBeenCalledTimes(2)
245+
expect(queryFn2).toHaveBeenCalledTimes(2)
246+
})
247+
248+
test('refetchQueries should be able to refetch all fresh queries', async () => {
249+
const key1 = queryKey()
250+
const key2 = queryKey()
251+
const queryFn1 = jest.fn()
252+
const queryFn2 = jest.fn()
253+
const cache = new QueryCache()
254+
await cache.fetchQuery(key1, queryFn1)
255+
await cache.fetchQuery(key2, queryFn2)
256+
await cache.refetchQueries([], { stale: false })
257+
cache.clear()
258+
expect(queryFn1).toHaveBeenCalledTimes(2)
259+
expect(queryFn2).toHaveBeenCalledTimes(2)
260+
})
261+
262+
test('refetchQueries should be able to refetch all stale queries', async () => {
263+
const key1 = queryKey()
264+
const key2 = queryKey()
265+
const queryFn1 = jest.fn()
266+
const queryFn2 = jest.fn()
267+
const cache = new QueryCache()
268+
await cache.fetchQuery(key1, queryFn1)
269+
await cache.fetchQuery(key2, queryFn2)
270+
cache.getQuery(key1)!.invalidate()
271+
await cache.refetchQueries([], { stale: true })
272+
cache.clear()
273+
expect(queryFn1).toHaveBeenCalledTimes(2)
274+
expect(queryFn2).toHaveBeenCalledTimes(1)
275+
})
276+
277+
test('refetchQueries should be able to refetch all stale and active queries', async () => {
278+
const key1 = queryKey()
279+
const key2 = queryKey()
280+
const queryFn1 = jest.fn()
281+
const queryFn2 = jest.fn()
282+
const cache = new QueryCache()
283+
await cache.fetchQuery(key1, queryFn1)
284+
await cache.fetchQuery(key2, queryFn2)
285+
const query1 = cache.getQuery(key1)!
286+
query1.invalidate()
287+
const observer = query1.subscribe()
288+
await cache.refetchQueries([], { active: true, stale: true })
289+
observer.unsubscribe()
290+
cache.clear()
291+
expect(queryFn1).toHaveBeenCalledTimes(2)
292+
expect(queryFn2).toHaveBeenCalledTimes(1)
293+
})
294+
295+
test('refetchQueries should be able to refetch all inactive queries', async () => {
296+
const key1 = queryKey()
297+
const key2 = queryKey()
298+
const queryFn1 = jest.fn()
299+
const queryFn2 = jest.fn()
300+
const cache = new QueryCache()
301+
await cache.fetchQuery(key1, queryFn1)
302+
await cache.fetchQuery(key2, queryFn2)
303+
const query1 = cache.getQuery(key1)!
304+
const observer = new QueryObserver({
305+
...query1.config,
306+
staleTime: Infinity,
307+
})
308+
const unsubscribe = observer.subscribe()
309+
await cache.refetchQueries([], { active: false })
310+
expect(queryFn1).toHaveBeenCalledTimes(1)
311+
unsubscribe()
312+
cache.clear()
313+
expect(queryFn1).toHaveBeenCalledTimes(1)
314+
expect(queryFn2).toHaveBeenCalledTimes(2)
315+
})
316+
233317
test('getQueries should return queries that partially match queryKey', async () => {
234318
const key1 = queryKey()
235319
const key2 = queryKey()

0 commit comments

Comments
 (0)