2

I'm trying to sort the content of my YAML file by date with GraphQL. I'm publishing the notifications through Netlify CMS and using React(Gatsby) as a front end. The notifications are being written to the notifications.yml file but I can't get them sorted by date; latest first. I'm rendering the contents out fine to the site but I can't find a way to sort them by date.

I have tried using "sort: { field: date, order: DESC }" in the GraphQL query but I get the error:

Unknown argument "sort" on field "file" of type "Query". Did you mean "root"? graphql/template-strings

Here's my 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:

query={graphql`
  query NotificationQuery {
    notifications: file(
      relativePath: { regex: "/notifications/" }
    ) {
      childDataYaml {
        notifications {
          date(formatString: "MMMM D, YYYY")
          message
          title
        }
      }
    }
  }
}

Could you guys please help me out?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SolidCake
  • 21
  • 2
  • see [this answer](https://stackoverflow.com/questions/54473828/how-to-query-date-range-in-gatsby-graphql/54495972#54495972) or [this answer](https://stackoverflow.com/questions/54782808/filtering-by-dates-in-gatsby-and-contentful). Looking at the error though you might have applied filter wrongly, would you update your post with the graphql query with filter? – Derek Nguyen Mar 22 '19 at 05:20

0 Answers0