代码之家  ›  专栏  ›  技术社区  ›  MBehtemam Erick

在realizations上查找相等字段的GraphQL查询

  •  0
  • MBehtemam Erick  · 技术社区  · 6 年前

    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 类型为 . 现在我想查询Poll并找到具有相同值的特定用户(id或email) usersPrediction -> predict Poll -> result .

    query{
      userPolls(where:{user:{id:"someid"}}){
    
      }
    }
    

    但在这里我不知道如何找到与民调预测相等的用户结果。如果我的计划有问题,请告诉我。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Matthias Oertel    6 年前

    我想不出在单个查询中表达这一点的方法,但是由于您使用的是枚举,所以只有三个类似的查询。

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

    这将给你所有的用户谁预测一个主场胜利,而结果是一个主场胜利。然后,您可以对其他两个枚举值执行相同的查询并汇总结果。然后,所有预测了正确结果的用户。您可以通过命名将查询一次性发送到Prisma。

    希望有帮助

        2
  •  1
  •   galkin    6 年前

    你能把你的 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

    推荐文章