带有 Flask 的石墨烯不加载模式
Posted
技术标签:
【中文标题】带有 Flask 的石墨烯不加载模式【英文标题】:Graphene with Flask doesn't load schema 【发布时间】:2017-12-29 12:19:46 【问题描述】:我正在尝试运行 this example 以将 Graphene 与 Flask 一起使用。我从该页面逐字制作models.py
、schema.py
和app.py
,并按照指示将它们全部放入flask_sqlalchemy
文件夹中。然后我使用底部列出的说明创建并填充数据库。
在我尝试运行 app.py
之前一切正常。当我这样做时,它给了我这个错误:
Traceback (most recent call last):
File "./app.py", line 6, in <module>
from schema import schema, Department
File "C:\Users\asdf\Python\GraphQL\flask_sqlalchemy\schema.py", line 7, in <module>
schema = graphene.Schema()
File "C:\Users\asdf\Envs\GraphQL\lib\site-packages\graphene\types\schema.py", line 27, in __init__
).format(query)
AssertionError: Schema query must be Object Type but got: None.
所以基本上它在schema.py
中失败了,上面写着:
schema = graphene.Schema()
确实,如果我打开命令提示符并执行此操作,它会以同样的方式失败:
>>> import graphene
>>> s = graphene.Schema()
Traceback... (same traceback)
我在 Windows 10 上使用 Python 3.5。与本教程的唯一区别是我使用 virtualenvwrapper-win 而不是常规的 virtualenv。
这是我对石墨烯或 graphql 的第一次体验,我确信这最终会成为一个愚蠢的错误。任何帮助将不胜感激!
非常感谢, 亚历克斯
【问题讨论】:
【参考方案1】:您需要为您的schema
对象提供至少一个query
对象,否则无法通过 GraphQL “公开”任何内容。
class Employee(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
class Query(graphene.ObjectType):
employee = graphene.Field(
Employee,
employee_id=graphene.Argument(graphene.Integer)
)
def resolve_employee(self, args, context, info):
"""Resolves `employee` object on the root query"""
employee_id = args.get('employee_id')
employee = EmployeeModel.query.get(employee_id)
return employee
# Provide the root query to schema
schema = graphene.Schema(query=query)
【讨论】:
【参考方案2】:谢谢伊万!您的回答让我走上了正确的道路,我会将其标记为正确答案,但解决方案更加简单。
教程中的schema.py
是这样的:
# flask_sqlalchemy/schema.py
import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models import db_session, Department as DepartmentModel, Employee as EmployeeModel
schema = graphene.Schema()
class Department(SQLAlchemyObjectType):
class Meta:
model = DepartmentModel
interfaces = (relay.Node, )
class Employee(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_employees = SQLAlchemyConnectionField(Employee)
schema.query = Query
但正如 Ivan 所指出的,graphene.Schema()
函数需要处理一个查询,所以它在schema = graphene.Schema()
处失败。简单的解决方案是将该行移至末尾并将 Query 类交给它,因此将文件更改为:
# flask_sqlalchemy/schema.py
import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models import db_session, Department as DepartmentModel, Employee as EmployeeModel
class Department(SQLAlchemyObjectType):
class Meta:
model = DepartmentModel
interfaces = (relay.Node, )
class Employee(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_employees = SQLAlchemyConnectionField(Employee)
schema = graphene.Schema(query = Query)
然后该示例似乎按预期工作。我认为这是教程中的错误,应该在石墨烯网站上进行更改。
【讨论】:
以上是关于带有 Flask 的石墨烯不加载模式的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flask 中重新加载有错误的 fancybox iframe 表单