[RTKQ] API Calls + onBehalfOf (multiple credentials) #4577
Unanswered
jd-carroll
asked this question in
Q&A
Replies: 1 comment
-
It's the best you can do. Even if you get it working otherwise, the cache key is calculated from the input arguments, so if it wouldn't be present there, data would be merged together for different "behalf ofs". |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am going to lead with an: I don't think this is possible.
Part of our application is focused on helping other people manage their data. So there is the data owner (group or individual) and they can invite someone else (group or individual) to help manage that data.
A normal API call might look like:
POST /api/update-data -H "x-user-id: 1234"
This is where user
1234
is the actual owner of the data.If someone is interacting with data they have been invited to help manage, the call would look like:
POST /api/update-data -H "x-user-id: 1234" -H "x-on-behalf-of: 3579"
Where user
3579
is the owner of the data and user1234
is acting on it.The React application has a context where this
onBehalfOf
value would be set and it would be easy enough to add thatonBehalfOf
to our global auth slice which is where our customsrpFetchQuery
grabs the credentials it needs when making any API call.The challenge is that other data processing might be happening within the application at the same time. Meaning some API calls should not contain the
onBehalfOf
value.Is there a way to expose the
onBehalfOf
value to our customsrpFetchQuery
for only the components under the context provider which provides theonBehalfOf
value? This way components outside of the context provider would continue to have no knowledge ofonBehalfOf
and therefore not include it in the API calls.If the
baseQuery
handler could support hooks, I would use something likeconst onBehalfOf = useOnBehalfOfOrUndefined()
I started looking at if it would be possible to have multiple stores. And while that may work, it introduces a whole host of new challenges making sure the
use
hooks are all using the appropriate contexts. (But maybe this is the correct approach)The approach I am attempting to avoid is adding a required
onBehalfOf
input parameter for every API call. But that feels a little painful (although is likely the easiest / fastest way to get this working).Beta Was this translation helpful? Give feedback.
All reactions