GraphQL查询以查找有关实地的平等字段

时间:2019-03-06 22:30:21

标签: graphql prisma prisma-graphql

我有一个像这样的GraphQL模式(很高兴提到我正在使用Prisma):

enum PollResult {
  HOME_WIN
  AWAY_WIN
  DRAW
}
type UserPoll {
  id: ID! @unique
  user: User!
  predict: PollResult!
}
type Poll {
  id: ID! @unique
  away: Team @relation(name: "AwayTeam")
  home: Team @relation(name: "HomeTeam")
  group: Group!
  country: Country!
  sport: Sport!
  result: PollResult
  state: PollState! @relation(name: "PollState")
  users: [User] @relation(name: "Users")
  usersPrediction: [UserPoll] @relation(name: "UserPoll")
}

如您在UserPoll中看到的,我有predict类型为PollResult的民意调查 我有result类型的PollResult。现在我想在Poll上查询并找到与usersPrediction -> predict具有相同值Poll -> resultquery{ userPolls(where:{user:{id:"someid"}}){ } } 的特定用户(具有ID或电子邮件)。

我尝试的一个查询是这样的:

for(var i = 1; i <= options.imageCount; i++){
    let img = new Image()
    var src = options.imagePath + i + "." + quality + "." + options.imageFormat

    img.onload = () => {
        completeCount += 1

        if(completeCount == options.imageCount){
            callback(true)
        }
    }

    img.src = src
}

但是这里我不知道如何找到与民意测验结果具有相同预测的用户。如果这是我的架构存在的问题,请告诉我。

2 个答案:

答案 0 :(得分:1)

您可以将您的usersPrediction字段替换为三个字段:

  • allUsersPrediction
  • rightUsersPrediction
  • wrongUsersPrediction

然后整个架构将是:

enum PollResult {
  HOME_WIN
  AWAY_WIN
  DRAW
}
type UserPoll {
  id: ID! @unique
  user: User!
  predict: PollResult!
}
type Poll {
  id: ID! @unique
  away: Team @relation(name: "AwayTeam")
  home: Team @relation(name: "HomeTeam")
  group: Group!
  country: Country!
  sport: Sport!
  result: PollResult
  state: PollState! @relation(name: "PollState")
  users: [User] @relation(name: "Users")
  allUsersPrediction: [UserPoll] @relation(name: "UserPoll")
  rightUsersPrediction: [UserPoll] @relation(name: "UserPoll")
  wrongUsersPrediction: [UserPoll] @relation(name: "UserPoll")
}

所需用户将位于rightUsersPrediction[].user

答案 1 :(得分:1)

我想不出一种在单个查询中表达它的方法,但是由于您使用的是枚举,因此只能是三个类似的查询。

query predict_HOME_WIN{
  polls(where:{result: HOME_WIN}){
    usersPrediction(where:{predict: HOME_WIN})
     user{
      id
    }  
  }
}

这将为您提供所有当结果为HOME_WIN时预测HOME_WIN的用户。然后,您可以对其他两个枚举值执行相同的查询,并对结果求和。然后,您拥有所有预测正确结果的用户。您可以通过命名将查询一次性发送给Prisma。

希望有帮助

相关问题