graphql-python / aiohttp-graphql

Adds GraphQL support to your aiohttp app.
Other
119 stars 24 forks source link
aiohttp graphql

aiohttp-graphql

Adds GraphQL support to your aiohttp application.

Based on flask-graphql by Syrus Akbary and sanic-graphql by Sergey Porivaev.

PyPI version Build Status Coverage Status

Usage

Use the GraphQLView view from aiohttp_graphql

from aiohttp import web
from aiohttp_graphql import GraphQLView

from schema import schema

app = web.Application()

GraphQLView.attach(app, schema=schema, graphiql=True)

# Optional, for adding batch query support (used in Apollo-Client)
GraphQLView.attach(app, schema=schema, batch=True, route_path="/graphql/batch")

if __name__ == '__main__':
    web.run_app(app)

This will add /graphql endpoint to your app (customizable by passing route_path='/mypath' to GraphQLView.attach) and enable the GraphiQL IDE.

Note: GraphQLView.attach is just a convenience function, and the same functionality can be achieved with

gql_view = GraphQLView(schema=schema, graphiql=True)
app.router.add_route('*', '/graphql', gql_view, name='graphql')

It's worth noting that the the "view function" of GraphQLView is contained in GraphQLView.__call__. So, when you create an instance, that instance is callable with the request object as the sole positional argument. To illustrate:

gql_view = GraphQLView(schema=Schema, **kwargs)
gql_view(request)  # <-- the instance is callable and expects a `aiohttp.web.Request` object.

Supported options for GraphQLView

Contributing

Since v3, aiohttp-graphql code lives at graphql-server repository to keep any breaking change on the base package on sync with all other integrations. In order to contribute, please take a look at CONTRIBUTING.md.

License

Copyright for portions of project aiohttp-graphql are held by Syrus Akbary as part of project flask-graphql and sanic-graphql as part of project Sergey Porivaev. All other claims to this project aiohttp-graphql are held by Devin Fee.

This project is licensed under the MIT License.