Hello, new to the channel and Prisma - wondering h...
# orm-help
c
Hello, new to the channel and Prisma - wondering how to do filtering with Enums. (I don't see a lot of documentation on using Enums for roles and permissions, so if there's a better way, I'd love to hear about it). Basically I'm trying to write a query that pulls all users that have two different enum statuses. My schema:
Copy code
type Query {
  candidates(role: Role!, accountStatus: AccountStatus!): [User]!
}

type User {
  id: ID!
  firstName: String!
  lastName: String!
  roles: [Role!]!
  accountStatus: AccountStatus!
}
My resolver:
Copy code
candidates(parent, args, ctx, info) {
    return ctx.db.query.users({
      where: {
        role: args.role,
        accountStatus: args.accountStatus,
      },
    }, info);
  },
My query:
Copy code
query CANDIDATES_QUERY {
  candidates(role: FULL_MEMBER, accountStatus: ACTIVE) {
    id
    firstName
    lastName
  }
}
Sadly, since the
Role
enum is defined on the user as an array, it doesn't appear in prisma.graphql's
UserWhereInput
input. AccountStatus does, though.
I really just want to filter the users with an AND operator, but I can't figure that out
The error message from running the query:
Copy code
{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$_v0_where\" got invalid value {\"role\":\"FULL_MEMBER\",\"accountStatus\":\"ACTIVE\"}; Field \"role\" is not defined by type UserWhereInput.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "candidates"
      ]
    }
  ]
}
Should I be using directives to handle roles/permissions instead??
My approach doesn’t feel secure…
h
your
User
has a
roles
field, not
role
.
So change your resolver to
Copy code
where: {
  roles_in: args.role
}
c
@hinsxd unfortunately there is no
roles_in
because
roles
is an enum
h
oh sorry
it should be
role_in
with
Copy code
enum Role {
  Customer
  Author
}
I have
role_in
c
I’ll look again, I didn’t see it……
wasn’t sure if there enums weren’t allowed in collections
h
oh thats right
i tried enum collections but doesnt work too
c
😅 ok, I’m not crazy
h
perhaps you need to check it manually after query
c
I do…
Copy code
hasRole(currentUser, ['ADMIN', 'EXECUTIVE_COMMITTEE']);