Strapiv4 & apollo graphql, sort results by multiples dates

Strapiv4 & apollo graphql, sort results by multiples dates


0

I have a list of articles, which have several dates attached to them :

  • publishedAt (default date generated by strapi when article created)
  • date (manual date user can enter to alter the default date)
  • updateDate (manual date user can enter to print a second date, mentioning that the article was updated, and when)

Basically what i would like is for my query to sort articles checking their updatedDate if they have one, if not their date, and if not their puslishedAt.

The probleme is that my sorting does not seem to work, it seems to sort dates as if they were string, and chaining sorting options does not seem to have an effect.

I have tried variations around this :

query {
  articles(
    sort:["publishedAt:DESC", "date:DESC", "updateDate:DESC"]) {
    data {
      id
      attributes {
        updateDate   
          date
        publishedAt       
      }
    }  
  }
}

The above renders this :

{
  "data": {
    "articles": {
      "data": [
        {
          "id": "28",
          "attributes": {
            "updateDate": null,
            "date": null,
            "publishedAt": "2023-09-14T13:21:17.708Z"
          }
        },
        {
          "id": "36",
          "attributes": {
            "updateDate": null,
            "date": null,
            "publishedAt": "2023-09-14T13:21:17.708Z"
          }
        },
        {
          "id": "37",
          "attributes": {
            "updateDate": null,
            "date": null,
            "publishedAt": "2023-09-14T13:21:17.708Z"
          }
        },
        {
          "id": "20",
          "attributes": {
            "updateDate": null,
            "date": "2023-08-31",
            "publishedAt": "2023-09-14T13:21:17.708Z"
          }
        },
        {
          "id": "15",
          "attributes": {
            "updateDate": "2023-09-10",
            "date": "2023-09-02",
            "publishedAt": "2023-09-01T13:20:18.508Z"
          }
        }
      ]
    }
  }
}

Null dates seem to come first as if the query was sorting strings. I do not seem to have other filter options, and i would like to avoid sorting this stuff in JS.

Thanks in advance for any help !


Load 5 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *