I have two resolvers, each with a @ResolveField that is related to each other, following is the code-first approach I tried to implement:
// post.resolver.ts
@Resolver(() => Post)
export class PostResolver {
constructor(
private authorService: AuthorService
) {}
...
@ResolveField(() => Author)
author(@Parent() post: Post) {
return this.authorService.getAuthor(post.id);
}
}
// author.resolver.ts
@Resolver(() => Author)
export class AuthorResolver {
constructor(
private readonly postService: PostService
) {}
...
@ResolveField(() => [Post])
posts(@Parent() author: Author) {
console.log(author);
return this.postService.getPostByAuthor(author.username);
}
}
This implementation is causing my graphql query to have nested query problems like the example below:
query ShowAllPost {
showAllPost {
author {
username
posts {
title
author {
email
}
}
}
}
}
How to avoid this problem?