排序
|
这是 GraphQL Library 7 版本的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 5 LTS 版本。 |
对于在类型定义中定义的每个对象类型,都会生成一个排序输入类型。这使得查询结果可以按每个单独的字段进行排序。
使用以下示例类型定义
type Movie @node {
title: String!
runtime: Int!
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
}
type Actor @node {
surname: String!
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
}
为类型 Movie 生成以下排序输入类型和查询
type Movie @node {
title: String!
runtime: Int!
}
enum SortDirection {
ASC
DESC
}
input MovieSort {
title: SortDirection
runtime: SortDirection
}
type Query {
movies(where: MovieWhere, limit: Int, offset: Int, sort: [MovieSort!]): [Movie!]!
}
以下查询按运行时间升序获取所有电影
query {
movies(sort: [{ runtime: ASC }]) {
title
runtime
}
}
如果 Movie 与 Actor 类型之间存在关系,在获取 actors 字段时也可以进行排序
query {
movies {
title
runtime
actors(sort: [{ surname: ASC }]) {
surname
}
}
}
|
使用连接 API 时,仅能基于关系属性对查询结果进行排序。 |