1
+ ==========
1
2
GraphQL WS
2
3
==========
3
4
4
- Websocket server for GraphQL subscriptions.
5
+ Websocket backend for GraphQL subscriptions.
6
+
7
+ Supports the following application servers:
8
+
9
+ Python 3 application servers, using asyncio:
10
+
11
+ * `aiohttp `_
12
+ * `websockets compatible servers `_ such as Sanic
13
+ (via `websockets <https://github.com/aaugustin/websockets/ >`__ library)
5
14
6
- Currently supports:
15
+ Python 2 application servers:
16
+
17
+ * `Gevent compatible servers `_ such as Flask
18
+ * `Django v1.x `_
19
+ (via `channels v1.x <https://channels.readthedocs.io/en/1.x/inshort.html >`__)
7
20
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)
12
21
13
22
Installation instructions
14
23
=========================
@@ -19,21 +28,54 @@ For instaling graphql-ws, just run this command in your shell
19
28
20
29
pip install graphql-ws
21
30
31
+
22
32
Examples
23
- --------
33
+ ========
34
+
35
+ Python 3 servers
36
+ ----------------
37
+
38
+ Create a subscribable schema like this:
39
+
40
+ .. code :: python
41
+
42
+ import asyncio
43
+ import graphene
44
+
45
+
46
+ class Query (graphene .ObjectType ):
47
+ hello = graphene.String()
48
+
49
+ @static_method
50
+ def resolve_hello (obj , info , ** kwargs ):
51
+ return " world"
52
+
53
+
54
+ class Subscription (graphene .ObjectType ):
55
+ count_seconds = graphene.Float(up_to = graphene.Int())
56
+
57
+ async def resolve_count_seconds (root , info , up_to ):
58
+ for i in range (up_to):
59
+ yield i
60
+ await asyncio.sleep(1 .)
61
+ yield up_to
62
+
63
+
64
+ schema = graphene.Schema(query = Query, subscription = Subscription)
24
65
25
66
aiohttp
26
67
~~~~~~~
27
68
28
- For setting up, just plug into your aiohttp server.
69
+ Then just plug into your aiohttp server.
29
70
30
71
.. code :: python
31
72
32
73
from graphql_ws.aiohttp import AiohttpSubscriptionServer
33
-
74
+ from .schema import schema
34
75
35
76
subscription_server = AiohttpSubscriptionServer(schema)
36
77
78
+
37
79
async def subscriptions (request ):
38
80
ws = web.WebSocketResponse(protocols = (' graphql-ws' ,))
39
81
await ws.prepare(request)
@@ -47,21 +89,26 @@ For setting up, just plug into your aiohttp server.
47
89
48
90
web.run_app(app, port = 8000 )
49
91
50
- Sanic
51
- ~~~~~
92
+ You can see a full example here:
93
+ https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp
94
+
52
95
53
- Works with any framework that uses the websockets library for it’s
54
- websocket implementation. For this example, plug in your Sanic server.
96
+ websockets compatible servers
97
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
+
99
+ Works with any framework that uses the websockets library for its websocket
100
+ implementation. For this example, plug in your Sanic server.
55
101
56
102
.. code :: python
57
103
58
104
from graphql_ws.websockets_lib import WsLibSubscriptionServer
59
-
105
+ from . import schema
60
106
61
107
app = Sanic(__name__ )
62
108
63
109
subscription_server = WsLibSubscriptionServer(schema)
64
110
111
+
65
112
@app.websocket (' /subscriptions' , subprotocols = [' graphql-ws' ])
66
113
async def subscriptions (request , ws ):
67
114
await subscription_server.handle(ws)
@@ -70,80 +117,73 @@ websocket implementation. For this example, plug in your Sanic server.
70
117
71
118
app.run(host = " 0.0.0.0" , port = 8000 )
72
119
73
- And then, plug into a subscribable schema:
120
+
121
+ Python 2 servers
122
+ -----------------
123
+
124
+ Create a subscribable schema like this:
74
125
75
126
.. code :: python
76
127
77
- import asyncio
78
128
import graphene
129
+ from rx import Observable
79
130
80
131
81
132
class Query (graphene .ObjectType ):
82
- base = graphene.String()
133
+ hello = graphene.String()
134
+
135
+ @static_method
136
+ def resolve_hello (obj , info , ** kwargs ):
137
+ return " world"
83
138
84
139
85
140
class Subscription (graphene .ObjectType ):
86
141
count_seconds = graphene.Float(up_to = graphene.Int())
87
142
88
- async def resolve_count_seconds (root , info , up_to ):
89
- for i in range (up_to):
90
- yield i
91
- await asyncio.sleep(1 .)
92
- yield up_to
143
+ async def resolve_count_seconds (root , info , up_to = 5 ):
144
+ return Observable.interval(1000 )\
145
+ .map(lambda i : " {0} " .format(i))\
146
+ .take_while(lambda i : int (i) <= up_to)
93
147
94
148
95
149
schema = graphene.Schema(query = Query, subscription = Subscription)
96
150
97
- You can see a full example here:
98
- https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp
99
-
100
- Gevent
101
- ~~~~~~
151
+ Gevent compatible servers
152
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
102
153
103
- For setting up, just plug into your Gevent server.
154
+ Then just plug into your Gevent server, for example, Flask:
104
155
105
156
.. code :: python
106
157
158
+ from flask_sockets import Sockets
159
+ from graphql_ws.gevent import GeventSubscriptionServer
160
+ from schema import schema
161
+
107
162
subscription_server = GeventSubscriptionServer(schema)
108
163
app.app_protocol = lambda environ_path_info : ' graphql-ws'
109
164
165
+
110
166
@sockets.route (' /subscriptions' )
111
167
def echo_socket (ws ):
112
168
subscription_server.handle(ws)
113
169
return []
114
170
115
- And then, plug into a subscribable schema:
116
-
117
- .. code :: python
118
-
119
- import graphene
120
- from rx import Observable
121
-
122
-
123
- class Query (graphene .ObjectType ):
124
- base = graphene.String()
125
-
126
-
127
- class Subscription (graphene .ObjectType ):
128
- count_seconds = graphene.Float(up_to = graphene.Int())
129
-
130
- async def resolve_count_seconds (root , info , up_to = 5 ):
131
- return Observable.interval(1000 )\
132
- .map(lambda i : " {0} " .format(i))\
133
- .take_while(lambda i : int (i) <= up_to)
134
-
135
-
136
- schema = graphene.Schema(query = Query, subscription = Subscription)
137
-
138
171
You can see a full example here:
139
172
https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
140
173
141
- Django Channels
142
- ~~~~~~~~~~~~~~~
174
+ Django v1.x
175
+ ~~~~~~~~~~~
143
176
144
- First `` pip install channels `` and it to your django apps
177
+ For Django v1.x and Django Channels v1.x, setup your schema in `` settings.py ``
145
178
146
- Then add the following to your settings.py
179
+ .. code :: python
180
+
181
+ GRAPHENE = {
182
+ ' SCHEMA' : ' yourproject.schema.schema'
183
+ }
184
+
185
+ Then ``pip install "channels<1" `` and it to your django apps, adding the
186
+ following to your ``settings.py ``
147
187
148
188
.. code :: python
149
189
@@ -153,53 +193,9 @@ Then add the following to your settings.py
153
193
" BACKEND" : " asgiref.inmemory.ChannelLayer" ,
154
194
" ROUTING" : " django_subscriptions.urls.channel_routing" ,
155
195
},
156
-
157
196
}
158
197
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
198
+ And finally add the channel routes
203
199
204
200
.. code :: python
205
201
@@ -209,3 +205,6 @@ and finally add the channel routes
209
205
channel_routing = [
210
206
route_class(GraphQLSubscriptionConsumer, path = r " ^ /subscriptions" ),
211
207
]
208
+
209
+ You can see a full example here:
210
+ https://github.com/graphql-python/graphql-ws/tree/master/examples/django_subscriptions
0 commit comments