|
| 1 | +You are in the `next` unreleased version of Graphene-sqlalchemy (`1.0.dev`). |
| 2 | +Please read [UPGRADE-v1.0.md](https://github.com/graphql-python/graphene/blob/master/UPGRADE-v1.0.md) to learn how to upgrade. |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +#  Graphene-SQLAlchemy [](https://travis-ci.org/graphql-python/graphene-sqlalchemy) [](https://badge.fury.io/py/graphene-sqlalchemy) [](https://coveralls.io/github/graphql-python/graphene-sqlalchemy?branch=master) |
| 7 | + |
| 8 | + |
| 9 | +A [SQLAlchemy](http://www.sqlalchemy.org/) integration for [Graphene](http://graphene-python.org/). |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +For instaling graphene, just run this command in your shell |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install "graphene-sqlalchemy>=1.0.dev" |
| 17 | +``` |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +Here is a simple SQLAlchemy model: |
| 22 | + |
| 23 | +```python |
| 24 | +from sqlalchemy import Column, Integer, String |
| 25 | +from sqlalchemy.orm import backref, relationship |
| 26 | + |
| 27 | +from sqlalchemy.ext.declarative import declarative_base |
| 28 | + |
| 29 | +Base = declarative_base() |
| 30 | + |
| 31 | +class UserModel(Base): |
| 32 | + __tablename__ = 'department' |
| 33 | + id = Column(Integer, primary_key=True) |
| 34 | + name = Column(String) |
| 35 | + last_name = Column(String) |
| 36 | +``` |
| 37 | + |
| 38 | +To create a GraphQL schema for it you simply have to write the following: |
| 39 | + |
| 40 | +```python |
| 41 | +from graphene_sqlalchemy import SQLAlchemyObjectType |
| 42 | + |
| 43 | +class User(SQLAlchemyObjectType): |
| 44 | + class Meta: |
| 45 | + model = UserModel |
| 46 | + |
| 47 | +class Query(graphene.ObjectType): |
| 48 | + users = graphene.List(User) |
| 49 | + |
| 50 | + def resolve_users(self, args, context, info): |
| 51 | + query = User.get_query(context) # SQLAlchemy query |
| 52 | + return query.all() |
| 53 | + |
| 54 | +schema = graphene.Schema(query=QueryRoot) |
| 55 | +``` |
| 56 | + |
| 57 | +Then you can simply query the schema: |
| 58 | + |
| 59 | +```python |
| 60 | +query = ''' |
| 61 | + query { |
| 62 | + users { |
| 63 | + name, |
| 64 | + lastName |
| 65 | + } |
| 66 | + } |
| 67 | +''' |
| 68 | +result = schema.execute(query, context_value={'session': db_session}) |
| 69 | +``` |
| 70 | + |
| 71 | +To learn more check out the following [examples](examples/): |
| 72 | + |
| 73 | +* **Full example**: [Flask SQLAlchemy example](examples/flask_sqlalchemy) |
| 74 | + |
| 75 | + |
| 76 | +## Contributing |
| 77 | + |
| 78 | +After cloning this repo, ensure dependencies are installed by running: |
| 79 | + |
| 80 | +```sh |
| 81 | +python setup.py install |
| 82 | +``` |
| 83 | + |
| 84 | +After developing, the full test suite can be evaluated by running: |
| 85 | + |
| 86 | +```sh |
| 87 | +python setup.py test # Use --pytest-args="-v -s" for verbose mode |
| 88 | +``` |
0 commit comments