python-3.x - 使用 GraphQL 显示统计信息
问题描述
我对 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
解决方案
在工作中,我们还开始将石墨烯用于分析部分。虽然我们使用graphene_django
并且我还没有使用 SQLAlchemy,但我希望我可以为您提供一些解决问题的想法:
我不确定我是否正确理解了您的代码,但您可能想要类似的东西
class Viewer(graphene.ObjectType):
node = relay.Node.Field()
statistics = graphene.List(Statistic, list_id=graphene.Int())
# Use `resolve_statistics' to define how you get the data for the
# statistics list
def resolve_statistics(self, args, context, info):
list_id = args.get('list_id')
# Some query which filters your model for the given ID and annotates or
# aggregates the stats
return <you query which filters for list id and annotates stats>
例如,您的查询可能会返回一个字典,例如
{'active': 11, 'inactive': 12, 'suspended':13}
.
如果选择这种方式,则需要调整Statistics
对象类型以从字典中提取字段值:
class Statistic(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
active= graphene.Int()
inactive= graphene.Int()
suspended= graphene.Int()
def resolve_active(self, args, context, info):
return self.get('active')
def resolve_inactive(self, args, context, info):
return self.get('inactive')
def resolve_suspended(self, args, context, info):
return self.get('suspended')
我不确定这是否是您想要的,但也许它会给您一些想法。例如,我们所做的是使用django_filter
可以更轻松地进行过滤的包。不幸的是,我不知道你是否可以在 SQLAlchemy 中做类似的事情。
我也不确定您为什么将EmployeeModel
用作Statistics
对象类型的元模型。如果您只想拥有一个包含active
,inactive
和supscended
employees 的统计数据字段,您也可以创建一个ObjectType
不需要基本模型的简单模型 - 但也许我只是理解错误。
推荐阅读
- c++ - 在 C++ 程序 csv 文件中添加了额外的行?
- python - 如何查看谁创建了记录?
- node.js - 如何修复内部服务器错误?- Google Cloud
- python - Python:读取溢出的文件
- java - Gradle 无法在 java 和 groovy 中运行测试
- r - 在 R ggplot 中,`..density..` 的用途是什么?
- excel - Excel 宏未显示为加载项
- javascript - Javascript推送功能
- sql-server - 如何管理数据库中的多种货币
- python - 将列表添加到 Pandas Dateframe,但将 NaN 保留在顶部