File tree Expand file tree Collapse file tree 2 files changed +9
-12
lines changed Expand file tree Collapse file tree 2 files changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ your Sanic server.
51
51
from graphql_ws.websockets_lib import WsLibSubscriptionServer
52
52
53
53
54
+ app = Sanic(__name__ )
55
+
54
56
subscription_server = WsLibSubscriptionServer(schema)
55
57
56
58
@app.websocket (' /subscriptions' , subprotocols = [' graphql-ws' ])
Original file line number Diff line number Diff line change 1
- from graphql import format_error
2
1
from graphql_ws .websockets_lib import WsLibSubscriptionServer
2
+ from graphql .execution .executors .asyncio import AsyncioExecutor
3
3
from sanic import Sanic , response
4
+ from sanic_graphql import GraphQLView
4
5
from schema import schema
5
6
from template import render_graphiql
6
7
7
8
app = Sanic (__name__ )
8
9
9
10
10
- @app .route ('/graphql' , methods = ['GET' , 'POST' ])
11
- async def graphql_view (request ):
12
- payload = request .json
13
- result = await schema .execute (payload .get ('query' , '' ),
14
- return_promise = True )
15
- data = {}
16
- if result .errors :
17
- data ['errors' ] = [format_error (e ) for e in result .errors ]
18
- if result .data :
19
- data ['data' ] = result .data
20
- return response .json (data ,)
11
+ @app .listener ('before_server_start' )
12
+ def init_graphql (app , loop ):
13
+ app .add_route (GraphQLView .as_view (schema = schema ,
14
+ executor = AsyncioExecutor (loop = loop )),
15
+ '/graphql' )
21
16
22
17
23
18
@app .route ('/graphiql' )
You can’t perform that action at this time.
0 commit comments