Please read UPGRADE-v2.0.mdto learn how to upgrade to Graphene 2.0
.
A SQLAlchemy integration for Graphene.
For instaling graphene, just run this command in your shell
pip install "graphene-sqlalchemy>=2.0"
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)
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
inyour 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)
To learn more check out the following examples:
See CONTRIBUTING.md
SQL Alchemy SQL Alchemy是python中最著名的ORM(Object Relationship Mapping)框架。ORM:对象关系映射。即将用户定义的Python类与数据库表相关联,并将这些类(对象)的实例与其对应表中的行相关联。 Flask-SQLAlchemy Flask-SQLAlchemy 是一个为您的 Flask 应用增加 SQLAlchemy 支持的扩展。它需
Graphene 项目的目的是使用非常性感的类型安全的 API 进行 Ajax 测试,是 Selenium 项目的扩展。
Graphene-Django A Django integration for Graphene. �� Join the community on Slack Documentation Visit the documentation to get started! Quickstart For installing graphene, just run this command in you
Graphene 是 Python 的 GraphQL 框架,用于快速轻松构建 GraphQL schemas/types 。支持多种数据源,包括 SQL(Django、SQLAlchemy)、NoSQL、自定义 Python 对象等等。 示例代码: import grapheneclass Query(graphene.ObjectType): hello = graphene.Strin
Graphene-Django-Extras This package adds some extra functionalities to graphene-django to facilitate the graphql use without Relay: Allow pagination and filtering on Queries. Allow defining DjangoRest
问题内容: 我在Django中有一些REST API端点,我想对Graphene使用相同的身份验证。该文档不提供任何指导。 问题答案: 例如,如果在API视图中使用,则可以将端点添加到以这种方式装饰的GraphQLView中: urls.py: 请注意,我们添加了一个新的端点,并保留了GraphiQL工具使用的原始端点。 然后,您应该在GraphQL客户端中设置标头并指向端点。 更新:请参阅此Gi
我有两种型号: 模式: GraphQL的目标之一是性能。为此,GraphQL必须通过GraphQL请求(例如:GraphiQL)仅向数据库请求所请求的字段 如果我请求以下查询: 石墨烯-django库生成以下SQL: 它得到了模型的所有字段!与API Rest问题相同,不符合GraphQL指南。 如果我从模型中请求字段,我希望查询是: 外键如何解决这个问题?