1
votes

I'm using createUser generated by Prisma which requires data that expect UserCreateInput.

However, i'm having an error.

the error

Error: Variable '$data' expected value of type 'UserCreateInput!' but got: {"data":{"name":"bryan","email":"[email protected]","password":"$2a$10$U33Z8mkK1hPgR96r6DOrNuxI6vaBSARUwFOgh4vYQq0VmMstgZNhG"}}. Reason: 'email' Expected non-null value, found null. (line 1, column 11): mutation ($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]
}

Mutation

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 for user

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

1 Answers

1
votes

There's no need to include a data property in the object passed to createUser -- just pass in the parameters as a map:

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

See the docs for additional details and examples.