聚合
类型定义
此页面上的查询假设以下类型定义
type Post {
id: ID! @id
content: String!
creator: User! @relationship(type: "HAS_POST", direction: IN, properties: "PostedAt")
createdAt: DateTime!
}
type User {
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, options: PostOptions): [Post!]!
postsAggregate(where: PostWhere): PostAggregationSelection!
users(where: UserWhere, options: UserOptions): [User!]!
usersAggregate(where: UserWhere): UserAggregationSelection!
}
聚合字段
根据类型定义,以下是可以接受 Neo4j GraphQL 支持的聚合的字段列表
类型 | 聚合函数 | 示例 |
---|---|---|
字符串(例如 |
|
最长的用户名
|
数字(例如 |
|
示例查询
|
时间(例如 |
|
第一个帖子日期
|
参数 |
聚合相关节点
相关节点也可以在查询中聚合,方法是在节点中访问聚合字段。在这些字段中,您可以计数、聚合节点或边字段。
之前相同的选择和类型在关系聚合中可用。
计数用户节点
query {
usersAggregate {
count
}
}
计数名称以“J”开头的用户节点
query {
usersAggregate(where: { name_STARTS_WITH: "J" }) {
count
}
}
计算每个用户的全部帖子
query {
users {
id
postsAggregate {
count
}
}
}
通过使用 node
字段,可以聚合相关节点的属性
查找每个用户的最长帖子
query {
users {
name
postsAggregate {
node {
content {
longest
}
}
}
}
}