GraphQL - sort error - Unknown argument "sort"

Viewed 392

I need your help in order to sort a GraphQL data by the field createdAt in descending order. When queering by ID, the API, doesn't allow you to sort the array of the given ID. If i remove the sort key, the query runs ok, but the data is in ascending order (older to new) and I need to retrieve all the new values.

Do you know how can I sort the array? Thank you in advance!

  query GetCategory{
    category(id: 4, sort: "createdAt:desc") {
      data
        {
          id
          attributes
          {
            name
            reviews{
              data
              {
                id
                attributes
                {
                  title
                  body
                  rating
                  createdAt
                  categories{
                    data
                    {
                      id
                      attributes{
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
    }
  }

1 Answers

I just need to add the sorting not at top level, but at fields level, because there is only one id (which you can't sort :)), but just because there are nested fields, you have to sort them at child/fields level i.e reviews (sort: "createdAt:desc")

The full code is:

const CATEGORY= gql`
  query GetCategory{
    category(id: 5) {
      data
        {
          id
          attributes
          {
            name
            reviews (sort: "createdAt:desc") {. <== HERE
              data
              {
                id
                attributes
                {
                  title
                  body
                  rating
                  createdAt
                  categories{
                    data
                    {
                      id
                      attributes{
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
    }
  }
`
Related