How do I perform a mutation that uses logged in us...
# orm-help
p
How do I perform a mutation that uses logged in user as a variable? :s Googling, and wondering
m
Are you using Yoga/Apollo on the backend?
If so, and putting aside the 'how am I retrieving this data in the first place' question for a second, you can use a bit of middleware to add the user information to the request object and then reference it from context inside your mutation resolver.
The approach I am using currently is thus:
(1) Send JWT to GQL Server from client; (2) Decode/Verify/Etc. JWT using a bit of Express Middleware; (3) Assuming that I have a valid JWT (one claim of which is the user's ID), I append the ID to the request object at 'req.user' and call next() (4) Then, when instantiating my GraphQL server I add the following to the context property: { ..., user: req.user ? req.user : Promise.resolve(null), ... } (5) Then, I can reference 'context.user' anywhere in my resolvers.
p
Thanks, nice tips to have 🙂
👍 1