-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing cookies in subscription resolver functions #424
Comments
Update: I have found out that I can indeed access the cookies sent with the request if #394 is merged via |
@joealden I know your question is about websockets specifically, but I've been looking into adding cookie-parser middleware form my mutations and queries. Can you show how you're setting up that middleware? |
@captDaylight yeah sure, as I mentioned above, this is the repo that I am using it in: https://github.com/joealden/talq-api/ (it's written in TypeScript). https://github.com/joealden/talq-api/blob/92ef03bef75518e8e1cdb763f8890824af10e3a7/src/index.ts#L36: this is how I added the cookie-parser middleware. This means that I can now access the cookies sent with the request from the context arg in any query or mutation resolver function (if you named this arg context, through context.request.cookies). https://github.com/joealden/talq-api/blob/92ef03bef75518e8e1cdb763f8890824af10e3a7/src/resolvers/utils.ts#L11: here I have a https://github.com/joealden/talq-api/blob/92ef03bef75518e8e1cdb763f8890824af10e3a7/src/resolvers/Query.ts#L5: then in the resolver functions, I use one of those above helper functions if I need to check if the user is logged in. If the user is logged in, the helper function will return and the resolver function will continue to execute, if the user is not logged in, the helper function will throw an error, causing the resolver function to halt execution and error out. This code was inspired by @wesbos's (unreleased) Advanced React codebase over at https://github.com/wesbos/Advanced-React. I altered the code to fit my use case. The helper function style code is inspired by this example code created by the @prismagraphql team: https://github.com/prismagraphql/prisma/blob/master/examples/authentication/src/utils.js#L6. I just altered how I retrieved the token because I wanted to use cookies instead of an Authorization HTTP header. Thanks to Wes and the Prisma team for the great learning resources! |
@joealden really appreciate it! this is super helpful, I was very close but this ironed out the kinks. |
@joealden the problem is still there I think, right? I'm using express.js vanilla with Apollo Server 2 and I'm stucked here like you. I'm using cookies to store my session using https://github.com/expressjs/cookie-session. I can get cookies with Can you hint me? |
@frederikhors Sorry, I don't think I can really help you with that as your question is about sessions and Apollo server specifically, which I don't have that much experience with. In my case, if you are interested, I forked graphql-yoga (https://github.com/joealden/graphql-yoga) as the maintainers haven't responded to this issue and I needed the feature. I then merged #394 and it worked like a charm. If its any help, here is a github repo where I am handling subscription auth through cookies using my forked version of yoga (https://github.com/joealden/talq-api). You can see in the subscription resolvers and the utility functions I made for handling auth how I check for cookies correctly depending on if it is a subscription action. Unfortunately I am using graphql-yoga and not Apollo server, so you might have to change the code a bit. Hope that is of some help. Also, it would be great if a maintainer took some time to look over this issue. |
Due to inactivity of this issue we have marked it |
This issue should be kept open, I don't understand why stale bot closes issues 'if no further activity occurs'. This is is still a feature that I think would be a worthy addition to |
Due to inactivity of this issue we have marked it |
Do not close |
Due to inactivity of this issue we have marked it |
No. |
I solved this problem in the
Next, when you initialize the server, you can get the
An example can be found here https://github.com/bakhaa/pw/blob/master/api/app.js. |
Any news on this? I've tried looking at examples by @bakhaa but I can't seem to get it to work. Here's my code so far but I can't get the here are my server options:
here's my server init:
the problem I'm having is in the If anyone has any suggestions to get this to work, that would be greatly appreciated! |
Due to inactivity of this issue we have marked it |
No stale. |
In new GraphQL Yoga v2, you can access platform independent (root, args, context, info) => {
context.req.cookies // Express
context.request.headers.get('Cookie') // Non node env
context.req.headers.cookie // Any other Node env
} |
Hi, I'd like to quickly thank the contributors of this project for making graphql server creation so easy!
Here is my issue:
I am writing an API where an authentication cookie is sent along with the request. I am using the express cookie-parser middleware to parse the cookie and place it in request.cookies. This works fine for query and mutation resolver functions as I have access to the request object through the context argument of the resolver function. However, the express request object is not passed to subscription resolver functions (for a good reason I presume), meaning that I cannot access the cookie this way.
To my understanding, WebSocket requests are sent over HTTP, and from a little reading online, I think this means that WebSocket requests are able to access HTTP cookies.
If what I have said is correct, how can I access the cookies that are sent along with the request?
I have seen issue #393 with an accompanying PR #394. If this gets merged, could I access the cookies through the
webSocket
object passed into the context?If so, could we get this PR merged (if there is nothing blocking it)? If cookies are able to be used with WebSockets, I think that it would be a great feature to add.
Here is the github link to the project that I am working on for reference: https://github.com/joealden/talq-api/.
It might also be worth mentioning that I have seen this https://www.apollographql.com/docs/react/advanced/subscriptions.html#authentication where you can send connectionParams with the request. But the issue is that because I am using a cookie that I want to be httpOnly, I cannot access it's contents and send it as a connectionParam.
Please correct me if any of what I have said is wrong, and thanks for your time!
The text was updated successfully, but these errors were encountered: