简体   繁体   English

带有Flask的石墨烯不会加载架构

[英]Graphene with Flask doesn't load schema

I'm trying to run this example for using Graphene with Flask. 我正在尝试运行此示例以将Graphene与Flask一起使用。 I make the models.py , schema.py , and app.py verbatim from that page, and put them all in the flask_sqlalchemy folder, as instructed. 我从该页面逐字制作了models.pyschema.pyapp.py ,并按照说明将它们全部放入flask_sqlalchemy文件夹中。 Then I make and populate the database with the instructions listed at the bottom. 然后,使用底部列出的说明制作并填充数据库。

Everything was going ok until I tried to run app.py . 一切正常,直到我尝试运行app.py When I did that it gave me this error: 当我这样做时,它给了我这个错误:

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.

So basically it failed on the line in schema.py that says: 因此,基本上,它在schema.py的代码行中失败了:

schema = graphene.Schema()

And indeed, if I open a command prompt and do this it fails the same way: 实际上,如果我打开命令提示符并执行此操作,则失败的方式相同:

>>> import graphene
>>> s  = graphene.Schema()
Traceback... (same traceback)

I'm using Python 3.5 on Windows 10. The only difference from the tutorial is that I use virtualenvwrapper-win instead of the regular virtualenv. 我在Windows 10上使用Python 3.5。与本教程唯一的不同是,我使用virtualenvwrapper-win而不是常规的virtualenv。

This is my first experience with graphene or graphql in general, and I'm sure it ends up being a stupid mistake. 一般来说,这是我第一次接触石墨烯或graphql,我相信它最终会成为一个愚蠢的错误。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks a lot, Alex 非常感谢Alex

You need to provide at least a query object to your schema object, else there is nothing to "expose" through GraphQL. 您需要至少为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)

Thank you Ivan! 谢谢伊凡! Your response put me on the right track and I'd mark that as the correct answer, but the solution was even simpler. 您的回答使我走上了正确的道路,我将其标记为正确的答案,但是解决方案甚至更简单。

schema.py in the tutorial is this: 本教程中的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

But as Ivan pointed out, the graphene.Schema() function needs to be handed a query, so it fails at schema = graphene.Schema() . 但是正如Ivan指出的那样, graphene.Schema()函数需要进行查询,因此它在schema = graphene.Schema()时失败。 The easy solution is to move that line to the end and hand it the Query class, so change the file to: 一种简单的解决方案是将该行移到末尾并将其交给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)

And then the example appears to work as intended. 然后该示例似乎按预期工作。 I think this is an error in the tutorial and should be changed on the Graphene website. 我认为这是本教程中的错误,应在Graphene网站上进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM