新手问题 Graphql Resolver 里如何获取查询的字段列表?

lidashuang · 2020年02月22日 · 最后由 lidashuang 回复于 2020年02月23日 · 2458 次阅读

例如下面的 query

statistics {
    orderSettlementPm
    orderSettlementShop
    todayPaidAmount
    todayPaidOrderCount
  }
}

目前 Resolvers 是这样的, 这样会导致不管请求什么字段, 都会计算一遍数据? 问: Resolvers 可不可以获取查询的字段列表?

class Resolvers::SysStatisticsData < Resolvers::Base
  def resolve
    data = {
      paid_order_count: ShopOrderStatistic.sum(:paid_order_count),
      paid_amount: ShopOrderStatistic.sum(:paid_amount),

      today_paid_amount: ShopOrderStatistic.today_paid_amount,
      today_paid_order_count: ShopOrderStatistic.today_paid_order_count,

      yesterday_paid_order_count: ShopOrderStatistic.yesterday_paid_order_count,
      yesterday_paid_amount: ShopOrderStatistic.yesterday_paid_amount,

      today_refund_count: Refund.today_data.success_status.count,
      yesterday_refund_count: Refund.yesterday_data.success_status.count,

      refund_amount: Refund.refund_amount_sum,
      withdraw_amount: WithdrawLog.withdraw_amount_sum,
      wait_withdraw_amount: WithdrawLog.processing_paid.sum(:amount),

      view_count: 100,
      yesterday_view_count: 100,

      wait_ship_order_count: Order.paid_status.count
    }
  end
end

应该可以用 proc 吧

hooopo 回复

单独定义了一个类, call 哪个 method 丢给 graphql 处理了

lidashuang 关闭了讨论。 02月23日 01:25
lidashuang 关闭了讨论。 02月23日 01:25
lidashuang 重新开启了讨论。 02月23日 01:39
7 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号