Please read UPGRADE-v2.0.md
to learn how to upgrade to Graphene 2.0
.
Graphene-SQLAlchemy
A SQLAlchemy integration for Graphene.
Installation
For instaling graphene, just run this command in your shell
pip install "graphene-sqlalchemy>=2.0"
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
# only return specified fields
only_fields = ("name",)
# exclude specified fields
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)
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
Commit History @71489ab8-145a-45fb-bf7a-176c2be3c66c/main
- Update standards version to 4.6.1, no changes needed. Kali Janitor 1 year, 8 months ago
- Prepare for Release Sophie Brun 2 years ago
- Remove test file from installation Sophie Brun 2 years ago
- Bump Standards-Version to 4.6.0 Sophie Brun 2 years ago
- Do not use obsolete the package python3-singledispatch Sophie Brun 2 years ago
- Remove double spaces from Description Ben Wilson 2 years ago
- Update email address Ben Wilson 2 years ago
- Update standards version to 4.5.0, no changes needed. Kali Janitor 3 years ago
- Set upstream metadata fields: Bug-Database, Bug-Submit, Repository, Repository-Browse. Kali Janitor 3 years ago
- Update URL in GitLab's CI configuration file Raphaël Hertzog 4 years ago
- Configure git-buildpackage for Kali Raphaël Hertzog 4 years ago
- Add GitLab's CI configuration file Raphaël Hertzog 4 years ago
- Update debian/changelog Sophie Brun 5 years ago
- Wrap-and-sort Sophie Brun 5 years ago
- Update build-deps and deps Sophie Brun 5 years ago
- Bump Standards-Version to 4.3.0 Sophie Brun 5 years ago
- Remove debian/README.Debian Sophie Brun 5 years ago
- Add Uploaders Sophie Brun 5 years ago
- Use debhelper-compat 12 Sophie Brun 5 years ago
- Update debian/copyright Sophie Brun 5 years ago
- Update upstream source from tag 'upstream/2.1.2' Sophie Brun 5 years ago
- New upstream version 2.1.2 Sophie Brun 5 years ago
- Update Vcs-* fields for the move to gitlab.com Raphaël Hertzog 5 years ago
- Update Maintainer field Raphaël Hertzog 5 years ago
- Import new upstream release Sophie Brun 6 years ago
- New upstream version 2.0.0 Sophie Brun 6 years ago
- Update upstream source from tag 'upstream/2.0.0' Sophie Brun 6 years ago
- Update debian/watch Sophie Brun 6 years ago
- Rebuild with new version of python-sqlalchemy Sophie Brun 6 years ago
- Initial release Sophie Brun 7 years ago