Accessing relation fields on logged in user
Say I have a
User
model that is defined with relation fields. For example a user can belong to many organizations. Is there a way to populate context.user
with the related properties? Or do I need to query User
with include
every time I want to read the relation?
I was looking at the docs for accessing the logged in user and didn't find an answer to this specifically.
Wasp version ^0.15.03 Replies
Hi @JakeLoew. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources provided, there isn't a built-in way to automatically populate
context.user
with related properties like organizations in Wasp. The current implementation of context.user
includes the User entity's fields and auth identities, but doesn't automatically include other relations.
According to the Auth Overview documentation, the user
object contains:
1. All fields defined in your User
entity
2. The auth
field with connected auth identities
There's no mention of automatically including other relations like organizations.
To access related fields, you would need to query the User
with include
each time you want to read the relation. This is similar to how you would handle it in a regular Prisma query.
However, there is an open GitHub issue discussing this limitation: Allow adding more info to the user
object that gets passed to the Page. The issue suggests allowing a custom Query for fetching the user, which could potentially include additional relations. But this feature is not yet implemented.
For now, the recommended approach would be to perform additional queries in your React components or server-side operations when you need to access related data that isn't part of the basic user
object.
If you need this functionality frequently, you might consider creating a helper function that fetches the user with the required relations included, which you can then use in your queries and actions as needed.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @JakeLoew, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Yeah, this is not currently possible to extend the built in
context.user
since it's fetched a lot - it's best to keep it minimal and fetch the related fields manually when you need them 🙂Right on 👌 Thanks!