Search code examples
graphqlprismaprisma-graphql

GraphQL query for find equal field on realations


I have a schema for GraphQL like this (it's good to mention that I'm using 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")
}

as you see in UserPoll I have predict with type of PollResult and in Poll I have result with the type of PollResult. now I want to query on Poll and find the specific user (with id or email) that has the same value of usersPrediction -> predict with Poll -> result.

one query that I try is something like this :

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

  }
}

but here I don't know how to find users with equal predict with polls result.If it's the problem with my schema please let me know.


Solution

  • I can't think of a way to express that in a single query, but since you're using an Enum it would only be three similar queries.

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

    This will give you all users that predicted a HOME_WIN when the result was a HOME_WIN. You could then do the same query for the other two enum values and sum up the results. Then you have all users who predicted the correct outcome. You could send the queries in one go to Prisma by naming them.

    Hope that helps