2

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

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

我经历了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)is a<class 'graphql.execution.base.ResolveInfo'>type(info.context)is <class 'werkzeug.local.LocalProxy'>。应该context是 SQLAlchemy 上下文吗?

4

1 回答 1

1

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

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

在此处了解更多信息

于 2018-01-30T17:54:03.950 回答