1+ ==========
12GraphQL WS
23==========
34
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)
514
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 >`__)
720
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)
1221
1322Installation instructions
1423=========================
@@ -19,21 +28,54 @@ For instaling graphql-ws, just run this command in your shell
1928
2029 pip install graphql-ws
2130
31+
2232 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)
2465
2566 aiohttp
2667~~~~~~~
2768
28- For setting up, just plug into your aiohttp server.
69+ Then just plug into your aiohttp server.
2970
3071.. code :: python
3172
3273 from graphql_ws.aiohttp import AiohttpSubscriptionServer
33-
74+ from .schema import schema
3475
3576 subscription_server = AiohttpSubscriptionServer(schema)
3677
78+
3779 async def subscriptions (request ):
3880 ws = web.WebSocketResponse(protocols = (' graphql-ws' ,))
3981 await ws.prepare(request)
@@ -47,21 +89,26 @@ For setting up, just plug into your aiohttp server.
4789
4890 web.run_app(app, port = 8000 )
4991
50- Sanic
51- ~~~~~
92+ You can see a full example here:
93+ https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp
94+
5295
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.
55101
56102.. code :: python
57103
58104 from graphql_ws.websockets_lib import WsLibSubscriptionServer
59-
105+ from . import schema
60106
61107 app = Sanic(__name__ )
62108
63109 subscription_server = WsLibSubscriptionServer(schema)
64110
111+
65112 @app.websocket (' /subscriptions' , subprotocols = [' graphql-ws' ])
66113 async def subscriptions (request , ws ):
67114 await subscription_server.handle(ws)
@@ -70,80 +117,73 @@ websocket implementation. For this example, plug in your Sanic server.
70117
71118 app.run(host = " 0.0.0.0" , port = 8000 )
72119
73- And then, plug into a subscribable schema:
120+
121+ Python 2 servers
122+ -----------------
123+
124+ Create a subscribable schema like this:
74125
75126.. code :: python
76127
77- import asyncio
78128 import graphene
129+ from rx import Observable
79130
80131
81132 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"
83138
84139
85140 class Subscription (graphene .ObjectType ):
86141 count_seconds = graphene.Float(up_to = graphene.Int())
87142
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)
93147
94148
95149 schema = graphene.Schema(query = Query, subscription = Subscription)
96150
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+ ~~~~~~~~~~~~~~~~~~~~~~~~~
102153
103- For setting up, just plug into your Gevent server.
154+ Then just plug into your Gevent server, for example, Flask:
104155
105156.. code :: python
106157
158+ from flask_sockets import Sockets
159+ from graphql_ws.gevent import GeventSubscriptionServer
160+ from schema import schema
161+
107162 subscription_server = GeventSubscriptionServer(schema)
108163 app.app_protocol = lambda environ_path_info : ' graphql-ws'
109164
165+
110166 @sockets.route (' /subscriptions' )
111167 def echo_socket (ws ):
112168 subscription_server.handle(ws)
113169 return []
114170
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-
138171 You can see a full example here:
139172https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
140173
141- Django Channels
142- ~~~~~~~~~~~~~~~
174+ Django v1.x
175+ ~~~~~~~~~~~
143176
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 ``
145178
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 ``
147187
148188.. code :: python
149189
@@ -153,53 +193,9 @@ Then add the following to your settings.py
153193 " BACKEND" : " asgiref.inmemory.ChannelLayer" ,
154194 " ROUTING" : " django_subscriptions.urls.channel_routing" ,
155195 },
156-
157196 }
158197
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
203199
204200.. code :: python
205201
@@ -209,3 +205,6 @@ and finally add the channel routes
209205 channel_routing = [
210206 route_class(GraphQLSubscriptionConsumer, path = r " ^ /subscriptions" ),
211207 ]
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