Sorting by field (Contentful + Gatsby)

Hi!

I am quite the noob. I am rebuilding an old “blog” (which I made with HTML and CSS only), because I now know vanilla JS and some React.

One of the content fields I created is called “date” (because I want to use the date the entries were posted in the original site). I want to sort them by reverse chronological order.

With this query I can get them in chronological order:

export const query = graphql`
    query {
        allContentfulBlogPost (sort: {fields: date}) {
            edges {
                node {
                    id
                    title
                    slug
                }
            }
        }
    }
`

I saw in the documentation that I can use a - sign to reverse the order, but I get errors no matter where I put it.

Case 1:
allContentfulBlogPost (sort: {fields: -date}) {

Result 1:
41:44 error Syntax Error: Invalid number, expected digit but got: "d" graphql/template-strings

Case 2:
allContentfulBlogPost (sort: -{fields: date}) {

Result 2:
41:35 error Syntax Error: Invalid number, expected digit but got: "{" graphql/template-strings

Case 3:
allContentfulBlogPost (sort: {fields: -(date)}) {

Result 3:
41:44 error Syntax Error: Invalid number, expected digit but got: "(" graphql/template-strings

…and so on. Can anyone help?

1 Like

Nevermind! Found the answer here.

I had to change it to the following:
allContentfulBlogPost (sort: {fields: [date], order: DESC}) {