Skip to content

Commit 8d32f4b

Browse files
committed
Tidy up django_channels (1) backend and example
1 parent a8c2f33 commit 8d32f4b

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

examples/django_subscriptions/django_subscriptions/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@
118118
]
119119
CHANNEL_LAYERS = {
120120
"default": {
121-
"BACKEND": "asgi_redis.RedisChannelLayer",
122-
"CONFIG": {"hosts": [("localhost", 6379)]},
121+
"BACKEND": "asgiref.inmemory.ChannelLayer",
123122
"ROUTING": "django_subscriptions.urls.channel_routing",
124123
},
125124
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-e ../..
2+
django<2
3+
channels<2
4+
graphene_django<3

graphql_ws/django_channels.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,40 @@
88

99

1010
class DjangoChannelConnectionContext(BaseConnectionContext):
11-
def __init__(self, message, request_context=None):
12-
self.message = message
13-
self.operations = {}
14-
self.request_context = request_context
11+
def __init__(self, message):
12+
super(DjangoChannelConnectionContext, self).__init__(
13+
message.reply_channel,
14+
request_context={"user": message.user, "session": message.http_session},
15+
)
1516

1617
def send(self, data):
17-
self.message.reply_channel.send({"text": json.dumps(data)})
18+
self.ws.send({"text": json.dumps(data)})
1819

1920
def close(self, reason):
2021
data = {"close": True, "text": reason}
21-
self.message.reply_channel.send(data)
22+
self.ws.send(data)
2223

2324

2425
class DjangoChannelSubscriptionServer(BaseSyncSubscriptionServer):
2526
def handle(self, message, connection_context):
2627
self.on_message(connection_context, message)
2728

2829

30+
subscription_server = DjangoChannelSubscriptionServer(graphene_settings.SCHEMA)
31+
32+
2933
class GraphQLSubscriptionConsumer(JsonWebsocketConsumer):
3034
http_user_and_session = True
3135
strict_ordering = True
3236

33-
def connect(self, message, **_kwargs):
37+
def connect(self, message, **kwargs):
3438
message.reply_channel.send({"accept": True})
3539

36-
def receive(self, content, **_kwargs):
40+
def receive(self, content, **kwargs):
3741
"""
3842
Called when a message is received with either text or bytes
3943
filled out.
4044
"""
41-
self.connection_context = DjangoChannelConnectionContext(self.message)
42-
self.subscription_server = DjangoChannelSubscriptionServer(
43-
graphene_settings.SCHEMA
44-
)
45-
self.subscription_server.on_open(self.connection_context)
46-
self.subscription_server.handle(content, self.connection_context)
45+
context = DjangoChannelConnectionContext(self.message)
46+
subscription_server.on_open(context)
47+
subscription_server.handle(content, context)

0 commit comments

Comments
 (0)