graphql-python/graphene-sqlalchemy
Fork: 226 Star: 980 (更新于 2024-11-01 16:26:45)
license: MIT
Language: Python .
Graphene SQLAlchemy integration
最后发布版本: v3.0.0rc1 ( 2023-12-05 05:43:04)
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-SQLAlchemy
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-09-13 22:01:07)
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-11-06 03:34:16 home-assistant/core
2024-11-05 16:16:26 Guovin/TV
2024-11-05 15:03:24 Cinnamon/kotaemon
2024-11-04 23:11:11 DS4SD/docling
2024-11-04 10:56:18 open-compass/opencompass
2024-11-04 08:51:21 yt-dlp/yt-dlp