Nested Resolvers
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[x] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
No possibility to create nested resolvers (or no info in the docs).
Expected behavior
Possibility to make nested resolvers (and/or appropriate info in the docs).
Minimal reproduction of the problem with instructions
In the docs we have example how to make Posts
property resolver on Author
Resolver. But we don’t have any info, how to make nested resolver on those posts. For example - we’d like to lazy fetch (only when someone requests the field) posts statistics. Seems like this isn’t possible at the moment?
@Resolver(of => Author)
export class AuthorResolver {
constructor(
private readonly authorsService: AuthorsService,
private readonly postsService: PostsService,
private readonly statsService: StatsService, // hypothetical, low-performing service
) {}
@Query(returns => Author)
async author(@Args({ name: 'id', type: () => Int }) id: number) {
return await this.authorsService.findOneById(id);
}
@ResolveProperty()
async posts(@Parent() author) {
const { id } = author;
return await this.postsService.findAll({ authorId: id });
}
// This does NOT work. Nor is mentioned anywhere in the docs.
@ResolveProperty()
async stats(@Parent() post) {
const { id } = post;
return await this.statsService.getPostStatistics({ postId: id });
}
}
So, is this possible in any way? If not, would a feature request like this be acceptable?
What is the motivation / use case for changing the behavior?
One of the biggest advantages of graphQL is lazy fetching nested fields (graph-like connections). Current implementation of graphQL decorators probably doesn’t allow creating nested resolvers.
Edit: Found similar question here: #162, but no luck with an answer yet. 😦
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8
You would end up with something like this.
First define the classes. Notice that there are no decorators on the object references. We instead add the mappings to that object in the Resolver.
In the first resolver, add the initial query for a search by id. We also add the getLevel2Property function, which tells graphql how to connect the two objects.
In the level 2 resolver, we need to add the mapping to Level3. Once again, the code inside getLevel3Property can be whatever you want.
Let me know if that makes sense. Thanks
@krislefeber I just want to make possible query like this one:
It is perfectly fine, to have it in separate resolver classes… but is it possible to nest it like this?