mirror of
https://github.com/TanStack/query.git
synced 2026-11-18 19:33:22 +00:00
1.2 KiB
1.2 KiB
TanStack Angular Query Auto Refetching Example
A todo list that polls the server with refetchInterval. Adding a todo or clearing the list invalidates the query.
How it works
src/app/services/tasks/queries.tshas two@Injectableclasses:TasksQueries, whoselist()returnsqueryOptions, andTasksMutations, whosecreate()anddeleteAll()returnmutationOptions. Both convert theHttpClientcalls fromsrc/app/services/tasks/api.tswithlastValueFrom.src/app/components/auto-refetching.component.tsspreadslist()intoinjectQuerywithrefetchInterval: this.intervalMs(), so changing theintervalMssignal from the input changes the polling rate. The template lights a dot whileisFetching()istrue.- In
src/app/components/auto-refetching.component.ts,addMutationandclearMutationare created withinjectMutation, and both invalidate the list'squeryKeyon success. src/app/interceptor/tasks-mock.interceptor.tsis anHttpInterceptorFn, registered withwithInterceptorsinsrc/app/app.config.ts, that answers/api/tasksfromlocalStorageafter 100 ms forGET,POSTandDELETE.
To run this example:
npm installnpm start