聚合

这是 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 支持的接受聚合的字段列表

类型 聚合函数 示例

字符串(例如 IDString

shortestlongest

最长的用户名称
query {
    usersConnection {
        aggregate {
            name {
                longest
            }
        }
    }
}

时间类型(例如 DateTimeTimeLocalTimeLocalDateTimeDuration

minmax

第一篇帖子日期
query {
    postsConnection {
        aggregate {
            node {
                createdAt {
                    min
                }
            }
        }
    }
}
计数用户节点
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
                    }
                }
            }
        }
    }
}

聚合关系

也可以通过使用 edge 字段来聚合关系属性

计数帖子与用户之间的边
query {
    users {
        id
        postsConnection {
            aggregate {
                count {
                    edges
                }
            }
        }
    }
}
查询在某日期之前发布的用户节点
query {
    users {
        name
        postsConnection {
            aggregate {
                edge {
                    date {
                        max
                    }
                }
            }
        }
    }
}

聚合与分页

由于聚合在 Connection 类型内部完成,可选参数 firstafter 可用于分页。然而,分页不适用于聚合。

例如:

query {
    usersConnection(first: 10) {
        aggregate {
            count {
                nodes
            }
        }
    }
}

结果返回所有节点的计数

{
    "usersConnection": {
        "aggregate": {
            "count": {
                "nodes": 38
            }
        }
    }
}