Skip to content

Commit f8885d6

Browse files
committed
Change graphql handler to use sanic_graphql
1 parent b1f39e7 commit f8885d6

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ your Sanic server.
5151
from graphql_ws.websockets_lib import WsLibSubscriptionServer
5252

5353

54+
app = Sanic(__name__)
55+
5456
subscription_server = WsLibSubscriptionServer(schema)
5557

5658
@app.websocket('/subscriptions', subprotocols=['graphql-ws'])

examples/websockets_lib/app.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
from graphql import format_error
21
from graphql_ws.websockets_lib import WsLibSubscriptionServer
2+
from graphql.execution.executors.asyncio import AsyncioExecutor
33
from sanic import Sanic, response
4+
from sanic_graphql import GraphQLView
45
from schema import schema
56
from template import render_graphiql
67

78
app = 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')

0 commit comments

Comments
 (0)