Skip to main content

Aggregates

PostGraphile’s engine has support for powerful aggregates. The @graphile/pg-aggregates module adds various aggregates to the schema, and gives you the ability to add more via plugins. Aggregates are located under connection fields.

PostGraphile 引擎支持强大的聚合查询。@graphile/pg-aggregates 模块会在 schema 中添加多种聚合字段,并支持你通过插件自行扩展更多聚合操作。这些聚合字段均位于 Connection 字段下。

Aggregates ignore pagination info聚合查询会忽略分页信息​

Aggregates are performed over the entire collection represented by the field and its filters — not just the data that would be returned if you were to query the nodes. This means they ignore the first, last, before, after and offset arguments. This is deliberate (if you only need aggregates over the data that matches your pagination information then you could calculate these on the client).

聚合计算作用于当前字段及其过滤条件所代表的整个集合,而不仅仅是查询 nodes 时实际返回的那部分数据。也就是说,聚合会忽略 first、last、before、after 和 offset 参数。这是有意为之的设计(如果你只希望针对当前分页范围内的数据做统计,直接在客户端计算即可)。

Aggregates only work on Relay connection聚合查询仅适用于 Relay Connection​

Thanks to their expansibility, relay connections were the perfect place to add aggregates support. If you’re using a behavior configuration that prefers lists over connections (e.g. -connection +list) then you can override it on a per-collection basis with the @behavior +connection smart tag.

Relay Connection 具备良好的扩展性,天然适合用来承载聚合功能。如果你的 behavior 配置偏向使用列表而非 Connection(例如 -connection +list),也可以通过 @behavior +connection smart tag 按集合粒度单独覆盖该设置。