我得到了';变量'$数据';类型'的预期值;UserCreateInput'';尝试Prisma GraphQL';createUser';

我得到了';变量'$数据';类型'的预期值;UserCreateInput'';尝试Prisma GraphQL';createUser';,graphql,apollo,apollo-server,prisma,prisma-graphql,Graphql,Apollo,Apollo Server,Prisma,Prisma Graphql,不久前,我在GraphQL瑜伽和Prisma上做了几个网站,我正在阿波罗快车上做同样的尝试。一切都很好。然而,现在我无法开始这件事——也许我走得太快了,同时尝试了太多的事情。我开始尝试执行以下指南。我的第一个(也是目前唯一的)突变是 const resolvers = { Mutation: { register: async (parent, { username, password }, ctx, info) => { const hashedPassword

不久前,我在GraphQL瑜伽和Prisma上做了几个网站,我正在阿波罗快车上做同样的尝试。一切都很好。然而,现在我无法开始这件事——也许我走得太快了,同时尝试了太多的事情。我开始尝试执行以下指南。我的第一个(也是目前唯一的)突变是

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操场上测试时

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
}

我确信我漏掉了一些显而易见的东西,但看了一段时间后我没有看到——所以任何线索都将不胜感激

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

调用该方法的正确方法如下所示:

const user = await ctx.prisma.createUser({
  data: {
    username,
    password: hashedPassword,
  }
})
这种构造参数的方法有助于确保与其他查询的一致性:

例如:


此外,错误显示:“原因:'name'期望非空值,found null.”但您只给出
用户名
密码
。你忘了传递
名称
值了吗?

答案是,@Errorname在评论中暗示了这一点,一位用户在Prisma Spectrum论坛上给了我(我自己没有看到),Prisma数据模型和GraphQL模式之间存在差异。也就是说,该字段在模式中被称为“username”,在数据模型中被称为“name”。我在数据模型中将其更改为“username”。啊

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

谢谢--我按照你的建议对它进行了如下更改,但仍然得到相同的错误<代码>变异:{register:async(父级,{username,password},ctx,info)=>{const hashedPassword=wait bcrypt.hash(password,10)const user=wait ctx.prisma.createUser({data:{username,password:hashedPassword,}})返回user},},}当我在操场上放置以下变异:
变异{register(用户名:“foo”,密码:“barr”){id username}
Hmm,错误是:“Reason:'name'期望非空值,find null。”但你只给出用户名和密码。你忘记传递name值了吗?
 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
    },
  },