石墨烯 - GraphQL:'Request'对象没有属性'context'

时间:2018-01-27 17:51:04

标签: python sqlalchemy graphql graphene-python

我正在开发一个简单的GraphQL Flask API,我正在实现一个简单的查找用户解析器。这是我的schema.py

import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models import db_session, Users as UsersModel, Channel as ChannelModel, Video as VideoModel

class Users(SQLAlchemyObjectType):
    class Meta:
        model = UsersModel
        interfaces = (relay.Node, )

class Channel(SQLAlchemyObjectType):
    class Meta:
        model = ChannelModel
        interfaces = (relay.Node, )

class Query(graphene.ObjectType):
    node = relay.Node.Field()
    users = SQLAlchemyConnectionField(Users)
    find_user = graphene.Field(Users, name = graphene.String())
    all_user = SQLAlchemyConnectionField(Users)

    def resolve_find_user(self, args, context, info):
        query = Users.get_query(context)
        name = args.get('name')
        return query.get(name)

我正在使用graphene 2.0,代码返回"message": "resolve_find_user() got an unexpected keyword argument 'name'",错误。经过一些谷歌搜索,看起来graphene中的major change改变了方式解析器工作。

我浏览了UPGRADE-v2.0.md并相应地更改了内容。旋转变压器现在看起来像这样。

# def resolve_find_user(self, info, name):
#     query = Users.get_query(info.context)
#     return query.get(name)

现在,关键字参数消失但我遇到了新问题。错误是"message": "'Request' object has no attribute 'context'",。现在我很困惑,因为我认为info.context应该有效,但显然不是。有关如何解决此上下文问题的任何想法?这是我第一次使用GraphQL,非常感谢任何帮助!

修改:看起来type(info)<class 'graphql.execution.base.ResolveInfo'>type(info.context)<class 'werkzeug.local.LocalProxy'>context应该是SQLAlchemy上下文吗?

1 个答案:

答案 0 :(得分:1)

在石墨烯2.0版中,您的解决方案就像这样

def resolve_find_user(self, info, name):
  query = Users.get_query(info)
  return query.filter(UsersModel.name == name).first()

了解更多信息here