Skip to content

Commit 1be8511

Browse files
authored
update readme (#8)
1 parent ebf1201 commit 1be8511

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,47 @@
55

66
A project for running [Graphene](http://graphene-python.org/) on top of [Tornado](http://www.tornadoweb.org/) in Python 2 and 3. The codebase is a port of [graphene-django](https://github.com/graphql-python/graphene-django).
77

8-
# Examples
8+
# Getting started
99

10-
See the [example](examples/example.py) application for an example on how to create GraphQL handlers in a Tornado project.
10+
Create a Tornado application and add the GraphQL handlers:
11+
12+
```python
13+
import tornado.web
14+
from tornado.ioloop import IOLoop
15+
16+
from graphene_tornado.schema import schema
17+
from graphene_tornado.tornado_graphql_handler import TornadoGraphQLHandler
18+
19+
20+
class ExampleApplication(tornado.web.Application):
21+
22+
def __init__(self):
23+
handlers = [
24+
(r'/graphql', TornadoGraphQLHandler, dict(graphiql=True, schema=schema)),
25+
(r'/graphql/batch', TornadoGraphQLHandler, dict(graphiql=True, schema=schema, batch=True)),
26+
(r'/graphql/graphiql', TornadoGraphQLHandler, dict(graphiql=True, schema=schema))
27+
]
28+
tornado.web.Application.__init__(self, handlers)
29+
30+
if __name__ == '__main__':
31+
app = ExampleApplication()
32+
app.listen(5000)
33+
IOLoop.instance().start()
34+
```
35+
36+
When writing your resolvers, decorate them with either Tornado's `@coroutine` decorator for Python 2.7:
37+
38+
```python
39+
@gen.coroutine
40+
def resolve_foo(self, info):
41+
foo = yield db.get_foo()
42+
raise Return(foo)
43+
```
44+
45+
Or use the `async` / `await` pattern in Python 3:
46+
47+
```python
48+
async def resolve_foo(self, info):
49+
foo = await db.get_foo()
50+
return foo
51+
```

0 commit comments

Comments
 (0)