I've just started using Prisma. Before was mainly using firebase and mongodb to define my schemas.
I'm trying to define the following schema:
Vote {
id: ID!
from: User! # The user who voted
for: User! # The user that received a vote
rate: Float!
}
Basically, what I want to achieve is enable users to vote for other users (give them a score). In, say, MongoDB I would do it by creating a separate collection like following:
{
id: DocumentID
from: String // id of the user who voted
for: String // id of the user that received a vote
rate: Number
}
In here I just specify those fields (from and for) as strings and after link them with the User collection by the application logic.
For sure, it's gonna be different in GraphQL Prisma. But I'm still a bit confused on how the relationships are built. And what really happens underneath.
How can I create such schema using Prisma GraphQL?
When there are more than one relational field to the same type, you need to use the @relation
directive to make it unambiguous.
type Vote {
id: ID! @unique
votingUser: User! @relation(name: "VoteAuthor")
votedUser: User! @relation(name: "VoteReceiver")
rate: Float!
}
type User {
id: ID! @unique
receivedVotes: [Vote!]! @relation(name: "VoteReceiver")
givenVotes: [Vote!]! @relation(name: "VoteAuthor")
name: String!
}