Skip to content

Commit fcb5976

Browse files
committed
test it with redis and some workers
1 parent 6110b29 commit fcb5976

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
from channels.asgi import get_channel_layer
3+
4+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_subscriptions.settings")
5+
6+
channel_layer = get_channel_layer()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import graphene
2+
from rx import Observable
3+
4+
5+
class Query(graphene.ObjectType):
6+
hello = graphene.String()
7+
8+
def resolve_hello(self, info, **kwargs):
9+
return 'world'
10+
11+
class Subscription(graphene.ObjectType):
12+
13+
count_seconds = graphene.Int(up_to=graphene.Int())
14+
15+
16+
def resolve_count_seconds(root, info, up_to=5):
17+
return Observable.interval(1000)\
18+
.map(lambda i: "{0}".format(i))\
19+
.take_while(lambda i: int(i) <= up_to)
20+
21+
22+
23+
schema = graphene.Schema(query=Query, subscription=Subscription)

examples/django_subscriptions/django_subscriptions/settings.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,36 +122,16 @@
122122
CHANNELS_WS_PROTOCOLS = ["graphql-ws", ]
123123
CHANNEL_LAYERS = {
124124
"default": {
125-
"BACKEND": "asgiref.inmemory.ChannelLayer",
125+
"BACKEND": "asgi_redis.RedisChannelLayer",
126+
"CONFIG": {
127+
"hosts": [("localhost", 6379)],
128+
},
126129
"ROUTING": "django_subscriptions.urls.channel_routing",
127130
},
128131

129132
}
130133

131-
import graphene
132-
from rx import Observable
133-
134-
135-
class Query(graphene.ObjectType):
136-
hello = graphene.String()
137-
138-
def resolve_hello(self, info, **kwargs):
139-
return 'world'
140-
141-
class Subscription(graphene.ObjectType):
142-
143-
count_seconds = graphene.Int(up_to=graphene.Int())
144-
145-
146-
def resolve_count_seconds(root, info, up_to=5):
147-
return Observable.interval(1000)\
148-
.map(lambda i: "{0}".format(i))\
149-
.take_while(lambda i: int(i) <= up_to)
150-
151-
152-
153-
schema = graphene.Schema(query=Query, subscription=Subscription)
154134

155135
GRAPHENE = {
156-
'SCHEMA': schema
136+
'SCHEMA': 'django_subscriptions.schema.schema'
157137
}

graphql_ws/django_channels.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def __init__(self, message, request_context = None):
2525

2626
def send(self, data):
2727
self.message.reply_channel.send(data)
28+
29+
def close(self, reason):
30+
data = {
31+
'close': True,
32+
'text': reason
33+
}
34+
self.message.reply_channel.send(data)
2835

2936
class DjangoChannelSubscriptionServer(BaseSubscriptionServer):
3037

@@ -35,7 +42,7 @@ def get_graphql_params(self, *args, **kwargs):
3542

3643
def handle(self, message, connection_context):
3744
self.on_message(connection_context, message)
38-
45+
3946
def send_message(self, connection_context, op_id=None, op_type=None, payload=None):
4047
message = {}
4148
if op_id is not None:
@@ -46,7 +53,6 @@ def send_message(self, connection_context, op_id=None, op_type=None, payload=Non
4653
message['payload'] = payload
4754

4855
assert message, "You need to send at least one thing"
49-
5056
return connection_context.send({'text': json.dumps(message)})
5157

5258
def on_open(self, connection_context):
@@ -98,10 +104,8 @@ class GraphQLSubscriptionConsumer(JsonWebsocketConsumer):
98104
strict_ordering = True
99105

100106
def connect(self, message, **kwargs):
101-
schema = getattr(settings, "schema", None)
102-
103107
message.reply_channel.send({"accept": True})
104-
print(self)
108+
105109

106110
def receive(self, content, **kwargs):
107111
"""
@@ -113,13 +117,6 @@ def receive(self, content, **kwargs):
113117
self.subscription_server.on_open(self.connection_context)
114118
self.subscription_server.handle(content, self.connection_context)
115119

116-
def disconnect(self, message, **kwargs):
117-
"""
118-
Perform things on connection close
119-
"""
120-
# self.subscription_cserver.on_close(self.connection_context)
121-
122-
123120
class SubscriptionObserver(Observer):
124121

125122
def __init__(self, connection_context, op_id, send_execution_result, send_error, on_close):

0 commit comments

Comments
 (0)