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

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

问题描述

我对GraphQL感兴趣的是一种解析解决方案(想像一个显示图的webapp).但是我找不到使用聚合函数的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轴为时间的BarChart中显示信息.

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

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天全站免登陆