You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 15:39:03