-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathapp.py
47 lines (32 loc) · 1.25 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from asyncio import Queue
from tornado import web, ioloop, websocket
from graphene_tornado.tornado_graphql_handler import TornadoGraphQLHandler
from graphql_ws.tornado import TornadoSubscriptionServer
from graphql_ws.constants import GRAPHQL_WS
from .template import render_graphiql
from .schema import schema
class GraphiQLHandler(web.RequestHandler):
def get(self):
self.finish(render_graphiql())
class SubscriptionHandler(websocket.WebSocketHandler):
def initialize(self, subscription_server):
self.subscription_server = subscription_server
self.queue = Queue(100)
def select_subprotocol(self, subprotocols):
return GRAPHQL_WS
def open(self):
ioloop.IOLoop.current().spawn_callback(self.subscription_server.handle, self)
async def on_message(self, message):
await self.queue.put(message)
async def recv(self):
return await self.queue.get()
subscription_server = TornadoSubscriptionServer(schema)
app = web.Application([
(r"/graphql$", TornadoGraphQLHandler, dict(
schema=schema)),
(r"/subscriptions", SubscriptionHandler, dict(
subscription_server=subscription_server)),
(r"/graphiql$", GraphiQLHandler),
])
app.listen(8000)
ioloop.IOLoop.current().start()