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:DESCbut 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.
3 Answers
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
try this:
query GetData{ datas(input:{ sort:{id:"asc"}, data:{limit:3} }){ id published_at }
}for more info
2In 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 }
} 1 This seem to work for me in Strapi v4:
sort: "publishedAt:DESC"...
query GetVideos($page: Int!, $pageSize: Int!) { videos( pagination: { page: $page, pageSize: $pageSize } sort: "publishedAt:DESC" ) { __typename data { __typename id attributes { __typename createdAt publishedAt } } } } }
`