custom useQuery
Hey guys, ive been working on this and stumping myself for actually 8 hours today. I figured it would be pretty easy to create a useAuthenticatedQuery hook to simply respond with the access token in the queryFn, but with typescript it has been a total nightmare. Any help would be appreciated
This is the default query that im using out of the box in all of my screens. I dont want to add the access token to the key every time manually, and I dont want to call the useAuth hook for the access token every time.
this code is close to what i want to accomplish (roughly) but typescript has made it hell.
I understand that i could not attach the access token as context, and rather call context.queryKey and get it from that array, but I cannot add the access token as a query key without typescript throwing a fit. (err below)
Any help is appreciated
5 Replies
ambitious-aquaOP•3y ago
This is the closest ive gotten
but that requires the function to look like this when called
Which is not only ugly, but also still doesnt solve the problem of the access token in the keys, and im posituive theres a better way
extended-salmon•3y ago
Have you considered using request interceptors to achieve this? It'll remove the bearer token concern from your wrapper hooks entirely
ambitious-aquaOP•3y ago
I was going to, but then I wouldnt be able to invalidate queries on logout right?
however, I did come up with a solution i believe, if you wouldnt mind taking a look to see if this seems proper
extended-salmon•3y ago
You would be able to. You can do this imperatively via methods on the query client
ambitious-aquaOP•3y ago
oh yeah i forgot about that, good point