Skip to content

Commit

Permalink
Allow ordering elements by RAND() via GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed May 2, 2022
1 parent 54a3959 commit aca7413
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions src/gql/ArgumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '`');
}
}
Expand Down

0 comments on commit aca7413

Please sign in to comment.