GraphiQL sort causes error "Cannot read property 'map' of undefined"

Viewed 343

I'm pulling in data from Strapi, a CMS, using a query in GraphiQL. Everything works fine as below:

query MyQuery {
  allStrapiPortfolioPages {
    nodes {
      order
    }
  }
}

with the response:

{
  "data": {
    "allStrapiPortfolioPages": {
      "nodes": [
        {
          "order": 1
        },
        {
          "order": 2
        },
        {
          "order": 3
        }
      ]
    }
  },
  "extensions": {}
}

I try to sort the data as below:

query MyQuery {
  allStrapiPortfolioPages(sort: {order: ASC}) {
    nodes {
      order
    }
  }
}

This leads to an error with the message: "Cannot read property 'map' of undefined".

How do I sort these nodes in my query?

1 Answers

Your query should look like:

query MyQuery {
  allStrapiPortfolioPages(
    sort: {
      fields: [order]
      order: ASC
    }) {
    nodes {
      order
    }
  }
}

Note that you are missing the field (order). In your case, it's a bit tricky because the name itself is the way to order a query in GraphQL but in general, you have to provide a field and order to sort.

For further reference check the Gatsby GraphQL reference.

Related