排序

在您的类型定义中定义的每个对象类型都会生成一个排序输入类型。 它允许对查询结果进行排序,方法是对每个单独的字段进行排序。

使用此示例类型定义

type Movie {
    title: String!
    runtime: Int!
}

将生成以下排序输入类型和查询

type Movie {
    title: String!
    runtime: Int!
}

enum SortDirection {
    ASC
    DESC
}

input MovieSort {
    title: SortDirection
    runtime: SortDirection
}

input MovieOptions {
    """Specify one or more MovieSort objects to sort Movies by. The sorts will be applied in the order in which they are arranged in the array."""
    sort: [MovieSort!]
    limit: Int
    offset: Int
}

type Query {
    movies(where: MovieWhere, options: MovieOptions): [Movie!]!
}

以下查询按运行时间的升序获取所有电影

query {
    movies(options: {
        sort: [
            {
                runtime: ASC
            }
        ]
    }) {
        title
        runtime
    }
}

如果 MovieActor 类型之间存在关系,您也可以在获取 actors 字段时进行排序

query {
    movies {
        title
        runtime
        actors(options: {
            sort: [
                {
                    surname: ASC
                }
            ]
        }) {
            surname
        }
    }
}

您只能在使用连接 API 时根据关系属性对查询结果进行排序。