我需要您的帮助,以便按字段GraphQL按降序对createdAt数据进行排序。当按ID请求时,API不允许对给定ID的数组进行排序。如果删除排序键,则查询运行正常,但数据按升序排列(从旧到新),需要检索所有新值。
你知道如何对数组进行排序吗?提前谢谢你!
query GetCategory{
category(id: 4, sort: "createdAt:desc") {
data
{
id
attributes
{
name
reviews{
data
{
id
attributes
{
title
body
rating
createdAt
categories{
data
{
id
attributes{
name
}
}
}
}
}
}
}
}
}
}发布于 2022-01-15 20:16:54
我只需要添加排序,而不是在顶级,而是在字段级别,因为只有一个id (您不能排序:),但是仅仅因为有嵌套字段,就必须在子/字段级别(即reviews (sort: "createdAt:desc") )对它们进行排序。
完整的代码是:
const CATEGORY= gql`
query GetCategory{
category(id: 5) {
data
{
id
attributes
{
name
reviews (sort: "createdAt:desc") {. <== HERE
data
{
id
attributes
{
title
body
rating
createdAt
categories{
data
{
id
attributes{
name
}
}
}
}
}
}
}
}
}
}
`https://stackoverflow.com/questions/70722828
复制相似问题