【问题标题】:Partial Update Mutations(GraphQL)部分更新突变(GraphQL)
【发布时间】:2020-05-30 08:02:29
【问题描述】:

我怎样才能更新一个只有一个字段更改的节点,而保留其余字段?

我的用户类型

type User {
        id: ID!
        user_id: String!
        username: String!
        email: String!
        role: Role!
        isVerified: Boolean!
    }

我的输入类型

input UserUpdateInput {
    user_id: String
    username: String
    email: String
    password: String
    role: Role
    isVerified: Boolean
    }
input UserWhereUniqueInput {
    id: ID
    user_id: String
    email: String
    }

我的突变类型

type Mutation {
        updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput): User
    }

我的解析器

function updateUser(root, args, context, info){
    return context.db.mutation.updateUser({
      data: args.data,
      where: {
      id: args.where.id     
      }
    }, info)
  }

这是我在 GraphQL 操场上发送的请求

mutation{
    updateUser(
    data: {
      isVerified: true
    }
    where:{
    user_id :  "afc485b"
        }
    )
  {
    isVerified
  }
}

这是我遇到的错误

{
  "errors": [
    {
      "message": "Cannot read property 'mutation' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'mutation' of undefined"

谁来帮帮我。我错过了什么? 按照 Daniel Rearden 在答案部分的建议更新我的服务器后,我收到了一个新错误

    {
      "message": "Cannot read property 'updateUser' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'updateUser' of undefined"

【问题讨论】:

    标签: graphql prisma prisma-graphql graphql-mutation


    【解决方案1】:

    该错误是未将db 属性正确添加到您的上下文的结果。假设您仍在使用版本 1,您的代码应如下所示:

    const { prisma } = require('./generated/prisma-client')
    
    const server = new ApolloServer({
      ...
      context: {
        db: prisma,
      },
    })
    

    【讨论】:

    • 我已经按照您的建议更新了我的服务器后出现新错误的问题
    • 不应该只是context.db.updateUser,如docs所示吗?你从哪里得到mutation
    【解决方案2】:

    我注意到的第一件事是您的 GQL 查询不正确。

    你的:

    mutation{
        updateUser(
        data: {
          isVerified: true
        }
        where:{
        user_id :  "afc485b"
            }
        )
      {
        isVerified
      }
    }
    
    1. 在“突变”一词之后,您为调用设置了一个名称,即 “UpdateUser”,但实际上可以是任何东西。对于每个部分

    2. 您需要将检查值设为对象的where子句,即 where: { myProperty: {eq: "some value"}}

    所以你的查询应该更像这样:

    mutation UpdateUser {
        updateUser(
          data: {isVerified: true}
          where:{user_id : {eq: "afc485b"}}
        )
      {
        isVerified
      }
    }
    

    希望对您有所帮助...我没有完全阅读其余部分,但认为这将有助于解决您遇到的初始错误。

    【讨论】:

      猜你喜欢
      • 2019-05-22
      • 2019-08-29
      • 2020-11-12
      • 2016-03-08
      • 2019-03-10
      • 2021-12-02
      • 2020-05-30
      • 2020-12-22
      • 2020-10-20
      相关资源
      最近更新 更多