我有一个包含注释数组的Post模型。我正在尝试使用field-resolver返回可以在我的graphql游乐场中查询的注释列表。我能够成功解决帖子-用户,但是我无法解决帖子-评论。有人可以帮我吗?谢谢
Query: {
//todo Using GraphQL Field Resolvers
getPosts: combineResolvers(isAuthenticated, async (_, __, { Post }) => {
const posts = await Post.find({});
return posts;
}),
},
Post: {
comments: async (parent, input, { Comment }) => {
console.log("parent", parent, input);
},
user: async (parent, __, { User }) => {
try {
const user = await User.findById(parent.user);
return user;
} catch (error) {
console.log(error);
throw error;
}
},
},
答案 0 :(得分:0)
以下方法可以起作用。如果有人有任何改善建议,欢迎分享。希望这个答案能够对某人有所帮助。谢谢
Post: {
comments: async (parent, __, { Post, Comment }) => {
console.log("parent", parent);
return parent.comments.map(async (c) => {
return Comment.findById(c);
});
},
user: async (parent, __, { User }) => {
try {
const user = await User.findById(parent.user);
return user;
} catch (error) {
console.log(error);
throw error;
}
},
},
Comment: {
user: async (parent, __, { User }) => {
console.log("parent ", parent);
const user = await User.findById(parent.user);
return user;
},
},