-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathsubscriptions.py
53 lines (40 loc) · 1.71 KB
/
subscriptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from graphene_django.settings import graphene_settings
from graphql import MiddlewareManager
from ..base_async import (BaseAsyncConnectionContext,
BaseAsyncSubscriptionServer)
from ..observable_aiter import setup_observable_extension
setup_observable_extension()
class ChannelsConnectionContext(BaseAsyncConnectionContext):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.socket_closed = False
async def send(self, data):
if self.closed:
return
await self.ws.send_json(data)
@property
def closed(self):
return self.socket_closed
async def close(self, code):
await self.ws.close(code=code)
async def receive(self, code):
"""
Unused, as the django consumer handles receiving messages and passes
them straight to ChannelsSubscriptionServer.on_message.
"""
class ChannelsSubscriptionServer(BaseAsyncSubscriptionServer):
async def handle(self, ws, request_context=None):
connection_context = ChannelsConnectionContext(ws, request_context)
await self.on_open(connection_context)
return connection_context
def get_graphql_params(self, connection_context, payload):
params = super().get_graphql_params(connection_context, payload)
middleware = graphene_settings.MIDDLEWARE
if middleware:
if not isinstance(middleware, MiddlewareManager):
middleware = MiddlewareManager(
*middleware, wrap_in_promise=False
)
params["middleware"] = middleware
return params
subscription_server = ChannelsSubscriptionServer(schema=graphene_settings.SCHEMA)