基于Mongoose Post Schema的Apollo GraphQL typeDef编写及查询问题
Apollo GraphQL 对应typeDef定义
# 声明日期时间标量,适配Mongoose自动生成的timestamps字段 scalar DateTime type User { id: ID! name: String! email: String! profile_pic: String! # 密码为敏感字段,不对外暴露,不加入类型定义 } type Post { id: ID! author: User! description: String image: String! likes: [User!]! createdAt: DateTime! updatedAt: DateTime! } type Query { # 按帖子ID查询单条帖子 getPost(postId: ID!): Post }
- 注意:对应
getPost的Resolver实现中需要调用Mongoose的populate方法关联查询author和likes字段关联的用户数据,才能正常返回关联的用户信息。
特定帖子点赞用户查询语句
query GetPostLikes($postId: ID!) { getPost(postId: $postId) { likes { name profile_pic } } }
- 调用时传入对应帖子的ID作为
postId参数,即可直接获取该帖子所有点赞用户的姓名和头像字段。
内容的提问来源于stack exchange,提问作者Ravishankar Chatterjee
相关产品推荐
相关产品推荐

