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