GraphQL WS
Websocket server for GraphQL subscriptions.
Currently supports:
Sanic (uses websockets library)
Installation instructions
For instaling graphql-ws, just run this command in your shell
pip install graphql-ws
Examples
aiohttp
For setting up, just plug into your aiohttp server.
from graphql_ws.aiohttp import AiohttpSubscriptionServer
subscription_server = AiohttpSubscriptionServer(schema)
async def subscriptions(request):
ws = web.WebSocketResponse(protocols=('graphql-ws',))
await ws.prepare(request)
await subscription_server.handle(ws)
return ws
app = web.Application()
app.router.add_get('/subscriptions', subscriptions)
web.run_app(app, port=8000)
Sanic
Works with any framework that uses the websockets library for it’s websocket implementation. For this example, plug in your Sanic server.
from graphql_ws.websockets_lib import WsLibSubscriptionServer
app = Sanic(__name__)
subscription_server = WsLibSubscriptionServer(schema)
@app.websocket('/subscriptions', subprotocols=['graphql-ws'])
async def subscriptions(request, ws):
await subscription_server.handle(ws)
return ws
app.run(host="0.0.0.0", port=8000)
And then, plug into a subscribable schema:
import asyncio
import graphene
class Query(graphene.ObjectType):
base = graphene.String()
class Subscription(graphene.ObjectType):
count_seconds = graphene.Float(up_to=graphene.Int())
async def resolve_count_seconds(root, info, up_to):
for i in range(up_to):
yield i
await asyncio.sleep(1.)
yield up_to
schema = graphene.Schema(query=Query, subscription=Subscription)
You can see a full example here: https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp
Gevent
For setting up, just plug into your Gevent server.
subscription_server = GeventSubscriptionServer(schema)
app.app_protocol = lambda environ_path_info: 'graphql-ws'
@sockets.route('/subscriptions')
def echo_socket(ws):
subscription_server.handle(ws)
return []
And then, plug into a subscribable schema:
import graphene
from rx import Observable
class Query(graphene.ObjectType):
base = graphene.String()
class Subscription(graphene.ObjectType):
count_seconds = graphene.Float(up_to=graphene.Int())
async def resolve_count_seconds(root, info, up_to=5):
return Observable.interval(1000)\
.map(lambda i: "{0}".format(i))\
.take_while(lambda i: int(i) <= up_to)
schema = graphene.Schema(query=Query, subscription=Subscription)
You can see a full example here: https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
Django Channels
First pip install channels and it to your django apps
Then add the following to your settings.py
CHANNELS_WS_PROTOCOLS = ["graphql-ws", ]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "django_subscriptions.urls.channel_routing",
},
}
Setup your graphql schema
import graphene
from rx import Observable
class Query(graphene.ObjectType):
hello = graphene.String()
def resolve_hello(self, info, **kwargs):
return 'world'
class Subscription(graphene.ObjectType):
count_seconds = graphene.Int(up_to=graphene.Int())
def resolve_count_seconds(
root,
info,
up_to=5
):
return Observable.interval(1000)\
.map(lambda i: "{0}".format(i))\
.take_while(lambda i: int(i) <= up_to)
schema = graphene.Schema(
query=Query,
subscription=Subscription
)
Setup your schema in settings.py
GRAPHENE = {
'SCHEMA': 'path.to.schema'
}
and finally add the channel routes
from channels.routing import route_class
from graphql_ws.django_channels import GraphQLSubscriptionConsumer
channel_routing = [
route_class(GraphQLSubscriptionConsumer, path=r"^/subscriptions"),
]
Metadata
Release files for graphql-ws-subs 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| graphql-ws-subs-0.0.2.tar.gz | 23.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| graphql_ws_subs-0.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 38.6 kB
Release files / graphql-ws-subs-0.0.2.tar.gz
| Download URL | graphql-ws-subs-0.0.2.tar.gz |
|---|---|
| Size | 23.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da8c342a96918636c2b1629264b13e09d4e3256503dedf77a54e956bdff762b6
|
|
BLAKE2b-256 checksum How to use checksums |
ca4ee21b7c51f57672d6fbb2182a374eee68439ba9b4ea8f36e781542a267e95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.3
|
Release files / graphql_ws_subs-0.0.2-py2.py3-none-any.whl
| Download URL | graphql_ws_subs-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
078beaf488f627fe003ea6dcc958155f1a4faff5f36dcfe22829cf9f4fbe8957
|
|
BLAKE2b-256 checksum How to use checksums |
bc00ddb5d902b4cdaffd7f8d29795356ada2b63398987c63c83a2ae675a82e08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.3
|