I added a mutation to delete comments and whenever I try to delete a comment I get this error.
JavaScript
x
13
13
1
"errors": [
2
{
3
"message": "Cannot return null for non-nullable field Mutation.deleteComment.",
4
"locations": [
5
{
6
"line": 2,
7
"column": 3
8
}
9
],
10
"path": [
11
"deleteComment"
12
],
13
Resolver
JavaScript
1
17
17
1
async deleteComment(_, { postId, commentId }, context) {
2
const { username } = checkAuth(context);
3
const post = await Post.findById(postId);
4
5
if (post) {
6
const commentIndex = Post.comments.findIndex((c) => c.id === commentId);
7
8
if (post.comments[commentIndex].username === username) {
9
post.comments.splice(commentIndex, 1);
10
await post.save();
11
return post;
12
} throw new AuthenticationError('Action not allowed');
13
} else {
14
throw new UserInputError('Page not found');
15
}
16
},
17
Type definition
JavaScript
1
2
1
deleteComment(postId: ID!, commentId: ID!): Post!
2
Advertisement
Answer
I found out that the issue was in importing errors from apollo-servers had to be in an alphabetical order.