简体   繁体   English

如何获得创建帖子的用户的帖子列表?

[英]How can I get a list of posts with the user who created them?

Post entity class contains a many to one relationship with my Users entity:发布实体 class 包含与我的用户实体的多对一关系:

@ManyToOne(() => Users, user => user.posts)
user: Users;

Users entity class contains a one to many relationship with my Post entity:用户实体 class 包含与我的 Post 实体的一对多关系:

@OneToMany(() => Post, post => post.user)
posts: Post[];

I have a query which returns the list of posts with the user:我有一个查询,它返回用户的帖子列表:

@Query(() => [Post])
async getAllUserPosts(@Ctx() context: MyContext) {

try {
  const qb = getConnection()
    .getRepository(Post)
    .createQueryBuilder("p")
    .innerJoinAndSelect("p.user", "u", "u.id = p.userID")
    .where("p.userID = :id", { id: 7 })
    .orderBy("p.datePublished", "DESC")

    const posts = await qb.getMany();

    console.log(posts)

    return posts

  } catch (e) {
    console.log(e);
    return null;
  }
}

When I run the query it returns the following:当我运行查询时,它返回以下内容: 帖子列表和用户

How can I return this data in GraphQL?如何在 GraphQL 中返回此数据? I have attempted the following:我尝试了以下方法: GraphQL 查询

Well aren't I silly...好吧,我是不是很傻...

I forgot to add the type of field for each relationship:我忘了为每个关系添加字段类型:

@ManyToOne(() => Users, user => user.posts)
@Field(() => Users)
user: Users;

@OneToMany(() => Post, post => post.user)
@Field(() => [Post])
posts: Post[];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按最新创建的Feed列出帖子? - How can I list posts in a feed by the most recently created? 如何获取创建文档的用户的数据 - How to get the data of a user who created a document 如何在特定主题标签中获取 Instagram 用户创建的帖子? - How to get Instagram user-created posts within a particular hashtag? 我可以获取喜欢我的供稿的朋友列表吗? - Can I get the list of friends who like my feed 如何获取特定Soundcloud用户帖子的JSON列表? - How can I get a JSON listing of a particular Soundcloud user's posts? 我如何才能在Twitter上吸引用户的关注者,然后在一段时间内从这些关注者那里获取帖子,以便创建数据库? - How can I get the followers of a user on twitter, then get posts from those followers over a period of time so i can create a database? 如何正确查询用户创建的所有帖子? - How to properly query all posts created by a user? 如何获取具有其ID的用户列表(列表)? - How can I get a list of User with their ID (list)? 我如何获得用户键入的非英语语言中的特殊单词的单词,包括c#,javascript - How i can get the word who user type for a special word in non-english language in any language even c# , javascript 如何获得使用相同名称的ID的数量 - How can I get the count of ids who are using same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM