|
| 1 | +|Build Status| |Coverage Status| |
| 2 | + |
| 3 | +graphene-tornado |
| 4 | +================ |
| 5 | + |
| 6 | +A project for running `Graphene <http://graphene-python.org/>`__ on top |
| 7 | +of `Tornado <http://www.tornadoweb.org/>`__ in Python 2 and 3. The |
| 8 | +codebase is a port of |
| 9 | +`graphene-django <https://github.com/graphql-python/graphene-django>`__. |
| 10 | + |
| 11 | +Getting started |
| 12 | +=============== |
| 13 | + |
| 14 | +Create a Tornado application and add the GraphQL handlers: |
| 15 | + |
| 16 | +.. code:: python |
| 17 | +
|
| 18 | + import tornado.web |
| 19 | + from tornado.ioloop import IOLoop |
| 20 | +
|
| 21 | + from graphene_tornado.schema import schema |
| 22 | + from graphene_tornado.tornado_graphql_handler import TornadoGraphQLHandler |
| 23 | +
|
| 24 | +
|
| 25 | + class ExampleApplication(tornado.web.Application): |
| 26 | +
|
| 27 | + def __init__(self): |
| 28 | + handlers = [ |
| 29 | + (r'/graphql', TornadoGraphQLHandler, dict(graphiql=True, schema=schema)), |
| 30 | + (r'/graphql/batch', TornadoGraphQLHandler, dict(graphiql=True, schema=schema, batch=True)), |
| 31 | + (r'/graphql/graphiql', TornadoGraphQLHandler, dict(graphiql=True, schema=schema)) |
| 32 | + ] |
| 33 | + tornado.web.Application.__init__(self, handlers) |
| 34 | +
|
| 35 | + if __name__ == '__main__': |
| 36 | + app = ExampleApplication() |
| 37 | + app.listen(5000) |
| 38 | + IOLoop.instance().start() |
| 39 | +
|
| 40 | +When writing your resolvers, decorate them with either Tornado’s |
| 41 | +``@coroutine`` decorator for Python 2.7: |
| 42 | + |
| 43 | +.. code:: python |
| 44 | +
|
| 45 | + @gen.coroutine |
| 46 | + def resolve_foo(self, info): |
| 47 | + foo = yield db.get_foo() |
| 48 | + raise Return(foo) |
| 49 | +
|
| 50 | +Or use the ``async`` / ``await`` pattern in Python 3: |
| 51 | + |
| 52 | +.. code:: python |
| 53 | +
|
| 54 | + async def resolve_foo(self, info): |
| 55 | + foo = await db.get_foo() |
| 56 | + return foo |
| 57 | +
|
| 58 | +.. |Build Status| image:: https://travis-ci.org/graphql-python/graphene-tornado.svg?branch=master |
| 59 | + :target: https://travis-ci.org/graphql-python/graphene-tornado |
| 60 | +.. |Coverage Status| image:: https://coveralls.io/repos/github/graphql-python/graphene-tornado/badge.svg?branch=master |
| 61 | + :target: https://coveralls.io/github/graphql-python/graphene-tornado?branch=master |
0 commit comments