【问题标题】:Am getting 'Variable '$data' expected value of type 'UserCreateInput!'' when attempting Prisma GraphQL 'createUser'尝试 Prisma GraphQL 'createUser' 时,我得到类型为 'UserCreateInput!' 的变量 '$data' 预期值
【发布时间】:2019-09-17 12:16:14
【问题描述】:

不久前,我使用 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",

我查看了“生成的/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
}

我确信我遗漏了一些非常明显的东西,但看了一段时间后我没有看到它——所以任何线索都会非常感激!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢-我按照您对以下内容的建议进行了更改,但仍然遇到相同的错误。 Mutation: { register: async (parent, {username, password}, ctx, info) => { const hashedPassword = await bcrypt.hash(password, 10) const user = await ctx.prisma.createUser({ data: { username, password: hashedPassword, } }) return user }, }, }
    • 当我将以下突变放入操场时:mutation { register(username:"foo",password: "barr") { id username }
    • 嗯,错误说:“原因:'name'预期的非空值,发现空。”但您只提供用户名和密码。您是否忘记传递名称值?
    【解决方案2】:

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

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

    而且效果很好!正如上面@Errorname 所建议的那样,我不需要将“数据”对象添加到 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
        },
      },
    

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 2018-11-11
      • 2019-08-14
      • 1970-01-01
      • 2018-01-04
      • 2020-06-01
      • 2022-01-22
      • 1970-01-01
      • 2020-11-27
      相关资源
      最近更新 更多