Home > Enterprise >  How can I query my data by published date (newest first) using Apollo and Graphql?
How can I query my data by published date (newest first) using Apollo and Graphql?

Time:09-22

I have React page that is fetching data via Graphql. I'm using Strapi as backend and queries are already generated. I´m querying my data in the frontend like this

query GetData{
  datas(limit:3){
    id
    published_at
  }
}

In the documentation I found this example about how to sort my queries by some especific order

GET /users?_sort=email:ASC,dateField:DESC

but is not really clear how to use it with the query structure. I tried something like this and other variations

query GetPodcasts{
  podcasts?_sort=published_at:DESC(limit:3){
    id
    published_at
  }
}

but it didn't work.

I may need some help understanding this.

CodePudding user response:

Graphql is like an Rest Api but with one end point to the server , this query with name GetData , point to datas query but you should define this query in the backend. please watch some tutorials that will guide you step by step. you can learn more about graphql here https://graphql.org/learn/

try this:

query GetData{
  datas(input:{
    sort:{id:"asc"},
    data:{limit:3}
    }){
    id
    published_at
  }
}

for more info https://strapi.io/documentation/developer-docs/latest/development/plugins/graphql.html#query-api

CodePudding user response:

In a forum some nice people also gave me an answer. When using graphql in frontend and want to sort or filter the data, just have to use "" to specify the sort or filter. In my case it just had to be:

query GetData{
  datas(limit:3, sort:"published_at:desc"){
    id
    published_at
  }
}
  • Related