Skip to content

Commit

Permalink
Change graphql handler to use sanic_graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
hballard committed Jan 13, 2018
1 parent b1f39e7 commit f8885d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ your Sanic server.
from graphql_ws.websockets_lib import WsLibSubscriptionServer


app = Sanic(__name__)

subscription_server = WsLibSubscriptionServer(schema)

@app.websocket('/subscriptions', subprotocols=['graphql-ws'])
Expand Down
19 changes: 7 additions & 12 deletions examples/websockets_lib/app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
from graphql import format_error
from graphql_ws.websockets_lib import WsLibSubscriptionServer
from graphql.execution.executors.asyncio import AsyncioExecutor
from sanic import Sanic, response
from sanic_graphql import GraphQLView
from schema import schema
from template import render_graphiql

app = Sanic(__name__)


@app.route('/graphql', methods=['GET', 'POST'])
async def graphql_view(request):
payload = request.json
result = await schema.execute(payload.get('query', ''),
return_promise=True)
data = {}
if result.errors:
data['errors'] = [format_error(e) for e in result.errors]
if result.data:
data['data'] = result.data
return response.json(data,)
@app.listener('before_server_start')
def init_graphql(app, loop):
app.add_route(GraphQLView.as_view(schema=schema,
executor=AsyncioExecutor(loop=loop)),
'/graphql')


@app.route('/graphiql')
Expand Down

0 comments on commit f8885d6

Please sign in to comment.