聚合
|
这是 GraphQL Library 7 版本的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 5 LTS 版本。 |
类型定义
此页面的查询假设以下类型定义
type Post @node {
id: ID! @id
content: String!
creators: [User!]! @relationship(type: "HAS_POST", direction: IN, properties: "PostedAt")
createdAt: DateTime!
}
type User @node {
id: ID! @id
name: String!
age: Int!
posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT, properties: "PostedAt")
friends: [User!]! @relationship(type: "FRIENDS_WITH", direction: OUT)
}
type PostedAt @relationshipProperties {
date: DateTime
}
为其生成以下查询字段
type Query {
posts(where: PostWhere, sort: [PostSort!]!, limit: Int, offset: Int,): [Post!]!
postsConnection(after: String, first: Int, sort: [PostSort], where: PostWhere): PostsConnection!
users(where: UserWhere, sort: [UserSort!]!, limit: Int, offset: Int,): [User!]!
usersConnection(after: String, first: Int, sort: [UserSort], where: UserWhere): UsersConnection!
}
查询聚合
类型的聚合在 Connection 中的 aggregate 字段提供
query {
usersConnection {
aggregate {
node {
name {
longest
}
}
}
}
}
聚合字段
根据类型定义,这里列出 Neo4j GraphQL 支持的接受聚合的字段列表
| 类型 | 聚合函数 | 示例 |
|---|---|---|
字符串(例如 |
|
最长的用户名称
|
时间类型(例如 |
|
第一篇帖子日期
|
计数用户节点
query {
usersConnection {
aggregate {
count {
nodes
}
}
}
}
计数名称以 “J” 开头的用户节点
query {
usersConnection(where: { name: { startsWith: "J" } }) {
aggregate {
count {
nodes
}
}
}
}
聚合关联节点
您可以通过在连接中访问聚合字段来在查询中聚合关联节点。在这些字段中,您可以计数、聚合节点或边字段。
与聚合字段相同的选择和类型可用于关系聚合。
计数每个用户的所有帖子
query {
users {
id
postsConnection {
aggregate {
count {
nodes
}
}
}
}
}
通过使用 node 字段,可以聚合关联节点的属性
查找每个用户的最长帖子
query {
users {
name
postsConnection {
aggregate {
node {
content {
longest
}
}
}
}
}
}