Open History to see the last commit
1# TanStack Angular Query Optimistic Updates Example
2β
3Adds todos optimistically by writing to the cache in `onMutate` and rolling back on error.
4β
5## How it works
6β
7- `src/app/services/tasks/queries.ts` has two `@Injectable` classes: `TasksQueries`, whose `list()` returns `queryOptions`, and `TasksMutations`, whose `create()` returns `mutationOptions`. `src/app/services/tasks/api.ts` posts to `/api/tasks-wrong-url` instead when `failMutation` is `true`.
8- `src/app/components/optimistic-updates.component.ts` reads the list with `injectQuery` and creates the task with `injectMutation`. `onMutate` cancels the list query, saves the previous list, and appends the new task with `queryClient.setQueryData`.
9- In `src/app/components/optimistic-updates.component.ts`, `onError` puts the saved list back, and `onSettled` invalidates the list so it is refetched from the mock server. The "Fail Mutation" checkbox makes the next mutation fail.
10- `src/app/interceptor/tasks-mock.interceptor.ts` is an `HttpInterceptorFn`, registered with `withInterceptors` in `src/app/app.config.ts`, that stores tasks in `sessionStorage` and responds after 1 second. Requests to `/api/tasks-wrong-url` get a 500 error.
11β
12To run this example:
13β
14- `npm install`
15- `npm start`