Package: graphql-ws-subs
GraphQL WSWebsocket server for GraphQL subscriptions.Currently supports:aiohttpGeventSanic (useswebsocketslibrary)Installation instructionsFor instaling graphql-ws, just run this command in your shellpipinstallgraphql-wsExamplesaiohttpFor setting up, just plug into your aiohttp server.fromgraphql_ws.aiohttpimportAiohttpSubscriptionServersubscription_server=AiohttpSubscriptionServer(schema)asyncdefsubscriptions(request):ws=web.WebSocketResponse(protocols=('graphql-ws',))awaitws.prepare(request)awaitsubscription_server.handle(ws)returnwsapp=web.Application()app.router.add_get('/subscriptions',subscriptions)web.run_app(app,port=8000)SanicWorks with any framework that uses the websockets library for it’s
websocket implementation. For this example, plug in your Sanic server.fromgraphql_ws.websockets_libimportWsLibSubscriptionServerapp=Sanic(__name__)subscription_server=WsLibSubscriptionServer(schema)@app.websocket('/subscriptions',subprotocols=['graphql-ws'])asyncdefsubscriptions(request,ws):awaitsubscription_server.handle(ws)returnwsapp.run(host="0.0.0.0",port=8000)And then, plug into a subscribable schema:importasyncioimportgrapheneclassQuery(graphene.ObjectType):base=graphene.String()classSubscription(graphene.ObjectType):count_seconds=graphene.Float(up_to=graphene.Int())asyncdefresolve_count_seconds(root,info,up_to):foriinrange(up_to):yieldiawaitasyncio.sleep(1.)yieldup_toschema=graphene.Schema(query=Query,subscription=Subscription)You can see a full example here:https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttpGeventFor setting up, just plug into your Gevent server.subscription_server=GeventSubscriptionServer(schema)app.app_protocol=lambdaenviron_path_info:'graphql-ws'@sockets.route('/subscriptions')defecho_socket(ws):subscription_server.handle(ws)return[]And then, plug into a subscribable schema:importgraphenefromrximportObservableclassQuery(graphene.ObjectType):base=graphene.String()classSubscription(graphene.ObjectType):count_seconds=graphene.Float(up_to=graphene.Int())asyncdefresolve_count_seconds(root,info,up_to=5):returnObservable.interval(1000)\.map(lambdai:"{0}".format(i))\.take_while(lambdai: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_geventDjango ChannelsFirstpip install channelsand it to your django appsThen add the following to your settings.pyCHANNELS_WS_PROTOCOLS=["graphql-ws",]CHANNEL_LAYERS={"default":{"BACKEND":"asgiref.inmemory.ChannelLayer","ROUTING":"django_subscriptions.urls.channel_routing",},}Setup your graphql schemaimportgraphenefromrximportObservableclassQuery(graphene.ObjectType):hello=graphene.String()defresolve_hello(self,info,**kwargs):return'world'classSubscription(graphene.ObjectType):count_seconds=graphene.Int(up_to=graphene.Int())defresolve_count_seconds(root,info,up_to=5):returnObservable.interval(1000)\.map(lambdai:"{0}".format(i))\.take_while(lambdai:int(i)<=up_to)schema=graphene.Schema(query=Query,subscription=Subscription)Setup your schema in settings.pyGRAPHENE={'SCHEMA':'path.to.schema'}and finally add the channel routesfromchannels.routingimportroute_classfromgraphql_ws.django_channelsimportGraphQLSubscriptionConsumerchannel_routing=[route_class(GraphQLSubscriptionConsumer,path=r"^/subscriptions"),]