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
}
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With