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.
5151from graphql_ws.websockets_lib import WsLibSubscriptionServer
5252
5353
54+ app = Sanic(__name__ )
55+
5456subscription_server = WsLibSubscriptionServer(schema)
5557
5658@app.websocket (' /subscriptions' , subprotocols = [' graphql-ws' ])
Original file line number Diff line number Diff line change 1- from graphql import format_error
21from graphql_ws .websockets_lib import WsLibSubscriptionServer
2+ from graphql .execution .executors .asyncio import AsyncioExecutor
33from sanic import Sanic , response
4+ from sanic_graphql import GraphQLView
45from schema import schema
56from template import render_graphiql
67
78app = Sanic (__name__ )
89
910
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' )
2116
2217
2318@app .route ('/graphiql' )
You can’t perform that action at this time.
0 commit comments