mongoose - 用于解析评论数组的字段解析器
问题描述
我有一个包含评论数组的帖子模型。我正在尝试使用字段解析器返回可以在我的 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;
}
},
},
解决方案
以下方法可以工作。如果有人有更好的建议,欢迎分享。希望这个答案能够帮助某人。谢谢
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;
},
},
推荐阅读
- linux - 如何设置 RPi 以自动 ssh 端口转发到通过以太网连接到 RPi 的设备?
- r - 根据列值从 data.frame 中随机选择行
- python - 如何在python3中计算天或月的时差
- c++ - 如何在同一父级中为多静态库编写 cmakelist
- python - AttributeError:“NoneType”对象没有属性“remove_roles”
- python - 过滤与列表中的元素匹配的熊猫数据框列表列
- api - 如何将 WEB API 链接到本地文件,以便在执行 POST 方法时,它将存储在我的本地文件中,而 GET 方法 API 将从本地文件中读取?
- javascript - setTimeout(() => ele.click()) 和 ele.click() 会导致不同的动画!那么,这与浏览器渲染或事件循环有关吗?
- apache - 如何在 Bitnami (Apache) 中访问 .htaccess?
- python - 将 2 个数据帧中的值与 IF 条件输出(1、0、-1)进行比较