From aca7413d3bca5aabc265f3aea981d778f5075299 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Mon, 2 May 2022 12:42:07 -0700 Subject: [PATCH] Allow ordering elements by RAND() via GraphQL Fixes #11063 --- CHANGELOG.md | 1 + src/gql/ArgumentManager.php | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5b98e7161a..49e34ba790f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Fixed a bug where “Name” and “Handle” were not getting translated properly in field, volume, and global set validation errors. ([#11029](https://github.com/craftcms/cms/issues/11029)) - Fixed a bug where passing an array of `craft\models\UserGroup` objects to a user query’s `group` param had no effect. - Fixed a bug where passing an array that began with `not` into a user query’s `group` param wouldn’t return users that had no user groups. +- Fixed a bug where it wasn’t possible to order elements by `RAND()` via GraphQL. ([#11063](https://github.com/craftcms/cms/issues/11063)) ## 3.7.39 - 2022-04-26 diff --git a/src/gql/ArgumentManager.php b/src/gql/ArgumentManager.php index 1fc0813dcba..cba98066e43 100644 --- a/src/gql/ArgumentManager.php +++ b/src/gql/ArgumentManager.php @@ -121,12 +121,15 @@ public function prepareArguments($arguments): array { $orderBy = $arguments['orderBy'] ?? null; if ($orderBy) { - if (StringHelper::containsAny($orderBy, ['(', ')'])) { - throw new GqlException('Illegal value for `orderBy` argument: `' . $orderBy . '`'); - } - $chunks = StringHelper::split($orderBy); - foreach ($chunks as $chunk) { - if (!preg_match('/^\w+(\.\w+)?( (asc|desc))?$/i', $chunk)) { + foreach (StringHelper::split($orderBy) as $chunk) { + // Special case for `RAND()` + if (strtolower($chunk) === 'rand()') { + continue; + } + if ( + StringHelper::containsAny($orderBy, ['(', ')']) || + !preg_match('/^\w+(\.\w+)?( (asc|desc))?$/i', $chunk) + ) { throw new GqlException('Illegal value for `orderBy` argument: `' . $orderBy . '`'); } }