Skip to content

Commit c0d00aa

Browse files
authored
fix(docs): Examples are now working properly. (#3263)
1 parent a90dea2 commit c0d00aa

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

docs/integrations/channels.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ First, let's create some Strawberry-types for the chat.
6666
```python
6767
# mysite/gqlchat/subscription.py
6868

69+
import strawberry
70+
6971

7072
@strawberry.input
7173
class ChatRoom:
@@ -105,6 +107,13 @@ Now we will create the chat [subscription](../general/subscriptions.md).
105107
```python
106108
# mysite/gqlchat/subscription.py
107109

110+
import os
111+
import threading
112+
113+
from typing import AsyncGenerator, List
114+
115+
from strawberry.types import Info
116+
108117

109118
@strawberry.type
110119
class Subscription:
@@ -184,7 +193,10 @@ have to create a mutation for sending messages via the `channel_layer`
184193
```python
185194
# mysite/gqlchat/subscription.py
186195

196+
from channels.layers import get_channel_layer
187197

198+
199+
@strawberry.type
188200
class Mutation:
189201
@strawberry.mutation
190202
async def send_chat_message(
@@ -193,8 +205,7 @@ class Mutation:
193205
room: ChatRoom,
194206
message: str,
195207
) -> None:
196-
ws = info.context["ws"]
197-
channel_layer = ws.channel_layer
208+
channel_layer = get_channel_layer()
198209

199210
await channel_layer.group_send(
200211
f"chat_{room.room_name}",
@@ -248,6 +259,7 @@ An example of this (continuing from channels tutorial) would be:
248259

249260
```python
250261
import os
262+
251263
from channels.auth import AuthMiddlewareStack
252264
from channels.routing import ProtocolTypeRouter, URLRouter
253265
from django.core.asgi import get_asgi_application
@@ -264,13 +276,14 @@ django_asgi_app = get_asgi_application()
264276
from chat import routing
265277
from mysite.graphql import schema
266278

267-
websocket_urlpatterns = routing.websocket_urlpatterns + [
268-
re_path(r"graphql", GraphQLWSConsumer.as_asgi(schema=schema)),
269-
]
270-
271279

272280
gql_http_consumer = AuthMiddlewareStack(GraphQLHTTPConsumer.as_asgi(schema=schema))
273281
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+
274287
application = ProtocolTypeRouter(
275288
{
276289
"http": URLRouter(

0 commit comments

Comments
 (0)