Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

query-retries.md

66 lines1.5 KB

1---
2id: query-retries
3title: Query Retries
4ref: docs/framework/react/guides/query-retries.md
5replace:
6 {
7 'Provider': 'Plugin',
8 'useQuery': 'injectQuery',
9 'useMutation': 'injectMutation',
10 }
11---
12​
13[//]: # 'Info'
14[//]: # 'Info'
15[//]: # 'Example'
16​
17```ts
18import { injectQuery } from '@tanstack/angular-query-experimental'
19​
20// Make a specific query retry a certain number of times
21const todoQuery = injectQuery(() => ({
22 queryKey: ['todos', 1],
23 queryFn: fetchTodoListPage,
24 retry: 10, // Will retry failed requests 10 times before displaying an error
25}))
26```
27​
28[//]: # 'Example'
29[//]: # 'Example2'
30​
31```ts
32// Configure for all queries
33import {
34 QueryCache,
35 QueryClient,
36 provideTanStackQuery,
37} from '@tanstack/angular-query-experimental'
38​
39const queryClient = new QueryClient({
40 defaultOptions: {
41 queries: {
42 retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
43 },
44 },
45})
46​
47bootstrapApplication(AppComponent, {
48 providers: [provideTanStackQuery(queryClient)],
49})
50```
51​
52[//]: # 'Example2'
53​
54Though it is not recommended, you can obviously override the `retryDelay` function/integer in both the Provider and individual query options. If set to an integer instead of a function the delay will always be the same amount of time:
55​
56[//]: # 'Example3'
57​
58```ts
59const todosQuery = injectQuery(() => ({
60 queryKey: ['todos'],
61 queryFn: fetchTodoList,
62 retryDelay: 1000, // Will always wait 1000ms to retry, regardless of how many retries
63}))
64```
65​
66[//]: # 'Example3'