0

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!

roo
  • 343
  • 1
  • 5
  • 17
  • You [have some options](https://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database/3579462#3579462) but any approach will be a bit awkward because it won't translate easily to Sequelize models. – Daniel Rearden Dec 16 '19 at 15:48
  • Thanks @DanielRearden - agreed that it's a bit awkward either way. I'm glad to see that my idea seems to match class based inheritance, so at least I'm heading in the right direction. – roo Dec 16 '19 at 16:06

0 Answers0