项目作者: graphql-python

项目描述 :
Graphene SQLAlchemy integration
高级语言: Python
项目地址: git://github.com/graphql-python/graphene-sqlalchemy.git
创建时间: 2016-09-18T01:50:59Z
项目社区:https://github.com/graphql-python/graphene-sqlalchemy

开源协议:MIT License

下载


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" class="reference-link">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.

  1. pip install --pre "graphene-sqlalchemy"

Examples

Here is a simple SQLAlchemy model:

  1. from sqlalchemy import Column, Integer, String
  2. from sqlalchemy.ext.declarative import declarative_base
  3. Base = declarative_base()
  4. class UserModel(Base):
  5. __tablename__ = 'user'
  6. id = Column(Integer, primary_key=True)
  7. name = Column(String)
  8. last_name = Column(String)

To create a GraphQL schema for it, you simply have to write the following:

  1. import graphene
  2. from graphene_sqlalchemy import SQLAlchemyObjectType
  3. class User(SQLAlchemyObjectType):
  4. class Meta:
  5. model = UserModel
  6. # use `only_fields` to only expose specific fields ie "name"
  7. # only_fields = ("name",)
  8. # use `exclude_fields` to exclude specific fields ie "last_name"
  9. # exclude_fields = ("last_name",)
  10. class Query(graphene.ObjectType):
  11. users = graphene.List(User)
  12. def resolve_users(self, info):
  13. query = User.get_query(info) # SQLAlchemy query
  14. return query.all()
  15. schema = graphene.Schema(query=Query)

We need a database session first:

  1. from sqlalchemy import (create_engine)
  2. from sqlalchemy.orm import (scoped_session, sessionmaker)
  3. engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
  4. db_session = scoped_session(sessionmaker(autocommit=False,
  5. autoflush=False,
  6. bind=engine))
  7. # We will need this for querying, Graphene extracts the session from the base.
  8. # Alternatively it can be provided in the GraphQLResolveInfo.context dictionary under context["session"]
  9. Base.query = db_session.query_property()

Then you can simply query the schema:

  1. query = '''
  2. query {
  3. users {
  4. name,
  5. lastName
  6. }
  7. }
  8. '''
  9. result = schema.execute(query, context_value={'session': db_session})

You may also subclass SQLAlchemyObjectType by providing abstract = True in
your subclasses Meta:

  1. from graphene_sqlalchemy import SQLAlchemyObjectType
  2. class ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):
  3. class Meta:
  4. abstract = True
  5. @classmethod
  6. def get_node(cls, info, id):
  7. return cls.get_query(info).filter(
  8. and_(cls._meta.model.deleted_at==None,
  9. cls._meta.model.id==id)
  10. ).first()
  11. class User(ActiveSQLAlchemyObjectType):
  12. class Meta:
  13. model = UserModel
  14. class Query(graphene.ObjectType):
  15. users = graphene.List(User)
  16. def resolve_users(self, info):
  17. query = User.get_query(info) # SQLAlchemy query
  18. return query.all()
  19. schema = graphene.Schema(query=Query)

Full Examples

To learn more check out the following examples:

Contributing

See CONTRIBUTING.md