Disable refresh and first request
I want to disable the first request made, as I am using tanstack angular experimental together with ngx store, which would be based on actions, and because of that I don't want it to perform the requests when the service is injected, and the only adaptation I verified is to put an enabled: false and then modify the status.
5 Replies
conscious-sapphire•7mo ago
I don't know enough about your code to specifically answer your question but these general principles might apply:
- When authenticating a user which would create a session on the server or set a cookie, use a mutation. A mutation is called imperatively, usually from an event handler such as clicking a login button.
- Try not to mix client and server state. I.e. avoid syncing server state to a client state management solution. It's fine of course to use client state as input for your query key etc.
- When performing a one off data fetch when working with promise based APIs such as Angular Router guards or resolvers consider using QueryClient functions directly such as
queryClient.ensureQueryData
or fetchQuery
.QueryClient | TanStack Query Docs
QueryClient The QueryClient can be used to interact with a cache: tsx import { QueryClient } from '@tanstack/react-query' const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime:...
sensitive-blueOP•7mo ago
Sorry, some information was missing, I will give more context about how I'm trying to use it.
- First, I use the service to make requests and pass any different parameters, such as useAuth which verifies that the user is logged in by getting the token from localStorage and making an API query, while the second being the mutate for when the user is logging in for the first time.
- Using ngx store effects, in my component I am only triggering events for such actions. For example, every time they enter the site, I will trigger the checkAuth event, which will be responsible for making the GET request to verify if they are already logged in.
conscious-sapphire•7mo ago
Setting enabled to false while authentication status shouldn't be checked yet is a good solution IMO
sensitive-blueOP•7mo ago
I'm not sure if this would be the most correct approach, but I ended up doing it this way, by default the enabled is set to false.
conscious-sapphire•7mo ago
It's a bit too complex to my taste and too much RxJS where observables aren't great to manage state. TanStack Query and Angular signals allow for very elegant reactive state management. But when integrating with RxJS based state management perhaps in a code base that already uses that I think this is a fine solution.