Graphene SQLAlchemy integration
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for graphene-sqlalchemy-3.0.0rc1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f30ab56ec097cde6771f215ab550335248a60823c886fbc64ec358332c08cefb |
|
MD5 | e08b1ce489129005f939e18267680a66 |
|
BLAKE2b-256 | eee87f7fe7b7d1d0c934433cb42d4668e89e9c1344435d03ac3b6950fc414833 |
Hashes for graphene_sqlalchemy-3.0.0rc1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b32e1386f7677c5dbd86d4340ca8601f29e209a5b1c59bf93c6dfbd79cd4684 |
|
MD5 | f5a5c3005bb22eddccf95901f7ed3654 |
|
BLAKE2b-256 | d0935b34035f8da7f160e9a14ec819bf23dd9ba63475e5b9a2941d4c79cdded6 |