Sorting a Storyblok GraphQL Query

Viewed 533

I have the following query that queries my storyblok CMS:

{
  allStoryblokEntry(filter: {field_component: {eq: "blog"}} ) {
    edges {
      node {
        id
        name
        content
        published_at
      }
    }
  }
}

I'd like to sort my nodes however, say by published_at date. I don't know how to do this though. GraphiQL suggests a sort parameter, which I somehow cannot get to work though. Storyblok tells me to use sort_by=name:asc - which my GraphiQL doesn't recognise at all, and which also gives me a syntax error. Has anyone done this before?

1 Answers

You're almost there. Try this:

{
  allStoryblokEntry(
    filter: {field_component: {eq: "blog"}}
    sort: { 
      fields: name, 
      order: ASC}){
    edges {
      node {
        id
        name
        content
        published_at
      }
    }
  }
}

To sort elements in GraphQL you need to provide a valid field (don't need even to query it) and a sort order (ASC/DESC).

You can check for further information in Gatsby's GraphQL reference.

Related