我正在开发一个简单的GraphQL Flask API,并实现了一个简单的find user解析器。这是我的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
并相应地进行了更改。解析器现在看起来像这样。在
现在,关键字参数消失了,但我有了一个新问题。错误是"message": "'Request' object has no attribute 'context'",
。现在我很困惑,因为我认为info.context
应该有效,但显然不是这样。有什么办法解决这个问题吗?这是我第一次在GraphQL上工作,任何帮助都非常感谢!在
编辑:看起来type(info)
是<class 'graphql.execution.base.ResolveInfo'>
,而{context
应该是SQLAlchemy上下文吗?在
在graphene的2.0版本中,您的解决方案如下所示
有关详细信息here
相关问题 更多 >
编程相关推荐