mirror of
https://github.com/TanStack/query.git
synced 2026-11-16 22:50:20 +00:00
* 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
|
|
|---|---|---|
| .. | ||
| .devcontainer | ||
| public | ||
| src | ||
| .gitignore | ||
| angular.json | ||
| eslint.config.js | ||
| package.json | ||
| README.md | ||
| tsconfig.app.json | ||
| tsconfig.json | ||
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.tshas two@Injectableclasses:TasksQueries, whoselist()returnsqueryOptions, andTasksMutations, whosecreate()returnsmutationOptions.src/app/services/tasks/api.tsposts to/api/tasks-wrong-urlinstead whenfailMutationistrue.src/app/components/optimistic-updates.component.tsreads the list withinjectQueryand creates the task withinjectMutation.onMutatecancels the list query, saves the previous list, and appends the new task withqueryClient.setQueryData.- In
src/app/components/optimistic-updates.component.ts,onErrorputs the saved list back, andonSettledinvalidates 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.tsis anHttpInterceptorFn, registered withwithInterceptorsinsrc/app/app.config.ts, that stores tasks insessionStorageand responds after 1 second. Requests to/api/tasks-wrong-urlget a 500 error.
To run this example:
npm installnpm start