Post entity class contains a many to one relationship with my Users entity:
@ManyToOne(() => Users, user => user.posts)
user: Users;
Users entity class contains a one to many relationship with my Post entity:
@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? I have attempted the following:
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[];