Skip to content

Commit 9d91564

Browse files
support the graphql-ws subprotocol
* The protocol for message exchange employed over a websocket connection is referred as the subprotocol. * We currently use the `graphql-ws` subprotocol for serving GraphQL subscriptions over websockets. * The JavaScript library which implements the `grahpql-ws` subprotocol is no longer maintained. * This PR adds support for the `graphql-transport-ws` subprotocol, allowing us to migrate our client away from this unsupported library. * This requires graphql-python/graphql-ws#65 and a new release of the Python graphql-ws library to work.
1 parent 7a6e1b9 commit 9d91564

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

cylc/uiserver/handlers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from graphene_tornado.tornado_graphql_handler import TornadoGraphQLHandler
2525
from graphql import get_default_backend
26-
from graphql_ws.constants import GRAPHQL_WS
26+
from graphql_ws.constants import GRAPHQL_WS, TRANSPORT_WS_PROTOCOL
2727
from jupyter_server.base.handlers import JupyterHandler
2828
from tornado import web, websocket
2929
from tornado.ioloop import IOLoop
@@ -36,9 +36,10 @@
3636
from cylc.uiserver.authorise import Authorization, AuthorizationMiddleware
3737
from cylc.uiserver.resolvers import Resolvers
3838
from cylc.uiserver.websockets import authenticated as websockets_authenticated
39-
from cylc.uiserver.websockets.tornado import TornadoSubscriptionServer
39+
4040
if TYPE_CHECKING:
4141
from graphql.execution import ExecutionResult
42+
from cylc.uiserver.websockets.tornado import TornadoSubscriptionServer
4243

4344

4445
ME = getpass.getuser()
@@ -367,11 +368,15 @@ class SubscriptionHandler(CylcAppHandler, websocket.WebSocketHandler):
367368
# No authorization decorators here, auth handled in AuthorizationMiddleware
368369
def initialize(self, sub_server, resolvers, sub_statuses=None):
369370
self.queue: Queue = Queue(100)
370-
self.subscription_server: TornadoSubscriptionServer = sub_server
371+
self.subscription_server: 'TornadoSubscriptionServer' = sub_server
371372
self.resolvers: Resolvers = resolvers
372373
self.sub_statuses: Dict = sub_statuses
373374

374375
def select_subprotocol(self, subprotocols):
376+
if TRANSPORT_WS_PROTOCOL in subprotocols:
377+
# use graphql-transport-ws out of preference
378+
return TRANSPORT_WS_PROTOCOL
379+
# fallback to graphql-ws if required
375380
return GRAPHQL_WS
376381

377382
@websockets_authenticated
@@ -415,5 +420,6 @@ def context(self):
415420
self.get_current_user()
416421
).get('name'),
417422
'ops_queue': {},
418-
'sub_statuses': self.sub_statuses
423+
'sub_statuses': self.sub_statuses,
424+
'subprotocols': [self.selected_subprotocol],
419425
}

0 commit comments

Comments
 (0)