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

问题描述 投票:1回答:2

我不久前在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
}

我确信我错过了一些令人眼花缭乱的明显的东西,但看了一段时间后我没有看到它 - 所以任何线索都会非常感激!

graphql apollo apollo-server prisma prisma-graphql
2个回答
0
投票

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

调用方法的正确方法是这样的:

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

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

Ef。:

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

此外,错误说:“原因:'名称'预期非空值,找到null。”但你只给usernamepassword。你忘了传递name价值吗?


0
投票

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

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

它工作正常!我不需要将'data'对象添加到createUser,如上面的@Errorname所示 - 也就是说,这个工作正常

 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
    },
  },
© www.soinside.com 2019 - 2024. All rights reserved.