使用Prisma生成的代码的可变错误

时间:2019-09-04 23:01:18

标签: javascript graphql prisma

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

但是,我有一个错误。

错误

错误:类型'UserCreateInput!'的变量'$ data'期望值!但得到:{“ data”:{“ name”:“ bryan”,“ email”:“ bryan@email.com”,“ password”:“ $ 2a $ 10 $ U33Z8mkK1hPgR96r6DOrNuxI6vaBSARUwFOgh4vYQq0VmMstgZNhG”}}。原因:'email'预期的非空值,发现为空。 (第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)
}

1 个答案:

答案 0 :(得分:0)

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

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

有关其他详细信息和示例,请参见the docs