@@ -66,6 +66,8 @@ First, let's create some Strawberry-types for the chat.
66
66
``` python
67
67
# mysite/gqlchat/subscription.py
68
68
69
+ import strawberry
70
+
69
71
70
72
@strawberry.input
71
73
class ChatRoom :
@@ -105,6 +107,13 @@ Now we will create the chat [subscription](../general/subscriptions.md).
105
107
``` python
106
108
# mysite/gqlchat/subscription.py
107
109
110
+ import os
111
+ import threading
112
+
113
+ from typing import AsyncGenerator, List
114
+
115
+ from strawberry.types import Info
116
+
108
117
109
118
@strawberry.type
110
119
class Subscription :
@@ -184,7 +193,10 @@ have to create a mutation for sending messages via the `channel_layer`
184
193
``` python
185
194
# mysite/gqlchat/subscription.py
186
195
196
+ from channels.layers import get_channel_layer
187
197
198
+
199
+ @strawberry.type
188
200
class Mutation :
189
201
@strawberry.mutation
190
202
async def send_chat_message (
@@ -193,8 +205,7 @@ class Mutation:
193
205
room : ChatRoom,
194
206
message : str ,
195
207
) -> None :
196
- ws = info.context[" ws" ]
197
- channel_layer = ws.channel_layer
208
+ channel_layer = get_channel_layer()
198
209
199
210
await channel_layer.group_send(
200
211
f " chat_ { room.room_name} " ,
@@ -248,6 +259,7 @@ An example of this (continuing from channels tutorial) would be:
248
259
249
260
``` python
250
261
import os
262
+
251
263
from channels.auth import AuthMiddlewareStack
252
264
from channels.routing import ProtocolTypeRouter, URLRouter
253
265
from django.core.asgi import get_asgi_application
@@ -264,13 +276,14 @@ django_asgi_app = get_asgi_application()
264
276
from chat import routing
265
277
from mysite.graphql import schema
266
278
267
- websocket_urlpatterns = routing.websocket_urlpatterns + [
268
- re_path(r " graphql" , GraphQLWSConsumer.as_asgi(schema = schema)),
269
- ]
270
-
271
279
272
280
gql_http_consumer = AuthMiddlewareStack(GraphQLHTTPConsumer.as_asgi(schema = schema))
273
281
gql_ws_consumer = GraphQLWSConsumer.as_asgi(schema = schema)
282
+
283
+ websocket_urlpatterns = routing.websocket_urlpatterns + [
284
+ re_path(r " graphql" , gql_ws_consumer),
285
+ ]
286
+
274
287
application = ProtocolTypeRouter(
275
288
{
276
289
" http" : URLRouter(
0 commit comments