graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Get user information of currently logged in user
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?
You can use removeArg + wrapResolve to prepare args like this https://github.com/graphql-compose/graphql-compose-mongoose/issues/321#issuecomment-816645685