{"id":13461701,"url":"https://github.com/graphql-python/graphene-sqlalchemy","last_synced_at":"2025-05-13T17:09:14.910Z","repository":{"id":11138068,"uuid":"68488677","full_name":"graphql-python/graphene-sqlalchemy","owner":"graphql-python","description":"Graphene SQLAlchemy integration","archived":false,"fork":false,"pushed_at":"2025-04-07T07:51:18.000Z","size":519,"stargazers_count":986,"open_issues_count":75,"forks_count":227,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-05-05T02:02:56.624Z","etag":null,"topics":["graphene","graphql","python","sqlalchemy"],"latest_commit_sha":null,"homepage":"http://docs.graphene-python.org/projects/sqlalchemy/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphql-python.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-09-18T01:50:59.000Z","updated_at":"2025-04-30T19:01:07.000Z","dependencies_parsed_at":"2024-01-11T19:17:22.832Z","dependency_job_id":"cbbf5fcd-3467-4b11-af34-1f21d5303f6e","html_url":"https://github.com/graphql-python/graphene-sqlalchemy","commit_stats":{"total_commits":210,"total_committers":63,"mean_commits":"3.3333333333333335","dds":0.8476190476190476,"last_synced_commit":"eb9c663cc0e314987397626573e3d2f940bea138"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphene-sqlalchemy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphene-sqlalchemy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphene-sqlalchemy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-python%2Fgraphene-sqlalchemy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-python","download_url":"https://codeload.github.com/graphql-python/graphene-sqlalchemy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990468,"owners_count":21995774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphene","graphql","python","sqlalchemy"],"created_at":"2024-07-31T11:00:53.182Z","updated_at":"2025-05-13T17:09:14.854Z","avatar_url":"https://github.com/graphql-python.png","language":"Python","readme":"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\nbeta releases.\n\n---\n\n# ![Graphene Logo](http://graphene-python.org/favicon.png) Graphene-SQLAlchemy \n[![Build Status](https://github.com/graphql-python/graphene-sqlalchemy/workflows/Tests/badge.svg)](https://github.com/graphql-python/graphene-sqlalchemy/actions)\n[![PyPI version](https://badge.fury.io/py/graphene-sqlalchemy.svg)](https://badge.fury.io/py/graphene-sqlalchemy) \n![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/graphql-python/graphene-sqlalchemy?color=green\u0026include_prereleases\u0026label=latest)\n[![codecov](https://codecov.io/gh/graphql-python/graphene-sqlalchemy/branch/master/graph/badge.svg?token=Zi5S1TikeN)](https://codecov.io/gh/graphql-python/graphene-sqlalchemy)\n\n\n\nA [SQLAlchemy](http://www.sqlalchemy.org/) integration for [Graphene](http://graphene-python.org/).\n\n## Installation\n\nFor installing Graphene, just run this command in your shell.\n\n```bash\npip install --pre \"graphene-sqlalchemy\"\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\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 graphene_sqlalchemy import SQLAlchemyObjectType\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\nclass Query(graphene.ObjectType):\n    users = graphene.List(User)\n\n    def resolve_users(self, info):\n        query = User.get_query(info)  # SQLAlchemy query\n        return query.all()\n\nschema = graphene.Schema(query=Query)\n```\n\nWe need a database session first:\n\n```python\nfrom sqlalchemy import (create_engine)\nfrom sqlalchemy.orm import (scoped_session, sessionmaker)\n\nengine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)\ndb_session = scoped_session(sessionmaker(autocommit=False,\n                                         autoflush=False,\n                                         bind=engine))\n# We will need this for querying, Graphene extracts the session from the base.\n# Alternatively it can be provided in the GraphQLResolveInfo.context dictionary under context[\"session\"]\nBase.query = db_session.query_property()\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```python\nfrom graphene_sqlalchemy import SQLAlchemyObjectType\n\nclass ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):\n    class Meta:\n        abstract = True\n\n    @classmethod\n    def get_node(cls, info, id):\n        return cls.get_query(info).filter(\n            and_(cls._meta.model.deleted_at==None,\n                 cls._meta.model.id==id)\n            ).first()\n\nclass User(ActiveSQLAlchemyObjectType):\n    class Meta:\n        model = UserModel\n\nclass Query(graphene.ObjectType):\n    users = graphene.List(User)\n\n    def resolve_users(self, info):\n        query = User.get_query(info)  # SQLAlchemy query\n        return query.all()\n\nschema = graphene.Schema(query=Query)\n```\n\n### Full Examples\n\nTo learn more check out the following [examples](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/):\n\n- [Flask SQLAlchemy example](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/flask_sqlalchemy)\n- [Nameko SQLAlchemy example](https://github.com/graphql-python/graphene-sqlalchemy/tree/master/examples/nameko_sqlalchemy)\n\n## Contributing\n\nSee [CONTRIBUTING.md](https://github.com/graphql-python/graphene-sqlalchemy/blob/master/CONTRIBUTING.md)\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-python%2Fgraphene-sqlalchemy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-python%2Fgraphene-sqlalchemy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-python%2Fgraphene-sqlalchemy/lists"}