我正在尝试使用 GraphQL 按日期对 YAML 文件的内容进行排序。我通过 Netlify CMS 发布通知并使用 React(Gatsby) 作为前端。通知正在写入 notification.yml 文件,但我无法按日期对它们进行排序;最新的第一。我正在将内容很好地呈现给网站,但我找不到按日期对它们进行排序的方法。
我尝试在 GraphQL 查询中使用“sort: { field: date, order: DESC }”,但出现错误:
“查询”类型的字段“文件”上的未知参数“排序”。您指的是 “root” 吗? graphql/模板字符串
这是我的通知.yml:
notifications:
- date: '2019-03-16'
message: 'First notification'
title: 'One'
- date: '2019-03-17'
message: 'Second notification'
title: 'Two'
- date: 2019-03-20'
message: 'Third notification'
title: 'Three'
GraphQL 查询:
query={graphql`
query NotificationQuery {
notifications: file(
relativePath: { regex: "/notifications/" }
) {
childDataYaml {
notifications {
date(formatString: "MMMM D, YYYY")
message
title
}
}
}
}
}
你们能帮帮我吗?