【问题标题】:Use GraphQL to show statistics使用 GraphQL 显示统计信息
【发布时间】: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


    【解决方案1】:

    在工作中,我们还开始将石墨烯用于分析部分。 虽然我们使用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 对象类型的元模型。如果您只想拥有一个包含 activeinactivesupscended 员工的统计字段,您也可以创建一个简单的 ObjectType 而不需要基本模型 - 但也许我只是理解错了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2021-05-28
      • 1970-01-01
      • 2011-05-08
      • 2012-04-25
      • 1970-01-01
      • 2010-10-23
      相关资源
      最近更新 更多