0

I am trying to find the results that match a certain enum, this is my type :

type User {
  id: ID! @id
  fname:String!
  lname:String!
  email:String! @unique
  password: String
  school_ID: String #@relation(Link: INLINE)
  role: [Role] @scalarList(strategy: RELATION)
  resetToken: String
  resetTokenExpiry: String
}

I can search for email because it has the @unique key to it, but Role is an enum and I can't add the @unique key to it and that makes sense, because the results are not going to be unique.

However I keep getting this message:

"Variable \"$_v0_where\" got invalid value {\"role\":\"TEACHER\"}; Field \"role\" is not defined by type UserWhereUniqueInput."

Using email gives me the result I want if I want to use email to find the items.

What I am after is to get an array return with all the objects matching the selected enum result.

In my schema I have this:

users: [User]!
user(where: UserWhereInput!): User
findUser(role: Role, id: ID, email: String): User

In my query resolvers I have the following that I am playing with:

  users: forwardTo('db'),
  user: forwardTo('db'),

  async findUser(parent, args, ctx, info) {
    return ctx.db.query.user({
      where: {email: args.email, id: args.id, role: args.role}
    }, info)
  },

I am using prisma (1.17.1) to generate my schemas

How do I alter my code, or the findUser function to give me the desired result?

jwknz
  • 6,598
  • 16
  • 72
  • 115
  • This question appears to be specific to Prisma, not GraphQL or Apollo. You should update your tags accordingly and indicate what version of Prisma you're running. – Daniel Rearden Feb 09 '20 at 03:26
  • @DanielRearden done :-) - I still think it is related to GraphQL though since i am sorting out my schema – jwknz Feb 09 '20 at 03:32

1 Answers1

0

You are using the ctx.db.query.user function with the following signature:

user: (where: UserWhereUniqueInput) => UserNullablePromise;

As you can see it requires the UserWhereUniqueInput type where condition. Why? Because the user function should always only return one distinct unique object. On the other hand there is also a ctx.db.query.users function:

  users: (args?: {
    where?: UserWhereInput;
    orderBy?: UserOrderByInput;
    skip?: Int;
    after?: String;
    before?: String;
    first?: Int;
    last?: Int;
  }) => FragmentableArray<User>;

As you can see this function has more options and uses UserWhereInput to define the where condition. This will also include the role of the user in your case. This function can return multiple objects and it is up to you to filter / handle accordingly.

Hint: You can also inspect the generated prisma client yourself. The path will usually be something like src\generated\prisma-client\index.ts. In there you can see the interface definitions of all functions, the UserWhereUniqueInput as well as the UserWhereInput for example. This way you can check what is possible and what is not.

realAlexBarge
  • 1,808
  • 12
  • 19