首页 > 解决方案 > Unknown arg `email` in where.email for type UserWhereUniqueInput. GraphQL | NodeJS

问题描述

I've been testing GraphQL for a few days now and I'm doing the authentication management (by following this tutorial). I'm currently blocked with the following error:

Unknown arg `email` in where.email for type UserWhereUniqueInput. Did you mean `id`?

When I make the following request:

mutation {
  login(email: "contact@floriankamps.fr", password: "Test123") {
    token
    user {
      email
      links {
        url
        description
      }
    }
  }
}

Here's my login resolver:

let login = async (parent, args, context, info) => {
    const user = await context.prisma.user.findUnique({ 
        where: {
            email: args.email 
        } 
    });
    if (!user)
        throw new Error('No such user found');

    const valid = await bcrypt.compare(args.password, user.password);
    if (!valid)
        throw new Error('Invalid password');
    
    const token = jwt.sign({ userId: user.id }, APP_SECRET);

    return {
        token,
        user
    }
}

And my GraphQL schema:

type Mutation {
    post(url: String!, description: String!): Link!
    updateLink(id: Int!, url: String, description: String): Link
    deleteLink(id: Int!): Link
    signup(email: String!, password: String!, name: String!): AuthPayload
    login(email: String!, password: String!): AuthPayload
}

I don't really see where it could get in the way...

标签: javascriptnode.jsgraphql

解决方案


Okay so after a long investigation, I've found the solution. The problem didn't come from GraphQL but from Prisma. To add an email field to the User model, we need to define it the @unique directive like this:

model User {
  id  Int @id @default(autoincrement())
  name  String
  email String @unique
  password  String
  links Link[]
}

Solution was initially posted here: https://github.com/howtographql/howtographql/issues/661#issuecomment-401372966


推荐阅读