首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL错误:无法为不可为空的字段Mutation.deleteComment返回null

GraphQL错误:无法为不可为空的字段Mutation.deleteComment返回null
EN

Stack Overflow用户
提问于 2020-12-07 23:51:24
回答 1查看 260关注 0票数 0

我添加了一个变体来删除评论,每当我尝试删除评论时,我都会收到这个错误。

代码语言:javascript
复制
"errors": [
    {
      "message": "Cannot return null for non-nullable field Mutation.deleteComment.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "deleteComment"
      ],

解析器

代码语言:javascript
复制
  async deleteComment(_, { postId, commentId }, context) {
  const { username } = checkAuth(context);
  const post = await Post.findById(postId);

  if (post) {
    const commentIndex = Post.comments.findIndex((c) => c.id === commentId);

    if (post.comments[commentIndex].username === username) {
      post.comments.splice(commentIndex, 1);
      await post.save();
      return post;
    } throw new AuthenticationError('Action not allowed');
  } else {
    throw new UserInputError('Page not found');
  }
},

类型定义

代码语言:javascript
复制
deleteComment(postId: ID!, commentId: ID!): Post!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-08 01:50:19

我发现问题出在从apollo导入错误--服务器必须按字母顺序排列。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65184935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档