0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/examples/angular/optimistic-updates
Wonsuk Choi cb62f4f2ed
chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555)
* chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors

* chore(pnpm-lock.yaml): run 'pnpm dedupe'

* chore(scripts): move 'oxlint/plugin.ts' to 'oxlint-plugin.ts' to match the flat layout of 'scripts'

* chore(pnpm-workspace.yaml): update '@typescript-eslint/parser' and '@typescript-eslint/rule-tester' to '^8.72.1' to match the deduped '@typescript-eslint/utils'
History 2026-11-16 22:25:25 +09:00
..
.devcontainer docs(angular-query): Add Optimistic Updates Example for Angular (#8439) 2025-02-18 20:06:08 +01:00
public docs(examples): use the TanStack Query emblem as the favicon in every example (#12578) 2026-11-12 13:33:29 +09:00
src chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555) 2026-11-16 22:25:25 +09:00
.gitignore docs(examples): use each framework's template '.gitignore' in every example (#12592) 2026-11-13 10:57:29 +09:00
angular.json docs(examples): use the TanStack Query emblem as the favicon in every example (#12578) 2026-11-12 13:33:29 +09:00
eslint.config.js chore(examples/*/eslint.config): use 'flat/recommended-strict' from '@tanstack/eslint-plugin-query' (#12385) 2026-10-26 02:55:51 +09:00
package.json ci: Version Packages (#12543) 2026-11-07 00:00:45 +01:00
README.md docs(examples/angular): align 'angular.json', root components, 'OnPush', mock interceptors and 'tsconfig' across the examples (#12593) 2026-11-14 02:55:47 +09:00
tsconfig.app.json docs(angular-query): Add Optimistic Updates Example for Angular (#8439) 2025-02-18 20:06:08 +01:00
tsconfig.json chore(examples/*/tsconfig): include '*.config.ts' and '*.config.js' like the packages (#12408) 2026-10-27 09:11:08 +09:00

TanStack Angular Query Optimistic Updates Example

Adds todos optimistically by writing to the cache in onMutate and rolling back on error.

How it works

  • 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.
  • 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.
  • 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.
  • 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.

To run this example:

  • npm install
  • npm start