<p><code>RAND()</code> is the correct way to do this. (At least, it works in 3.5). One catch is that GraphQL queries are <a href="https://docs.craftcms.com/v3/config/config-settings.html#enablegraphqlcaching" rel="nofollow noreferrer">cached by default</a>, so your results will always be returned in the same random order until your cache is invalidated. If you're using Nuxt with a static target, your entries will be displayed in the same order until the cache is expired <em>and</em> your app is regenerated.</p>
<p>Example of a working query:</p>
<pre><code>{
  entries(section: &quot;posts&quot;, orderBy:&quot;RAND()&quot;) {
    title
    id
  }
}
</code></pre>

CC BY-SA 4.0
2022-02-11T22:56:42.410Z
Unfortunately, this stopped working after a recent update. Now I get "Illegal value for `orderBy` argument: `RAND()`".
David Jones
CC BY-SA 4.0
2023-11-15T13:23:44.770Z
It seems to work with `random()` now.
thomasjonas
CC BY-SA 4.0
2020-08-02T00:03:48.193Z
2020-08-02T00:03:48.193Z
Dalton Rooney
<p>I'm currently building my first JAM stack app using Nuxt as my front-end framework and Craft as my headless CMS. I'm leveraging Apollo as my GraphQL interface client. I have it pulling data back just fine, but I want to shuffle the output to always be random. I know I could do this in a typical twig query with <code>.orderBy('RAND()')</code> but I need a way to shuffle that with my GraphQL endpoint. </p>

<p>I know I can do this on the frontend by shuffling my <code>&lt;li&gt;</code>s or something, but would love for this to be more performant if I can help it.</p>

<p>I've tried a few variations of the below but can't seem to get it to work. Is this possible?</p>

<pre><code>apollo: {
  entries: {
    query: stories,
    prefetch: true,
    variables: {
       "section": ["stories"],
       "orderBy": "RAND()"
    }
  }
},
</code></pre>

CC BY-SA 4.0
2020-04-12T10:27:09.557Z
This feels like it should work. Are you sure you are not hitting cached results?
Oli
CC BY-SA 4.0
2020-04-12T19:44:21.623Z
Hey Oli. Well I know RAND() is a PHP function. Maybe this isn't a Craft CMS question and more of an Apollo question?
Rob Erskine
<graphql><headless><vue>
CraftCMS + GraphQL - orderBy Random?
# CraftCMS + GraphQL - orderBy Random?

(Asked by: username_0 on 2020-04-12)

I'm currently building my first JAM stack app using Nuxt as my front-end framework and Craft as my headless CMS. I'm leveraging Apollo as my GraphQL interface client. I have it pulling data back just fine, but I want to shuffle the output to always be random. I know I could do this in a typical twig query with .orderBy('RAND()') but I need a way to shuffle that with my GraphQL endpoint. 
I know I can do this on the frontend by shuffling my s or something, but would love for this to be more performant if I can help it.
I've tried a few variations of the below but can't seem to get it to work. Is this possible?
apollo: {
  entries: {
    query: stories,
    prefetch: true,
    variables: {
       "section": ["stories"],
       "orderBy": "RAND()"
    }
  }
},

**Tags:** graphql, headless, vue
> **Comments:**
> **username_1**: This feels like it should work. Are you sure you are not hitting cached results?  
> **username_0**: Hey Oli. Well I know RAND() is a PHP function. Maybe this isn't a Craft CMS question and more of an Apollo question?  

---

## 1 answers

### Answer by username_2 on 2020-08-02. Score: 2 | Accepted answer

RAND() is the correct way to do this. (At least, it works in 3.5). One catch is that GraphQL queries are cached by default, so your results will always be returned in the same random order until your cache is invalidated. If you're using Nuxt with a static target, your entries will be displayed in the same order until the cache is expired and your app is regenerated.
Example of a working query:
{
  entries(section: "posts", orderBy:"RAND()") {
    title
    id
  }
}

> **Comments:**
> **username_3**: Unfortunately, this stopped working after a recent update. Now I get "Illegal value for `orderBy` argument: `RAND()`".  
> **username_4**: It seems to work with `random()` now.  

---

