Graphene GraphQL:“Request”对象没有属性“context”

2024-09-26 23:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个简单的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)

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

我浏览了UPGRADE-v2.0.md并相应地进行了更改。解析器现在看起来像这样。在

^{pr2}$

现在,关键字参数消失了,但我有了一个新问题。错误是"message": "'Request' object has no attribute 'context'",。现在我很困惑,因为我认为info.context应该有效,但显然不是这样。有什么办法解决这个问题吗?这是我第一次在GraphQL上工作,任何帮助都非常感谢!在

编辑:看起来type(info)<class 'graphql.execution.base.ResolveInfo'>,而{}是{}。context应该是SQLAlchemy上下文吗?在


Tags: namefromimportnode解析器ascontextfind

热门问题