GraphQL Prisma - 定义链接到两个用户的 "vote" 类型

GraphQL Prisma - define "vote" type that links to two users

我刚开始使用 Prisma。之前主要使用 firebase 和 mongodb 来定义我的模式。

我正在尝试定义以下架构:

Vote {
    id: ID!
    from: User! # The user who voted
    for: User! # The user that received a vote
    rate: Float!
}

基本上,我想要实现的是让用户能够为其他用户投票(给他们打分)。 例如,在 MongoDB 中,我会通过创建一个单独的集合来做到这一点,如下所示:

{
    id: DocumentID
    from: String // id of the user who voted
    for: String // id of the user that received a vote
    rate: Number
}

在这里,我只是将这些字段(from 和 for)指定为字符串,并在 link 之后通过应用程序逻辑将它们与用户集合一起指定。

当然,在 GraphQL Prisma 中会有所不同。但我仍然对如何建立关系感到困惑。以及下面到底发生了什么。

我如何使用 Prisma GraphQL 创建这样的模式?

当同一类型有多个关系字段时,需要使用@relation指令使其无歧义。

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!
}