Skip to content

Commit d365c72

Browse files
committed
Split the channels 2 app into different modules
1 parent 86762d3 commit d365c72

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

graphql_ws/django/consumers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from channels.generic.websocket import AsyncJsonWebsocketConsumer
2+
from ..constants import WS_PROTOCOL
3+
from .subscriptions import subscription_server
4+
5+
6+
class GraphQLSubscriptionConsumer(AsyncJsonWebsocketConsumer):
7+
async def connect(self):
8+
self.connection_context = None
9+
if WS_PROTOCOL in self.scope["subprotocols"]:
10+
self.connection_context = await subscription_server.handle(self, self.scope)
11+
await self.accept(subprotocol=WS_PROTOCOL)
12+
else:
13+
await self.close()
14+
15+
async def disconnect(self, code):
16+
if self.connection_context:
17+
await subscription_server.on_close(self.connection_context)
18+
19+
async def receive_json(self, content):
20+
await subscription_server.on_message(self.connection_context, content)

graphql_ws/django/routing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from channels.routing import ProtocolTypeRouter, URLRouter
2+
from django.apps import apps
3+
from django.urls import path
4+
from .consumers import GraphQLSubscriptionConsumer
5+
6+
if apps.is_installed("django.contrib.auth"):
7+
from channels.auth import AuthMiddlewareStack
8+
else:
9+
AuthMiddlewareStack = None
10+
11+
12+
websocket_urlpatterns = [path("subscriptions", GraphQLSubscriptionConsumer)]
13+
14+
application = ProtocolTypeRouter({"websocket": URLRouter(websocket_urlpatterns)})
15+
16+
if AuthMiddlewareStack:
17+
auth_application = ProtocolTypeRouter(
18+
{"websocket": AuthMiddlewareStack(URLRouter(websocket_urlpatterns))}
19+
)

graphql_ws/django/graphql_channels.py renamed to graphql_ws/django/subscriptions.py

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
from asgiref.sync import async_to_sync
2-
from channels.generic.websocket import AsyncJsonWebsocketConsumer
32
from graphene_django.settings import graphene_settings
43
from graphql.execution.executors.asyncio import AsyncioExecutor
5-
from django.urls import path
64
from rx import Observer, Observable
75
from ..base import BaseConnectionContext, BaseSubscriptionServer
8-
from ..constants import GQL_CONNECTION_ACK, GQL_CONNECTION_ERROR, WS_PROTOCOL
6+
from ..constants import GQL_CONNECTION_ACK, GQL_CONNECTION_ERROR
7+
8+
9+
class SubscriptionObserver(Observer):
10+
def __init__(
11+
self, connection_context, op_id, send_execution_result, send_error, on_close
12+
):
13+
self.connection_context = connection_context
14+
self.op_id = op_id
15+
self.send_execution_result = send_execution_result
16+
self.send_error = send_error
17+
self.on_close = on_close
18+
19+
def on_next(self, value):
20+
self.send_execution_result(self.connection_context, self.op_id, value)
21+
22+
def on_completed(self):
23+
self.on_close(self.connection_context)
24+
25+
def on_error(self, error):
26+
self.send_error(self.connection_context, self.op_id, error)
927

1028

1129
class ChannelsConnectionContext(BaseConnectionContext):
@@ -87,43 +105,3 @@ async def on_stop(self, connection_context, op_id):
87105

88106

89107
subscription_server = ChannelsSubscriptionServer(schema=graphene_settings.SCHEMA)
90-
91-
92-
class GraphQLSubscriptionConsumer(AsyncJsonWebsocketConsumer):
93-
async def connect(self):
94-
self.connection_context = None
95-
if WS_PROTOCOL in self.scope["subprotocols"]:
96-
self.connection_context = await subscription_server.handle(self, self.scope)
97-
await self.accept(subprotocol=WS_PROTOCOL)
98-
else:
99-
await self.close()
100-
101-
async def disconnect(self, code):
102-
if self.connection_context:
103-
await subscription_server.on_close(self.connection_context)
104-
105-
async def receive_json(self, content):
106-
await subscription_server.on_message(self.connection_context, content)
107-
108-
109-
class SubscriptionObserver(Observer):
110-
def __init__(
111-
self, connection_context, op_id, send_execution_result, send_error, on_close
112-
):
113-
self.connection_context = connection_context
114-
self.op_id = op_id
115-
self.send_execution_result = send_execution_result
116-
self.send_error = send_error
117-
self.on_close = on_close
118-
119-
def on_next(self, value):
120-
self.send_execution_result(self.connection_context, self.op_id, value)
121-
122-
def on_completed(self):
123-
self.on_close(self.connection_context)
124-
125-
def on_error(self, error):
126-
self.send_error(self.connection_context, self.op_id, error)
127-
128-
129-
websocket_urlpatterns = [path("subscriptions", GraphQLSubscriptionConsumer)]

0 commit comments

Comments
 (0)