【问题标题】:Unknown arg `email` in where.email for type UserWhereUniqueInput. GraphQL | NodeJS未知 arg `email` 在 where.email 中,用于 UserWhereUniqueInput 类型。 GraphQL |节点JS
【发布时间】:2021-06-02 12:38:34
【问题描述】:

我已经测试 GraphQL 几天了,我正在做身份验证管理(通过关注this tutorial)。我目前因以下错误而被阻止:

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

当我提出以下要求时:

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

这是我的登录解析器:

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
    }
}

还有我的 GraphQL 架构:

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
}

我真的不知道它会妨碍到哪里......

【问题讨论】:

  • UserWhereUniqueInput ?
  • 我不知道这是什么

标签: javascript node.js graphql


【解决方案1】:

好的,经过长时间的调查,我找到了解决方案。问题不是来自 GraphQL,而是来自 Prisma。 要将电子邮件字段添加到 User 模型,我们需要像这样定义 @unique 指令:

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

解决方案最初发布在这里: https://github.com/howtographql/howtographql/issues/661#issuecomment-401372966

【讨论】:

  • 谢谢。如果您创建了更多用户,您还需要进行另一次 prisma 迁移:npx prisma migrate dev --name "emailunique" 或完全删除您的 prisma DB(从 prisma 文件夹中删除所有文件,schema.prisma 除外)。
猜你喜欢
  • 2020-02-06
  • 2022-07-06
  • 2021-07-16
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2020-12-13
  • 2019-01-15
相关资源
最近更新 更多