So right now I'm working on a system where a user signs up, but will be assigned a specific type of profile. For example:
- Teacher Profile
- Student Profile
- Staff Profile
The schema looks a bit like this:
type User {
id: String!
email: String!
profile: Profile # Profile linked to the Profile Union Type
...
}
union Profile = TeacherProfile | StudentProfile | StaffProfile
type TeacherProfile {
id: String!
subjects: [Subject]
}
type StudentProfile {
id: String!
guidanceTeacher: User
}
This lets me write a query like this:
{
listUsers {
profile {
... on TeacherProfile {
subjects {
name
}
}
... on StudentProfile {
guidanceTeacher {
name
}
}
}
}
}
This is fine and easy to reason through. But what is the best way to structure this on the backend?
Right now I'm thinking of making the following models and tables:
Profile
TeacherProfile
StudentProfile
The Profile
table would act as a many-to-many
table which links a user ID with the ID of their teacher profile, or student profile (with the appropriate columns in the table). Then I'd need to write a resolver to pull the correct ID and type.
This feels very inefficient though and I'm sure there's a better way of doing it. So in the situation where I want to be able to access different profile types through a single field in GraphQL but with different table structures for each profile type, how can I best achieve this?
Thanks!