Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort GraphQL query randomly

I am looking for a solution to randomly sort a GraphQL query. I have looked through the documentation and it appears that their is no options.

Does anyone know of one or a workaround to randomising the following query?

query {
  allContentfulAttraction(
    limit: 4
    filter: { location: { eq: "AttractionLocation1" } }
  ) {
    edges {
      node {
        id
      }
    }
  }
}
like image 613
Darren Avatar asked Nov 29 '25 10:11

Darren


1 Answers

GraphQL simply provides a framework for resolving and validating field values based on a schema. It does not come with any preset means of filtering or sorting the queried data. It's up to the server whether to expose ways for the client to do that, and how to implement that functionality.

With regards to Gatsby specifically, looking at their API supports querying random nodes. At best, you could query for all nodes, and then select a random set of elements from the returned data by using lodash's sampleSize or Chance's pickset.

like image 56
Daniel Rearden Avatar answered Dec 01 '25 23:12

Daniel Rearden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!