alchql


Namealchql JSON
Version 3.4.10.45323042 PyPI version JSON
download
home_pagehttps://github.com/startupmillio/alchql
SummaryGraphene SQLAlchemy core integration
upload_time2024-01-24 11:21:05
maintainer
docs_urlNone
authorYorsh Sergey
requires_python
licenseMIT
keywords api graphql protocol rest relay graphene
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Please read [UPGRADE-v2.0.md](https://github.com/graphql-python/graphene/blob/master/UPGRADE-v2.0.md)
to learn how to upgrade to Graphene `2.0`.

---

# AlchQL

[![Lint](https://github.com/startupmillio/alchql/actions/workflows/python-black.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-black.yml)
[![PyTest](https://github.com/startupmillio/alchql/actions/workflows/python-pytest.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-pytest.yml)
[![Upload Python Package](https://github.com/startupmillio/alchql/actions/workflows/python-publish.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-publish.yml)

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

## Installation

For instaling graphene, just run this command in your shell

```bash
pip install "alchql>=3.0"
```

## 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 alchql 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 = await User.get_query(info)  # SQLAlchemy query
        return query.all()


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

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 alchql import SQLAlchemyObjectType
import sqlalchemy as sa
import graphene


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

    @classmethod
    async def get_node(cls, info, id):
        return (await cls.get_query(info)).filter(
            sa.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 = await User.get_query(info)  # SQLAlchemy query
        return query.all()


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

### Full Examples

To learn more check out the following [examples](examples/):

- [Flask SQLAlchemy example](examples/flask_sqlalchemy)
- [Nameko SQLAlchemy example](examples/nameko_sqlalchemy)
- [FastAPI SQLAlchemy example](examples/fastapi_sqlalchemy)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/startupmillio/alchql",
    "name": "alchql",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "api graphql protocol rest relay graphene",
    "author": "Yorsh Sergey",
    "author_email": "myrik260138@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "Please read [UPGRADE-v2.0.md](https://github.com/graphql-python/graphene/blob/master/UPGRADE-v2.0.md)\nto learn how to upgrade to Graphene `2.0`.\n\n---\n\n# AlchQL\n\n[![Lint](https://github.com/startupmillio/alchql/actions/workflows/python-black.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-black.yml)\n[![PyTest](https://github.com/startupmillio/alchql/actions/workflows/python-pytest.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-pytest.yml)\n[![Upload Python Package](https://github.com/startupmillio/alchql/actions/workflows/python-publish.yml/badge.svg)](https://github.com/startupmillio/alchql/actions/workflows/python-publish.yml)\n\nA [SQLAlchemy](http://www.sqlalchemy.org/) integration for [Graphene](http://graphene-python.org/).\n\n## Installation\n\nFor instaling graphene, just run this command in your shell\n\n```bash\npip install \"alchql>=3.0\"\n```\n\n## Examples\n\nHere is a simple SQLAlchemy model:\n\n```python\nfrom sqlalchemy import Column, Integer, String\n\nfrom sqlalchemy.ext.declarative import declarative_base\n\nBase = declarative_base()\n\n\nclass UserModel(Base):\n    __tablename__ = 'user'\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    last_name = Column(String)\n```\n\nTo create a GraphQL schema for it you simply have to write the following:\n\n```python\nimport graphene\nfrom alchql import SQLAlchemyObjectType\n\n\nclass User(SQLAlchemyObjectType):\n    class Meta:\n        model = UserModel\n        # use `only_fields` to only expose specific fields ie \"name\"\n        # only_fields = (\"name\",)\n        # use `exclude_fields` to exclude specific fields ie \"last_name\"\n        # exclude_fields = (\"last_name\",)\n\n\nclass Query(graphene.ObjectType):\n    users = graphene.List(User)\n\n    def resolve_users(self, info):\n        query = await User.get_query(info)  # SQLAlchemy query\n        return query.all()\n\n\nschema = graphene.Schema(query=Query)\n```\n\nThen you can simply query the schema:\n\n```python\nquery = '''\n    query {\n      users {\n        name,\n        lastName\n      }\n    }\n'''\nresult = schema.execute(query, context_value={'session': db_session})\n```\n\nYou may also subclass SQLAlchemyObjectType by providing `abstract = True` in\nyour subclasses Meta:\n\n```python\nfrom alchql import SQLAlchemyObjectType\nimport sqlalchemy as sa\nimport graphene\n\n\nclass ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):\n    class Meta:\n        abstract = True\n\n    @classmethod\n    async def get_node(cls, info, id):\n        return (await cls.get_query(info)).filter(\n            sa.and_(\n                cls._meta.model.deleted_at == None,\n                cls._meta.model.id == id\n            )\n        ).first()\n\n\nclass User(ActiveSQLAlchemyObjectType):\n    class Meta:\n        model = UserModel\n\n\nclass Query(graphene.ObjectType):\n    users = graphene.List(User)\n\n    def resolve_users(self, info):\n        query = await User.get_query(info)  # SQLAlchemy query\n        return query.all()\n\n\nschema = graphene.Schema(query=Query)\n```\n\n### Full Examples\n\nTo learn more check out the following [examples](examples/):\n\n- [Flask SQLAlchemy example](examples/flask_sqlalchemy)\n- [Nameko SQLAlchemy example](examples/nameko_sqlalchemy)\n- [FastAPI SQLAlchemy example](examples/fastapi_sqlalchemy)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Graphene SQLAlchemy core integration",
    "version": "3.4.10.45323042",
    "project_urls": {
        "Homepage": "https://github.com/startupmillio/alchql"
    },
    "split_keywords": [
        "api",
        "graphql",
        "protocol",
        "rest",
        "relay",
        "graphene"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5acef8ef8a8dbcaf6e97dc36f248cadd7a204fdc66861a124b706075a88ed41",
                "md5": "d077dc2a59f7e2354db5a9b9b6329542",
                "sha256": "5e9a2bc8c286d68d9c9f3cfbf5d08d95b60afd793432fabdae772304429525a3"
            },
            "downloads": -1,
            "filename": "alchql-3.4.10.45323042-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d077dc2a59f7e2354db5a9b9b6329542",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 51754,
            "upload_time": "2024-01-24T11:21:05",
            "upload_time_iso_8601": "2024-01-24T11:21:05.775600Z",
            "url": "https://files.pythonhosted.org/packages/a5/ac/ef8ef8a8dbcaf6e97dc36f248cadd7a204fdc66861a124b706075a88ed41/alchql-3.4.10.45323042-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-24 11:21:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "startupmillio",
    "github_project": "alchql",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "alchql"
}
        
Elapsed time: 0.17476s