Query Random Entries

Is it possible to query random entries via graphQL with Dato? I’m creating a related posts component and instead of defining relations in Dato I was hoping to just randomly grab 3 entries and drop them into the component, though I can’t think of any way to accomplish this.

1 Like

hello @lee and welcome to Community!

Unfortunately it’s not possible at the moment, but I’m converting your message in a feature request so that if it gets enough traction we can think about implementing it :slight_smile:

2 Likes

Reviving this feature request and hoping this gets implemented at some point :slight_smile:

Just as a workaround, you can accomplish this by just having the component (on your frontend) fetch X number of post IDs, like the latest 500 of them, and then randomly choose 3 of them in Javascript.

Might that work, @mathieu.gauthier? If you can please share your example use case and the model/query in question, I can show you a quick example?

Hi Roger, I have done something similar to work around the problem. I just don’t like querying more than what I need haha. Thanks for your response

1 Like

I see, thanks for explaining! Yeah, it doesn’t feel great to be asking for and getting more data back than you really need.

You can also do this by combining GraphQL variables with the first and skip parameters, in a query like:

query GetRandomModels($first: IntType, $second: IntType, $third: IntType) {
  record_1: allExampleRecords(first: 1, skip: $first) {
    ...ExampleRecordFragment
  }
  record_2: allExampleRecords(first: 1, skip: $second) {
    ...ExampleRecordFragment
  }
  record_3: allExampleRecords(first: 1, skip: $third) {
    ...ExampleRecordFragment
  }
}

fragment ExampleRecordFragment on ExampleRecordRecord {
  id
  title
  slug
}

Where the variables are:

{
  "first": 56, // random integer < max # of records
  "second":425, // another random integer
  "third": 1025 // etc
}

That way, your app just generates a few random integers and fetches only their data.

The resulting output would be those three records (only), like:

{
  "data": {
    "record_1": [
      {
        "id": "N4sYF4wjSeaqlNRDyafsjw",
        "title": "lime partial online cow openly synthesizing kinase matrix",
        "slug": "lime-partial-online-cow-openly-synthesizing-kinase-matrix"
      }
    ],
    "record_2": [
      {
        "id": "SCHclbQnQ9SRM3GeLJf9Gw",
        "title": "violet sore digital giraffe especially overriding fraudster pixel",
        "slug": "violet-sore-digital-giraffe-especially-overriding-fraudster-pixel"
      }
    ],
    "record_3": [
      {
        "id": "cMpdMyN6SGG3yFA-poFy3g",
        "title": "salmon frizzy back-end panda daintily programming release program",
        "slug": "salmon-frizzy-back-end-panda-daintily-programming-release-program"
      }
    ]
  }
}

I still think a true “get me X random records” would be nice (so hopefully this feature gets implemented), but in the meantime, hopefully that’s better than nothing…

That’s exactly what I have done, go with the skip / limit method and generating a random number < max records!

1 Like