【问题标题】:Need help to debug this updateLink resolver需要帮助来调试此 updateLink 解析器
【发布时间】:2019-07-24 12:51:28
【问题描述】:

我需要为我的 graphQL 服务器实现一个 updateLink,但它一直抛出“id is not defined”错误。服务器还没有数据库,只有一个用于练习目的的数组。

我已经定义了用于更新的突变的架构,还编写了解析器,但它没有工作。我查看了 apollo 文档,看来我的解析器和架构是正确的。

以下是解析器

 updateLink:(parent,args)=>{
            const link = {  
                id:args.id,              
                description:args.description,
                url:args.url,
            }
            links[Number(id)]=link
            return link
        }
    },

以下架构


type Query {
  info: String!
  feed: [Link!]!
  link(id:ID!):Link
}

type Mutation {
  post(url: String!, description: String!): Link!
  updateLink(id:ID!,url:String!, description: String!): Link
  delete(id:ID!):Link
}

type Link {
  id: ID!
  description: String!
  url: String!
}

预期的结果应该是更新链接数组的服务器,但我得到了以下错误。

{
  "data": {
    "updateLink": null
  },
  "errors": [
    {
      "message": "id is not defined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateLink"
      ]
    }
  ]
}

【问题讨论】:

    标签: graphql-js


    【解决方案1】:

    您在这里引用了一个名为 id 的变量:

    links[Number(id)]=link
    

    但您尚未定义具有该名称的变量,如错误所示。我建议在你的项目中添加一个 linter 来帮助你捕获这样的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多