graphql-compose / graphql-compose/graphql-compose-mongoose

How to use internal mechanisms for dynamic population/projection for custom resolver

Open
#321 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

Hello!

Let's suppose that we've got the below 2 resolvers:

```
{
userById: UserTC.mongooseResolvers.findById(),
userGetMe: UserTC.addResolver({
name:"userGetMe",
args: {},
type: User,
resolve: (_,__,context) => {
const {auth} = context;
const {myId} = auth;
return User.findById(myId);
},
}).getResolver("userGetMe").withMiddlewares([getMyAuthId])
}
```

As you can see both of the resolvers above eventually utilize `findById` of mongoose. The only difference is that `userGetMe` resolver does not take any arguments but returns the User with the id of the current authenticated user.

The solution above is ideal and not realistic. In reality I have to manually populate any nested fields in the `User.findById(myId)` call, based on the fields request inside the graphql query. So basically I need to write helper functions like what you've got [here](https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/projection.ts) in order to achieve my result.

My question is, is there a clever way to overcome this? Is there a way for the `userGetMe` resolver to eventually use the `UserTC.mongooseResolvers.findById()` resolver to fetch the User fields correctly populated, projected etc?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.