@@ -3,9 +3,12 @@ GraphQL WS
3
3
4
4
Websocket server for GraphQL subscriptions.
5
5
6
- Currently supports: \*
7
- `aiohttp <https://github.com/graphql-python/graphql-ws#aiohttp >`__ \*
8
- `Gevent <https://github.com/graphql-python/graphql-ws#gevent >`__
6
+ Currently supports:
7
+
8
+ * `aiohttp <https://github.com/graphql-python/graphql-ws#aiohttp >`__
9
+ * `Gevent <https://github.com/graphql-python/graphql-ws#gevent >`__
10
+ * Sanic (uses `websockets <https://github.com/aaugustin/websockets/ >`__
11
+ library)
9
12
10
13
Installation instructions
11
14
=========================
@@ -44,6 +47,29 @@ For setting up, just plug into your aiohttp server.
44
47
45
48
web.run_app(app, port = 8000 )
46
49
50
+ Sanic
51
+ ~~~~~
52
+
53
+ Works with any framework that uses the websockets library for it’s
54
+ websocket implementation. For this example, plug in your Sanic server.
55
+
56
+ .. code :: python
57
+
58
+ from graphql_ws.websockets_lib import WsLibSubscriptionServer
59
+
60
+
61
+ app = Sanic(__name__ )
62
+
63
+ subscription_server = WsLibSubscriptionServer(schema)
64
+
65
+ @app.websocket (' /subscriptions' , subprotocols = [' graphql-ws' ])
66
+ async def subscriptions (request , ws ):
67
+ await subscription_server.handle(ws)
68
+ return ws
69
+
70
+
71
+ app.run(host = " 0.0.0.0" , port = 8000 )
72
+
47
73
And then, plug into a subscribable schema:
48
74
49
75
.. code :: python
@@ -110,4 +136,76 @@ And then, plug into a subscribable schema:
110
136
schema = graphene.Schema(query = Query, subscription = Subscription)
111
137
112
138
You can see a full example here:
113
- https://github.com/graphql-python/graphql-ws/tree/master/examples/flask\_ gevent
139
+ https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
140
+
141
+ Django Channels
142
+ ~~~~~~~~~~~~~~~
143
+
144
+ First ``pip install channels `` and it to your django apps
145
+
146
+ Then add the following to your settings.py
147
+
148
+ .. code :: python
149
+
150
+ CHANNELS_WS_PROTOCOLS = [" graphql-ws" , ]
151
+ CHANNEL_LAYERS = {
152
+ " default" : {
153
+ " BACKEND" : " asgiref.inmemory.ChannelLayer" ,
154
+ " ROUTING" : " django_subscriptions.urls.channel_routing" ,
155
+ },
156
+
157
+ }
158
+
159
+ Setup your graphql schema
160
+
161
+ .. code :: python
162
+
163
+ import graphene
164
+ from rx import Observable
165
+
166
+
167
+ class Query (graphene .ObjectType ):
168
+ hello = graphene.String()
169
+
170
+ def resolve_hello (self , info , ** kwargs ):
171
+ return ' world'
172
+
173
+ class Subscription (graphene .ObjectType ):
174
+
175
+ count_seconds = graphene.Int(up_to = graphene.Int())
176
+
177
+
178
+ def resolve_count_seconds (
179
+ root ,
180
+ info ,
181
+ up_to = 5
182
+ ):
183
+ return Observable.interval(1000 )\
184
+ .map(lambda i : " {0} " .format(i))\
185
+ .take_while(lambda i : int (i) <= up_to)
186
+
187
+
188
+
189
+ schema = graphene.Schema(
190
+ query = Query,
191
+ subscription = Subscription
192
+ )
193
+
194
+ Setup your schema in settings.py
195
+
196
+ .. code :: python
197
+
198
+ GRAPHENE = {
199
+ ' SCHEMA' : ' path.to.schema'
200
+ }
201
+
202
+ and finally add the channel routes
203
+
204
+ .. code :: python
205
+
206
+ from channels.routing import route_class
207
+ from graphql_ws.django_channels import GraphQLSubscriptionConsumer
208
+
209
+ channel_routing = [
210
+ route_class(GraphQLSubscriptionConsumer, path = r " ^ /subscriptions" ),
211
+ ]
0 commit comments