-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun.py
More file actions
52 lines (42 loc) · 1.41 KB
/
Copy pathrun.py
File metadata and controls
52 lines (42 loc) · 1.41 KB
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
48
49
50
51
52
from db import DBSession
from schemas import get_dataloaders, schema
from sanic import Sanic
from sanic.testing import SanicTestClient
from sanic_graphql import GraphQLView
from graphql.execution.executors.asyncio import AsyncioExecutor
from web_template import TEMPLATE
def create_app(path="/graphql", **kwargs):
app = Sanic(__name__)
app.debug = True
@app.listener("before_server_start")
def init_web_route(app, loop):
print("初始化web路由")
app.add_route(
GraphQLView.as_view(
schema=schema,
context=dict({"session": DBSession()}, **get_dataloaders()),
graphiql_template=TEMPLATE,
**kwargs
),
"/web",
)
@app.listener("before_server_start")
def init_async_executor(app, loop):
executor = AsyncioExecutor(loop)
app.add_route(
GraphQLView.as_view(
schema=schema,
executor=executor,
context=dict({"session": DBSession()}, **get_dataloaders()),
**kwargs
),
path,
)
@app.listener("before_server_stop")
def remove_graphql_endpoint(app, loop):
app.remove_route(path)
app.client = SanicTestClient(app)
return app
if __name__ == "__main__":
app = create_app(graphiql=True, async_executor=True)
app.run(host="0.0.0.0", port=5000)