|
| 1 | +GraphQL WS |
| 2 | +========== |
| 3 | + |
| 4 | +Websocket server for GraphQL subscriptions. |
| 5 | + |
| 6 | +Currently supports: \* |
| 7 | +`aiohttp <https://github.com/graphql-python/graphql-ws#aiohttp>`__ \* |
| 8 | +`Gevent <https://github.com/graphql-python/graphql-ws#gevent>`__ |
| 9 | + |
| 10 | +Installation instructions |
| 11 | +========================= |
| 12 | + |
| 13 | +For instaling graphql-ws, just run this command in your shell |
| 14 | + |
| 15 | +.. code:: bash |
| 16 | +
|
| 17 | + pip install graphql-ws |
| 18 | +
|
| 19 | +Examples |
| 20 | +-------- |
| 21 | + |
| 22 | +aiohttp |
| 23 | +~~~~~~~ |
| 24 | + |
| 25 | +For setting up, just plug into your aiohttp server. |
| 26 | + |
| 27 | +.. code:: python |
| 28 | +
|
| 29 | + from graphql_ws.aiohttp import AiohttpSubscriptionServer |
| 30 | +
|
| 31 | +
|
| 32 | + subscription_server = AiohttpSubscriptionServer(schema) |
| 33 | +
|
| 34 | + async def subscriptions(request): |
| 35 | + ws = web.WebSocketResponse(protocols=('graphql-ws',)) |
| 36 | + await ws.prepare(request) |
| 37 | +
|
| 38 | + await subscription_server.handle(ws) |
| 39 | + return ws |
| 40 | +
|
| 41 | +
|
| 42 | + app = web.Application() |
| 43 | + app.router.add_get('/subscriptions', subscriptions) |
| 44 | +
|
| 45 | + web.run_app(app, port=8000) |
| 46 | +
|
| 47 | +And then, plug into a subscribable schema: |
| 48 | + |
| 49 | +.. code:: python |
| 50 | +
|
| 51 | + import asyncio |
| 52 | + import graphene |
| 53 | +
|
| 54 | +
|
| 55 | + class Query(graphene.ObjectType): |
| 56 | + base = graphene.String() |
| 57 | +
|
| 58 | +
|
| 59 | + class Subscription(graphene.ObjectType): |
| 60 | + count_seconds = graphene.Float(up_to=graphene.Int()) |
| 61 | +
|
| 62 | + async def resolve_count_seconds(root, info, up_to): |
| 63 | + for i in range(up_to): |
| 64 | + yield i |
| 65 | + await asyncio.sleep(1.) |
| 66 | + yield up_to |
| 67 | +
|
| 68 | +
|
| 69 | + schema = graphene.Schema(query=Query, subscription=Subscription) |
| 70 | +
|
| 71 | +You can see a full example here: |
| 72 | +https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp |
| 73 | + |
| 74 | +Gevent |
| 75 | +~~~~~~ |
| 76 | + |
| 77 | +For setting up, just plug into your Gevent server. |
| 78 | + |
| 79 | +.. code:: python |
| 80 | +
|
| 81 | + subscription_server = GeventSubscriptionServer(schema) |
| 82 | + app.app_protocol = lambda environ_path_info: 'graphql-ws' |
| 83 | +
|
| 84 | + @sockets.route('/subscriptions') |
| 85 | + def echo_socket(ws): |
| 86 | + subscription_server.handle(ws) |
| 87 | + return [] |
| 88 | +
|
| 89 | +And then, plug into a subscribable schema: |
| 90 | + |
| 91 | +.. code:: python |
| 92 | +
|
| 93 | + import graphene |
| 94 | + from rx import Observable |
| 95 | +
|
| 96 | +
|
| 97 | + class Query(graphene.ObjectType): |
| 98 | + base = graphene.String() |
| 99 | +
|
| 100 | +
|
| 101 | + class Subscription(graphene.ObjectType): |
| 102 | + count_seconds = graphene.Float(up_to=graphene.Int()) |
| 103 | +
|
| 104 | + async def resolve_count_seconds(root, info, up_to=5): |
| 105 | + return Observable.interval(1000)\ |
| 106 | + .map(lambda i: "{0}".format(i))\ |
| 107 | + .take_while(lambda i: int(i) <= up_to) |
| 108 | +
|
| 109 | +
|
| 110 | + schema = graphene.Schema(query=Query, subscription=Subscription) |
| 111 | +
|
| 112 | +You can see a full example here: |
| 113 | +https://github.com/graphql-python/graphql-ws/tree/master/examples/flask\_gevent |
0 commit comments