j
Extra query keys for functions for invalidation
Is it possible to easily provide extra query hashes for functions?
For example, lets say i have a procedure
GetCurrentState()
With no input.
Before when i used raw react query, i could do
UseQuery({ queryKey: ["currentState", dependency] })
To easily add dependencies to the query key function even if the fetch itself may not need it.
How would i do this? I think modifying queryHashFn option or adding arbitrary input to procedure is hacky
2 replies
best practice for invalidating protected queries upon profile switch
I have an app where users can have multiple profiles. I have a protectedProcedure that does a db check on the user and profile ownership as a base.
on the frontend, once user switches profile, I want to invalidate all queries that are "protected".
Well known option is:
Above will work but switching profile may not always happen in one spot, or through an explicit call. Server might also return a response that may switch the user's profile.
previously to tRPC, I have done attached the userId to the query key to "protected" queries like
queryKey: [arg1, arg2, userId, currentProfileId]
, which ensured revalidation upon switching through any measure.
But this is manual and requires me to add an arbitrary .input()
to the protectedProcedure
I also thought about using queryHashKeyFn
at the global level, but can get finicky with SSR/Suspense and feels too hacky, and not to mention it will invalidate ALL, not just protected.
Any suggestions here?3 replies