graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Get user information of currently logged in user

Open riggedCoinflip opened this issue 3 years ago • 1 comments

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?

riggedCoinflip avatar May 13 '21 22:05 riggedCoinflip

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

yurtaev avatar May 14 '21 02:05 yurtaev