【发布时间】:2023-03-25 21:38:01
【问题描述】:
我对 GraphQL 和 Graphene 还很陌生,找不到任何可以帮助我解决问题的东西,所以现在就在这里提问。
基本上,我想要显示的是仅针对给定列表的活动、非活动和暂停用户的数量。我在做查询时的想法是这样的:
query{
viewer{
statistics(listId:5) {
active
inactive
suspended
}
}
}
并收到这样的输出:
{
"data": {
"viewer": {
"statistics": {
"active": 11,
"inactive": 12,
"suspended": 13
}
}
}
这是我目前拥有的(我正在使用 Python):
class Statistic(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
active= graphene.Int()
inactive= graphene.Int()
suspended= graphene.Int()
@staticmethod
def resolve_active(self, args, context, info):
return 11
@staticmethod
def resolve_inactive(self, args, context, info):
return 12
@staticmethod
def resolve_suspended(self, args, context, info):
return 13
class Viewer(graphene.ObjectType):
node = relay.Node.Field()
statistics = graphene.List(Statistic, list_id=graphene.Int())
def resolve_active_employees(self, args, context, info):
list_id = args.get('list_id')
if (employeelist is None):
raise GraphQLError("Missing argument: list_id (Employee List ID)")
return db_session.query(EmployeeModel) \
.join(EmployeeListModel,
EmployeeModel.employeelist_id == EmployeeListModel.id ) \
.filter(EmployeeModel.terminated == 0) \
.filter(EmployeeListModel.id == employeelist)
所以我肯定没有得到我想要的,而是收到了所有活跃(或未终止)用户的记录。我不知道该用什么,所以这就是我现在得到的所有东西。
有人可以为我指明如何实现目标输出的正确方向(不是真的希望得到硬编码的答案,如果可能的话,结果最好来自数据库查询)?
我正在使用:
graphene==1.4.1
graphene-sqlalchemy==1.1.1
graphql-core==1.1
graphql-relay==0.4.5
【问题讨论】:
标签: python-3.x graphql graphene-python