我正在开发一个简单的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上下文吗?
答案 0 :(得分:1)
在石墨烯2.0版中,您的解决方案就像这样
def resolve_find_user(self, info, name):
query = Users.get_query(info)
return query.filter(UsersModel.name == name).first()
了解更多信息here