-
-
Notifications
You must be signed in to change notification settings - Fork 609
feat(subscriptions): support graphql-ws and all operations over websockets
#1338
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
Conversation
|
this looks awesome! following :) |
|
What's the advantage of using graphql-transport-ws instead of subscriptions-transport-ws ? |
|
Hey @asafyish, good question! In short - the On the other hand, |
|
Hey @benjie, As of [email protected] the lib is feature full and ready for the world! I've been using it in production for a whole week now - except for the occasional closure caused by network problems, I ran in no problems at all. Will make a PR for v5 later on. However, besides resolving conflicts, rebasing, dropping the version bumps and deciding on how to handle breaking changes; this PR is ready too! |
|
Awesome 🙌 We're definitely going to have to work to minimize breaking changes; but fortunately the server plugin API has this note at the very top:
So basically I'm happy to break the server plugin API if it's absolutely necessary, so long as that breakage is super obvious. So we need to make it so people calling the older hooks causes a panic and the system refuses to start. Do you want to have a go at doing this? |
|
(Before anything else, please rebase. I'm on the verge of releasing 4.9.0 so I don't think there's any more major changes coming in soon.) |
|
Thats great! We should be leaving the experimental phase soon 😁. From the looks of it, there are 2 breaking changes:
|
|
Ready 👍 |
|
The first of those:
This is a major breaking change. We can't do it on a semver minor update. Protocol-wise, what specifically has broken? Can we use both libraries and use a header/sniff the traffic to pipe it into one or the other implementation? Maybe we can change the I'd like to sort this out conceptually before I review the specific code. |
|
I like your idea of using the flag. I see it something like this: type SubscriptionsFlag = 'v0' | 'v1' | boolean;
// true = 'v0' 👈 to reduce breaking changes. For all those who had this flag set to true - nothing will change.I am thinking about Also, if you choose What do you say? |
|
This seems very reasonable. Could you attempt to integrate this in? Also I don't think we actually have tests for websocket connections right now, but with two separate websocket implementations in play we're going to need them - are you interested in taking this on or do you need me to attempt it? I'd like to see tests for the existing implementation merged separately (with this PR in mind) and then see how this PR changes those tests. |
|
Actually; can you add a |
|
I'll do the Regarding the tests, I'd be happy to contribute with test cases; but, I would be very grateful if you could lay down the ground work? Just some basic subscriptions test setup to help me understand more about PostGraphile's approach to testing and to allow the tests to fit with the rest of the codebase. My lib already fully supports all operations, so I can integrate that in this PR too. I also oversaw the discussion about auth in #1233 - so I'd just like to add my 2 cents with how I approach this in my production environments. Besides relying on CORS to protect against connections originating from unknown sources I authenticate the requester on every subscription request and kick him out if I sense something fishy (almost the same way you authenticate plain ol' HTTP requests). However, the |
|
Sounds good; hopefully I can take a look at this on Tuesday 👍 |
|
Rebased. |
|
Hey @benjie, I have addressed all raised concerns. Renamed the name and updated the PR description to reflect that. Would be awesome if you can check it out! |
|
👋 @benjie, me again. I guess you are very busy and I don't want to be annoying, I just wanted to ping and check about this PR. I am looking forward to using the official release instead of a packaged fork 😁 . |
|
Rebased. |
|
@enisdenjo I was just speaking to @benjie and was looking to see how to support changing JWT tokens on the fly for a WS connection... basically check it every time the onOperation is called in subscriptions.ts I would like to use this obviously to change roles when the query hits. Have you considered this at all? |
|
Hey @jwrascoe, The By "changing JWT tokens on the fly" you mean changing the actual token in the original ...
async onSubscribe(ctx, message, args) {
// ctx is the context of the connection and the connectionParams are from the initialisation message
ctx.connectionParams.authorization = yourBearerToken(request, message, args);
// request is the initial connection HTTP request from the client
ctx.request.headers.authorization = ctx.connectionParams.authorization;
...
}
...Out of curiousity, why would you change the clients JWT token to switch roles? |
|
@enisdenjo Interesting... let me see if I can figure out how to implement your suggestion. Without a token, I have the default access set to only allowing the request of a token... once the user provides credentials, I look it up and then decide which role they need to be so I can enforce security.. The reason for changing the token is not to change roles (only if they log in and out as someone else) but rather to secure the data a bit more. In the event that the JWT token was caught somehow in a packet trace, I only wanted it to be good for a few minutes. The way I wrote my client it looks at the TTL in the JWT and renews it (if its able to) for continued access. I could close and restart the WS connection, but if I had outstanding subscriptions I would have to re-issue them and wanted to just supply the replacement(s) token to keep the connection alive with the subscriptions intact. Maybe there is a better way? Or im just being paranoid? |
|
I'd say you should always be aware of the exact details a client provides (or has provided) you; so, personally I wouldn't change the token on the fly in the server ever... I would recommend relying on the library and the |
|
@enisdenjo ok, thanks for the info... I'll see if I can come up with something that fits my use case. |
|
After a brief discussion with @benjie over GraphQL Slack a decision was made to allow choosing the Furthermore, the GraphiQL was reverted to also support both libraries. |
benjie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really close now 👍
…ransport-ws # Conflicts: # package.json # postgraphiql/src/components/PostGraphiQL.js # src/postgraphile/pluginHook.ts # yarn.lock
benjie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome; looks good to me! Thanks for all your hard work on this 🙌
graphql-ws clients and all 3 operations through WebSocketsgraphql-ws and all operations over websockets
Story
Support
graphql-wsclients alongsidesubscriptions-transport-ws.A single breaking change of dropping the hook
postgraphile:ws:onOperationin favour of the newpostgraphile:ws:onSubscriptionis introduced by opting-in.Additionally, support all 3 GraphQL operations by setting the
websocketOperationsoption toall. Defaults tosubscriptionswhich restricts operations to subscriptions only.TODOs:
query&mutation