简体   繁体   English

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

[英]GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment

I added a mutation to delete comments and whenever I try to delete a comment I get this error.我添加了一个突变来删除评论,每当我尝试删除评论时,我都会收到此错误。

"errors": [
    {
      "message": "Cannot return null for non-nullable field Mutation.deleteComment.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "deleteComment"
      ],

Resolver解析器

  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');
  }
},

Type definition类型定义

deleteComment(postId: ID!, commentId: ID!): Post!

I found out that the issue was in importing errors from apollo-servers had to be in an alphabetical order.我发现问题在于从阿波罗服务器导入错误必须按字母顺序排列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 graphql 返回“不能返回 null of non-nullable field mutation.createAccnt” - why is graphql returning “cannot return null of non-nullable field mutation.createAccnt” 在 graphql 中更新/编辑用户数据(不能为不可空字段 Mutation.updateUser 返回 null) - Updating/editing user data in graphql(Cannot return null for non-nullable field Mutation.updateUser) 为什么在执行更改时会收到“无法为不可为空的字段返回 null”错误? - Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation? 无法为 GraphQL 查询中的不可空字段返回 null - Cannot return null for non-nullable field in a GraphQL query 在添加新架构字段后,aws cdk nextjs graphql 突变无法为不可空类型返回 null - aws cdk nextjs graphql mutation cannot return null for non-nullable type after adding in new schema field GraphQL 查询返回错误“不能为不可为空的字段返回空值” - GraphQL query returns error "Cannot return null for non-nullable field" graphql 解析器返回 无法从 nodejs 中的异步 function 为非空字段返回 null - graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs Prisma API返回关系但客户端返回“不能为非可空字段返回null”。 - Prisma API returns relation but client returns “cannot return null for non-nullable field..” 为什么这会抛出“不能为不可为空的字段 Query.hello 返回 null。”? - Why does this throw "Cannot return null for non-nullable field Query.hello."? 让TS抱怨与非空字段的空比较? - Getting TS to complain about null comparison to a non-nullable field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM