Metadata-Version: 2.1
Name: graphene-sqlalchemy
Version: 3.0.0rc2
Summary: Graphene SQLAlchemy integration
Home-page: https://github.com/graphql-python/graphene-sqlalchemy
Author: Syrus Akbary
Author-email: me@syrusakbary.com
License: MIT
Project-URL: Documentation, https://docs.graphene-python.org/projects/sqlalchemy/en/latest
Keywords: api graphql protocol rest relay graphene sqlalchemy
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: PyPy
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: graphene>=3.0.0b7
Requires-Dist: promise>=2.3
Requires-Dist: SQLAlchemy>=1.1
Requires-Dist: aiodataloader<1.0,>=0.2.0
Requires-Dist: packaging>=23.0
Provides-Extra: dev
Requires-Dist: tox==3.7.0; extra == "dev"
Requires-Dist: pre-commit==2.19; extra == "dev"
Requires-Dist: flake8==4.0.0; extra == "dev"
Provides-Extra: test
Requires-Dist: pytest<7.0,>=6.2.0; extra == "test"
Requires-Dist: pytest-asyncio>=0.18.3; extra == "test"
Requires-Dist: pytest-cov<3.0,>=2.11.0; extra == "test"
Requires-Dist: sqlalchemy-utils<1.0,>=0.37.0; extra == "test"
Requires-Dist: pytest-benchmark<4.0,>=3.4.0; extra == "test"
Requires-Dist: aiosqlite>=0.17.0; extra == "test"
Requires-Dist: nest-asyncio; extra == "test"
Requires-Dist: greenlet; extra == "test"

Version 3.0 is in beta stage. Please read https://github.com/graphql-python/graphene-sqlalchemy/issues/348 to learn about progress and changes in upcoming
beta releases.

---

# ![Graphene Logo](http://graphene-python.org/favicon.png) Graphene-SQLAlchemy 
[![Build Status](https://github.com/graphql-python/graphene-sqlalchemy/workflows/Tests/badge.svg)](https://github.com/graphql-python/graphene-sqlalchemy/actions)
[![PyPI version](https://badge.fury.io/py/graphene-sqlalchemy.svg)](https://badge.fury.io/py/graphene-sqlalchemy) 
![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/graphql-python/graphene-sqlalchemy?color=green&include_prereleases&label=latest)
[![codecov](https://codecov.io/gh/graphql-python/graphene-sqlalchemy/branch/master/graph/badge.svg?token=Zi5S1TikeN)](https://codecov.io/gh/graphql-python/graphene-sqlalchemy)



A [SQLAlchemy](http://www.sqlalchemy.org/) integration for [Graphene](http://graphene-python.org/).

## Installation

For installing Graphene, just run this command in your shell.

```bash
pip install --pre "graphene-sqlalchemy"
```

## Examples

Here is a simple SQLAlchemy model:

```python
from sqlalchemy import Column, Integer, String

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class UserModel(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    last_name = Column(String)
```

To create a GraphQL schema for it, you simply have to write the following:

```python
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType

class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        # use `only_fields` to only expose specific fields ie "name"
        # only_fields = ("name",)
        # use `exclude_fields` to exclude specific fields ie "last_name"
        # exclude_fields = ("last_name",)

class Query(graphene.ObjectType):
    users = graphene.List(User)

    def resolve_users(self, info):
        query = User.get_query(info)  # SQLAlchemy query
        return query.all()

schema = graphene.Schema(query=Query)
```

We need a database session first:

```python
from sqlalchemy import (create_engine)
from sqlalchemy.orm import (scoped_session, sessionmaker)

engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
# We will need this for querying, Graphene extracts the session from the base.
# Alternatively it can be provided in the GraphQLResolveInfo.context dictionary under context["session"]
Base.query = db_session.query_property()
```

Then you can simply query the schema:

```python
query = '''
    query {
      users {
        name,
        lastName
      }
    }
'''
result = schema.execute(query, context_value={'session': db_session})
```

You may also subclass SQLAlchemyObjectType by providing `abstract = True` in
your subclasses Meta:
```python
from graphene_sqlalchemy import SQLAlchemyObjectType

class ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):
    class Meta:
        abstract = True

    @classmethod
    def get_node(cls, info, id):
        return cls.get_query(info).filter(
            and_(cls._meta.model.deleted_at==None,
                 cls._meta.model.id==id)
            ).first()

class User(ActiveSQLAlchemyObjectType):
    class Meta:
        model = UserModel

class Query(graphene.ObjectType):
    users = graphene.List(User)

    def resolve_users(self, info):
        query = User.get_query(info)  # SQLAlchemy query
        return query.all()

schema = graphene.Schema(query=Query)
```

### Full Examples

To learn more check out the following [examples](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/):

- [Flask SQLAlchemy example](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/flask_sqlalchemy)
- [Nameko SQLAlchemy example](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/nameko_sqlalchemy)

## Contributing

See [CONTRIBUTING.md](https://github.com/graphql-python/graphene-sqlalchemy/blob/master/CONTRIBUTING.md)
