MyGit
🚩收到GitHub仓库的更新通知

graphql-python/graphene-sqlalchemy

Fork: 225 Star: 971 (更新于 2024-05-06 06:02:27)

license: MIT

Language: Python .

Graphene SQLAlchemy integration

最后发布版本: v3.0.0rc1 ( 2023-12-05 05:43:04)

官方网址 GitHub网址

✨免费申请网站SSL证书,支持多域名和泛域名,点击查看

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 Graphene-SQLAlchemy

Build Status PyPI version GitHub release (latest by date including pre-releases) codecov

A SQLAlchemy integration for Graphene.

Installation

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

pip install --pre "graphene-sqlalchemy"

Examples

Here is a simple SQLAlchemy model:

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:

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:

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:

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:

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:

Contributing

See CONTRIBUTING.md

最近版本更新:(数据更新于 2024-05-07 17:10:53)

2023-12-05 05:43:04 v3.0.0rc1

2023-02-28 04:32:49 v3.0.0b4

2022-09-08 17:34:39 3.0.0b3

2022-07-15 17:35:05 3.0.0b2

2021-09-21 12:05:09 3.0.0b1

2020-06-05 05:59:46 2.3.0

2019-08-14 21:30:14 2.2.2

2019-06-18 22:48:28 2.2.1

2019-05-29 03:36:14 2.2.0

2019-04-18 05:14:20 2.1.2

主题(topics):

graphene, graphql, python, sqlalchemy

graphql-python/graphene-sqlalchemy同语言 Python最近更新仓库

2024-05-15 10:09:51 pipecat-ai/pipecat

2024-05-15 09:56:59 Sitoi/dailycheckin

2024-05-15 08:43:07 xtekky/gpt4free

2024-05-15 07:47:19 jxxghp/MoviePilot

2024-05-14 23:47:14 Ryujinx/release-channel-master

2024-05-14 22:18:38 linruowuyin/Fhoe-Rail