带有Flask的石墨烯不会加载模式

时间:2017-07-24 02:12:58

标签: python flask graphql graphene-python

我试图运行this example将石墨烯与Flask一起使用。我从该页面逐字models.pyschema.pyapp.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,我确信它最终会成为一个愚蠢的错误。任何帮助将不胜感激!

非常感谢, 亚历

2 个答案:

答案 0 :(得分:1)

您需要为query对象提供至少一个schema对象,否则没有任何内容可以&#34;暴露&#34;通过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)

答案 1 :(得分:0)

谢谢伊万!您的回复让我走上正轨,我将其标记为正确的答案,但解决方案更简单。

本教程中的

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)

然后该示例似乎按预期工作。我认为这是教程中的错误,应该在Graphene网站上进行更改。