尝试Prisma GraphQL'createUser'时,正在获得类型'UserCreateInput!'的'变量'$ data'期望值!

时间:2019-04-28 19:18:05

标签: graphql apollo apollo-server prisma prisma-graphql

不久前,我在GraphQL-Yoga和Prisma上做了两个站点,并尝试在Apollo Express上进行同样的尝试。一切都很好。但是现在我无法开始工作,也许我走得太快,一次尝试了太多事情。我开始尝试按照this指南实施注册突变。我的第一个(也是目前为止)突变是

const resolvers = {
  Mutation: {
    register: async (parent, { username, password }, ctx, info) => {
      const hashedPassword = await bcrypt.hash(password, 10)
      const user = await ctx.prisma.createUser({
        username,
        password: hashedPassword,
      })
      return user
    },
  },
}

但是当我在GraphQL Playground中对其进行测试

mutation {
 register(username:"foo",password: "bar") {
  id
  username
}
}

我收到此错误

"errors": [
    {
      "message": "Variable '$data' expected value of type 'UserCreateInput!' but got: {\"username\":\"sandor\",\"password\":\"$2a$10$S7IF1L3YjS4dLUm8QB2zjenPnKHwlohml7AaPf1iVhNipHSkNb/62\"}. Reason: 'name' Expected non-null value, found null. (line 1, column 11):\nmutation ($data: UserCreateInput!) {\n          ^",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "register"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",

我查看了“ generated / prisma-client / prisma-schema”,发现我有

  createUser(data: UserCreateInput!): User!

但是当我使用Prisma之前,从来没有抱怨过将对象传递给输入类型。我在哪里错了?

我的模式是

const { gql } = require('apollo-server-express');


const typeDefs = gql`

  type User {
    id: ID!
    username: String!
    password: String!
  }

  type Query {
    currentUser: User!
  }

  type Mutation {
    register(username: String!, password: String!): User!
    login(username: String!, password: String!): LoginResponse!
  }

  type LoginResponse {
    id: ID!
    token: String
    user: User
  }

`
module.exports = typeDefs

我的datamodel.prisma是

type User {
  id: ID! @id
  name: String!
    password: String!

}


type LoginResponse {
     id: ID! @id
    token: String
    user: User
}

我确定我会丢失一些显而易见的东西,但是经过一段时间我没看到它-因此任何线索将不胜感激!

2 个答案:

答案 0 :(得分:0)

Prisma客户端的createUser方法需要单个对象参数,然后包含Prisma所需的不同对象。

正确的方法调用方式如下:

const user = await ctx.prisma.createUser({
  data: {
    username,
    password: hashedPassword,
  }
})

这种构造参数的方式有助于确保与其他查询的一致性:

例如:

const user = await ctx.prisma.updateUser({
  where: {
    name: "John Doe"
  },
  data: {
    password: newPassword
  }
})

此外,错误显示:“原因:'名称'预期的非空值,发现为空。”但您只给出usernamepassword。您是否忘了传递name值?

答案 1 :(得分:0)

答案-@Errorname在评论中暗示并由Prisma Spectrum论坛上的用户提供给我(我自己没有看到),原因是Prisma数据模型与GraphQL之间存在差异模式。也就是说,该字段在模式中被称为“用户名”,在数据模型中被称为“名称”。我在数据模型中将其更改为“用户名”。 h!

type User {
  id: ID! @id
  username: String!
  password: String!
}

,效果很好!正如上面@Errorname所建议的那样,我不需要将'data'对象添加到createUser中-也就是说,这很好用

 Mutation: {
    register: async (parent, {username, password}, ctx, info) => {
      const hashedPassword = await bcrypt.hash(password, 10)
      const user = await ctx.prisma.createUser({

          username,
          password: hashedPassword,

      })
      return user
    },
  },
相关问题