Skip to content
Advertisement

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!

Advertisement

Answer

I found out that the issue was in importing errors from apollo-servers had to be in an alphabetical order.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement