GraphQL 中聚合函数的支持 [英] Support of aggregate function in GraphQL

查看:44
本文介绍了GraphQL 中聚合函数的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 GraphQL 的分析解决方案非常感兴趣(想想一个显示图表的 web 应用程序).但是我找不到任何使用聚合函数的 GraphQL 示例.这是我的前端完成的大多数查询的主要方面.

I'm am very interested by GraphQL for an analytic solution (think of an webapp displaying graphs). But I cannot find any examples of GraphQL using aggregate function. This is a main aspect of most of the queries done by my frontend.

对于我的解决方案,我们有 3 个典型的后端调用.

For my solution, we have 3 typical backend calls.

  1. 搜索
  2. 聚合
  3. 时间序列

假设我们在 GraphQL 中指定了这种类型

Let say we have this type specified in GraphQL

type Person {
  name: String
  age: Int
  create_time: Date
}

  1. 搜索

这似乎被 GraphQL 处理得很好.这里没有问题.

This seems to be well handled by GraphQL. No question here.

例如.名为 Bob 的人的搜索年龄{人(姓名:鲍勃"){年龄}}

ex. Search age of Person named Bob { Person(name: "Bob") { age } }

  1. 聚合

这是我想在饼图中显示信息的典型情况.所以假设我想按年龄计算人数.

This is the typical case where I want to display the info in a Pie Chart. So let say I want to count the number of person by age.

这里是 PostgreSQL 查询:

Here would be the PostgreSQL query:

SELECT age, count(*) from Ticket group by age;

GraphQL 中的等价物是什么?

What would be the equivalent in GraphQL?

  1. 时间序列这是我想在条形图中以 X 轴为时间显示信息的典型情况.

例如.假设我想计算每小时创建的用户数.

ex. Let say I want to count the number of created user per hour.

这里是 PostgreSQL 查询:

Here would be the PostgreSQL query:

SELECT date_trunc('hour', create_time) as create_time_bin, count(*) from Person group by create_time_bin order by create_time_bin ASC;

GraphQL 等效查询是什么?

What would be the GraphQL equivalent query?

推荐答案

GraphQL,归根结底,会以您定义的类型进行响应.您只需要将该数据放入一个类型中.无论这是针对这些不同查询的特定类型,还是针对现有类型的数据的字段,这取决于您,但归结起来仅此而已.GraphQL 在定义类型和所有查询将返回的内容方面确实需要更多的努力,这使得它更加僵化,但其想法是在另一方面存在一些很酷的功能,例如内省和类型检查.如果将这种临时"数据结构放入 GraphQL 类型似乎没有逻辑意义,那么如果您需要其他数据源,拥有非 GraphQL 端点并不违法.

GraphQL, at the end of the day, responds with your defined types. You just need to put that data into a type. Whether this is a specific type for these different queries, or fields for that data on existing types, it's up to you but that's all it boils down to. GraphQL does require more effort up front in terms of defining your types and what all queries will return, which makes it more rigid, but the idea is that on other side of that lies some cool features, like introspection and type checking. If it doesn't seem to make logical sense to put that sort of "ad hoc" data structures into a GraphQL type, then it's not illegal to have non-GraphQL endpoints if you need other data sources.

这篇关于GraphQL 中聚合函数的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆