graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Get user information of currently logged in user #333

Open riggedCoinflip opened 3 years ago

riggedCoinflip commented 3 years ago

I want to have a query that displays me all the information of the currently logged in user.

I use JWT for log in that stores the id in context and can get the id this way. If I would use the .mongooseResolvers.findById() query, the id would be an argument - which does not work as id is in context.

I came up with this solution (and it works!)

UserTCPublic.addResolver({
    kind: 'query',
    name: 'userSelf',
    description: "execute user query on currently logged in user",
    type: UserTCPublic.mongooseResolvers.findById().getType(),
    resolve: async ({context}) => {
        return User.findById(context.req.user._id);
    }
})

but I feel like there is a better, more scalable version - if I want to implement userUpdateByIdSelf and userDeleteByIdSelf I would basically have the same code 3 times over.

Do you have any recommendations for this?

yurtaev commented 3 years ago

You can use removeArg + wrapResolve to prepare args like this https://github.com/graphql-compose/graphql-compose-mongoose/issues/321#issuecomment-816645685