hinsxd
09/23/2018, 6:52 PMstudent or teacher. Student can only createQuestion and teachers can only answerQuestion.
As I come from the REST world, when I first constructed the data model, I started with
type Question {
student: User! @relation(name:"ask")
teacher: User @relation(name:"answer")
}
type User {
askedQuestions: [Question!]! @relation(name:"ask")
answeredQuestions: [Question!]! @relation(name:"answer")
role: Role! // enum Role { New Student Teacher }
}
Quickly I found that a lot of post-query logic has to be done (on choosing which variable to process on getUser for example) on the client side, which should not happen. I thought of two roles sharing the same variable questions because their functions are exclusive, but this would be SO confusing for further development. Then I learnt implements feature of GraphQL and tried the following in `datamodel.graphql`:
type Question {
student: Student!
teacher: Teacher
}
interface User { role: Role! }
type Student implements User { askedQuestions: [Question!]! }
type Teacher implements User { answeredQuestions: [Question!]! }
I omitted the repeated fields for simplicity. I learnt this from https://graphql.org/learn/schema/ , and that is actually what I want: role-based fields. Then I found out that prisma will flatten all implements and give me different types. I started to realize that I should put this in schema.graphql rather than datamodel.graphql. But then, how can I construct my datamodel.graphql? Should I first make a User type like the first paragraph of code, then implement a schema like the second one, followed by resolvers to fetch from ctx.db.Users?
Please let me know if I am on the right track! Thanks so much!