【问题标题】:Variable error using Prisma generated code使用 Prisma 生成代码的变量错误
【发布时间】:2020-01-07 19:51:30
【问题描述】:

我正在使用由 Prisma 生成的 createUser,它需要需要 UserCreateInput 的数据。

但是,我遇到了一个错误。

错误

错误:“UserCreateInput!”类型的变量“$data”预期值但是得到了:{"data":{"name":"bryan","email":"bryan@email.com","password":"$2a$10$U33Z8mkK1hPgR96r6DOrNuxI6vaBSARUwFOgh4vYQq0VmMstgZNhG"}}。原因:“电子邮件”预期的非空值,发现为空。 (第 1 行,第 11 列): 突变 ($data: UserCreateInput!) {

schema.graphql

type Query {
  users: [User!]!
}

type Mutation {
  signup(email: String!, password: String!, name: String!): User!

type User {
  id: ID!
  email: String!
  name: String!
  clients: [Client]
  products: [Product]
}

变异

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')

const Mutation = {
  async signup(root, args, context) {
    const email = args.email.toLowerCase()
    const password = await bcrypt.hash(args.password, 10)
    const user = await context.prisma.createUser({
      data: {
        name: args.name,
        email,
        password
      }
    })
    const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET)
    context.response.cookie('token', token, {
      httpOnly: true,
      maxAge: 1000 * 60 * 60 * 24 * 356
    })
    return user
  }
}

module.exports = Mutation

用户的datamodel.prisma

type User {
  id: ID! @id
  email: String! @unique
  name: String!
  password: String!
  clients: [Client] @relation(link:INLINE)
  products: [Product] @relation(link:INLINE)
  transactions: [Transaction] @relation(link:INLINE)
}

【问题讨论】:

    标签: javascript graphql prisma


    【解决方案1】:

    不需要在传递给createUser 的对象中包含data 属性——只需将参数作为映射传递:

    const user = await context.prisma.createUser({
      name: args.name,
      email,
      password,
    })
    

    有关更多详细信息和示例,请参阅the docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2021-07-06
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      相关资源
      最近更新 更多