使用GraphQL按日期对YAML文件内容进行排序?

时间:2019-03-21 14:52:45

标签: reactjs yaml graphql gatsby netlify-cms

我正在尝试使用GraphQL按日期对YAML文件的内容进行排序。我正在通过Netlify CMS并使用React(Gatsby)作为前端发布通知。通知被写入notifications.yml文件,但是我无法按日期对它们进行排序;最新的。我正在将内容呈现给网站,但找不到按日期对它们进行排序的方法。

我尝试在GraphQL查询中使用“ sort:{字段:日期,顺序:DESC}”,但出现错误:

  

类型为“查询”的字段“文件”上的未知参数“排序”。你是说“根”吗? graphql / template-strings

这是我的notifications.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
        }
      }
    }
  }
}

你们能帮我吗?

0 个答案:

没有答案