GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment
GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment
我添加了一个删除评论的修改,每当我尝试删除评论时,我都会收到此错误。
"errors": [
{
"message": "Cannot return null for non-nullable field Mutation.deleteComment.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"deleteComment"
],
解析器
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');
}
},
类型定义
deleteComment(postId: ID!, commentId: ID!): Post!
我发现问题出在从 apollo 服务器导入错误必须按字母顺序排列。
我添加了一个删除评论的修改,每当我尝试删除评论时,我都会收到此错误。
"errors": [
{
"message": "Cannot return null for non-nullable field Mutation.deleteComment.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"deleteComment"
],
解析器
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');
}
},
类型定义
deleteComment(postId: ID!, commentId: ID!): Post!
我发现问题出在从 apollo 服务器导入错误必须按字母顺序排列。