Skip to content

Commit 927ca27

Browse files
committed
Improved codebase. Isolated modules (aiohttp, gevent) so they don’t collide
1 parent b3e5459 commit 927ca27

File tree

21 files changed

+307
-747
lines changed

21 files changed

+307
-747
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ python aio.py
2121

2222
## Setup
2323

24-
For setting up, just plug into your AioHTTP server.
24+
### aiohttp
25+
26+
For setting up, just plug into your aiohttp server.
2527

2628
```python
27-
subscription_server = WebSocketSubscriptionServer(schema)
29+
from graphql_ws.aiohttp import AiohttpSubscriptionServer
30+
31+
32+
subscription_server = AiohttpSubscriptionServer(schema)
2833

2934
async def subscriptions(request):
3035
ws = web.WebSocketResponse(protocols=('graphql-ws',))
@@ -63,3 +68,41 @@ class Subscription(graphene.ObjectType):
6368

6469
schema = graphene.Schema(query=Query, subscription=Subscription)
6570
```
71+
72+
73+
### Gevent
74+
75+
For setting up, just plug into your Gevent server.
76+
77+
```python
78+
subscription_server = GeventSubscriptionServer(schema)
79+
app.app_protocol = lambda environ_path_info: 'graphql-ws'
80+
81+
@sockets.route('/subscriptions')
82+
def echo_socket(ws):
83+
subscription_server.handle(ws)
84+
return []
85+
```
86+
87+
And then, plug into a subscribable schema:
88+
89+
```python
90+
import graphene
91+
from rx import Observable
92+
93+
94+
class Query(graphene.ObjectType):
95+
base = graphene.String()
96+
97+
98+
class Subscription(graphene.ObjectType):
99+
count_seconds = graphene.Float(up_to=graphene.Int())
100+
101+
async def resolve_count_seconds(root, info, up_to=5):
102+
return Observable.interval(1000)\
103+
.map(lambda i: "{0}".format(i))\
104+
.take_while(lambda i: int(i) <= up_to)
105+
106+
107+
schema = graphene.Schema(query=Query, subscription=Subscription)
108+
```
File renamed without changes.

examples/aio.py renamed to examples/aiohttp/app.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from aiohttp import web, WSMsgType
2-
from template import render_graphiql
1+
import json
2+
3+
from aiohttp import web
34
from schema import schema
45
from graphql import format_error
5-
import json
6-
from graphql_ws import WebSocketSubscriptionServer
6+
from graphql_ws.aiohttp import AiohttpSubscriptionServer
7+
8+
from template import render_graphiql
79

810

911
async def graphql_view(request):
@@ -21,27 +23,14 @@ async def graphql_view(request):
2123
async def graphiql_view(request):
2224
return web.Response(text=render_graphiql(), headers={'Content-Type': 'text/html'})
2325

24-
subscription_server = WebSocketSubscriptionServer(schema)
26+
subscription_server = AiohttpSubscriptionServer(schema)
2527

2628

2729
async def subscriptions(request):
2830
ws = web.WebSocketResponse(protocols=('graphql-ws',))
2931
await ws.prepare(request)
3032

3133
await subscription_server.handle(ws)
32-
33-
# async for msg in ws:
34-
# if msg.type == WSMsgType.TEXT:
35-
# if msg.data == 'close':
36-
# await ws.close()
37-
# else:
38-
# await ws.send_str(msg.data + '/answer')
39-
# elif msg.type == WSMsgType.ERROR:
40-
# print('ws connection closed with exception %s' %
41-
# ws.exception())
42-
43-
# print('websocket connection closed')
44-
4534
return ws
4635

4736

examples/aiohttp/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
graphql_ws
2+
aiohttp>=2.1.0
3+
graphene>=2.0

examples/aiohttp/schema.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import random
2+
import asyncio
3+
import graphene
4+
5+
6+
class Query(graphene.ObjectType):
7+
base = graphene.String()
8+
9+
10+
class RandomType(graphene.ObjectType):
11+
seconds = graphene.Int()
12+
random_int = graphene.Int()
13+
14+
15+
class Subscription(graphene.ObjectType):
16+
count_seconds = graphene.Float(up_to=graphene.Int())
17+
random_int = graphene.Field(RandomType)
18+
19+
async def resolve_count_seconds(root, info, up_to=5):
20+
for i in range(up_to):
21+
print("YIELD SECOND", i)
22+
yield i
23+
await asyncio.sleep(1.)
24+
yield up_to
25+
26+
async def resolve_random_int(root, info):
27+
i = 0
28+
while True:
29+
yield RandomType(seconds=i, random_int=random.randint(0, 500))
30+
await asyncio.sleep(1.)
31+
i += 1
32+
33+
34+
schema = graphene.Schema(query=Query, subscription=Subscription)
File renamed without changes.

examples/flask_gevent/flask_app.py renamed to examples/flask_gevent/app.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
11
import json
22

3-
import graphene
43
from flask import Flask, make_response
54
from flask_graphql import GraphQLView
65
from flask_sockets import Sockets
7-
from rx import Observable
8-
9-
from graphql_ws import GeventSubscriptionServer
10-
from template import render_graphiql
11-
12-
13-
class Query(graphene.ObjectType):
14-
base = graphene.String()
15-
16-
17-
class RandomType(graphene.ObjectType):
18-
seconds = graphene.Int()
19-
random_int = graphene.Int()
20-
21-
22-
class Subscription(graphene.ObjectType):
23-
24-
count_seconds = graphene.Int(up_to=graphene.Int())
25-
26-
random_int = graphene.Field(RandomType)
27-
28-
29-
def resolve_count_seconds(root, info, up_to):
30-
return Observable.interval(1000)\
31-
.map(lambda i: "{0}".format(i))\
32-
.take_while(lambda i: int(i) <= up_to)
33-
34-
def resolve_random_int(root, info):
35-
import random
36-
return Observable.interval(1000).map(lambda i: RandomType(seconds=i, random_int=random.randint(0, 500)))
37-
38-
schema = graphene.Schema(query=Query, subscription=Subscription)
396

7+
from graphql_ws.gevent import GeventSubscriptionServer
408

9+
from schema import schema
10+
from template import render_graphiql
4111

4212
app = Flask(__name__)
4313
app.debug = True
@@ -48,12 +18,14 @@ def resolve_random_int(root, info):
4818
def graphql_view():
4919
return make_response(render_graphiql())
5020

21+
5122
app.add_url_rule(
5223
'/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=False))
5324

5425
subscription_server = GeventSubscriptionServer(schema)
5526
app.app_protocol = lambda environ_path_info: 'graphql-ws'
5627

28+
5729
@sockets.route('/subscriptions')
5830
def echo_socket(ws):
5931
subscription_server.handle(ws)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
graphql_ws
2+
gevent
3+
flask
4+
flask_graphql
5+
flask_sockets
6+
graphene>=2.0

examples/flask_gevent/schema.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
import graphene
3+
from rx import Observable
4+
5+
6+
class Query(graphene.ObjectType):
7+
base = graphene.String()
8+
9+
10+
class RandomType(graphene.ObjectType):
11+
seconds = graphene.Int()
12+
random_int = graphene.Int()
13+
14+
15+
class Subscription(graphene.ObjectType):
16+
17+
count_seconds = graphene.Int(up_to=graphene.Int())
18+
19+
random_int = graphene.Field(RandomType)
20+
21+
def resolve_count_seconds(root, info, up_to=5):
22+
return Observable.interval(1000)\
23+
.map(lambda i: "{0}".format(i))\
24+
.take_while(lambda i: int(i) <= up_to)
25+
26+
def resolve_random_int(root, info):
27+
return Observable.interval(1000).map(lambda i: RandomType(seconds=i, random_int=random.randint(0, 500)))
28+
29+
30+
schema = graphene.Schema(query=Query, subscription=Subscription)

examples/flask_gevent/template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def render_graphiql():
117117
</script>
118118
</body>
119119
</html>''').substitute(
120-
GRAPHIQL_VERSION='0.10.2',
120+
GRAPHIQL_VERSION='0.11.7',
121121
SUBSCRIPTIONS_TRANSPORT_VERSION='0.7.0',
122122
subscriptionsEndpoint='ws://localhost:5000/subscriptions',
123123
# subscriptionsEndpoint='ws://localhost:5000/',

0 commit comments

Comments
 (0)