Skip to content

Commit 63906ed

Browse files
committed
Add new emptyStringAsHighest to sorting
Similar to `nullAsHighest`, it allows to control where empty string values appear in a sorting.
1 parent 4c370af commit 63906ed

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

src/Factory/FilteredQueryBuilderFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ private function applySorting(ClassMetadata $metadata, string $className, array
155155
$this->queryBuilder->addOrderBy($sortingFieldNullAsHighest, $sort['order']);
156156
}
157157

158+
if ($sort['emptyStringAsHighest'] ?? false) {
159+
$expression = 'CASE WHEN ' . $sortingField . ' = \'\' THEN 1 ELSE 0 END';
160+
$sortingFieldEmptyStringAsHighest = $this->uniqueNameFactory->createAliasName('sorting');
161+
$this->queryBuilder->addSelect($expression . ' AS HIDDEN ' . $sortingFieldEmptyStringAsHighest);
162+
$this->queryBuilder->addOrderBy($sortingFieldEmptyStringAsHighest, $sort['order']);
163+
}
164+
158165
$this->queryBuilder->addOrderBy($sortingField, $sort['order']);
159166
}
160167
}

src/Factory/Type/SortingTypeFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public function create(string $className, string $typeName): Type
5757
'description' => 'If true `NULL` values will be considered as the highest value, so appearing last in a `ASC` order, and first in a `DESC` order.',
5858
'defaultValue' => false,
5959
],
60+
[
61+
'name' => 'emptyStringAsHighest',
62+
'type' => Type::boolean(),
63+
'description' => 'If true empty strings (`""`) will be considered as the highest value, so appearing last in a `ASC` order, and first in a `DESC` order.',
64+
'defaultValue' => false,
65+
],
6066
[
6167
'name' => 'order',
6268
'type' => $this->types->get('SortingOrder'),

tests/data/ModelWithTraitsSorting.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ input ModelWithTraitsSorting {
1010
last in a `ASC` order, and first in a `DESC` order.
1111
"""
1212
nullAsHighest: Boolean = false
13+
14+
"""
15+
If true empty strings (`""`) will be considered as the highest value, so
16+
appearing last in a `ASC` order, and first in a `DESC` order.
17+
"""
18+
emptyStringAsHighest: Boolean = false
1319
order: SortingOrder = ASC
1420
}
1521

tests/data/PostSorting.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ input PostSorting {
1010
last in a `ASC` order, and first in a `DESC` order.
1111
"""
1212
nullAsHighest: Boolean = false
13+
14+
"""
15+
If true empty strings (`""`) will be considered as the highest value, so
16+
appearing last in a `ASC` order, and first in a `DESC` order.
17+
"""
18+
emptyStringAsHighest: Boolean = false
1319
order: SortingOrder = ASC
1420
}
1521

tests/data/UserSorting.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ input UserSorting {
1616
last in a `ASC` order, and first in a `DESC` order.
1717
"""
1818
nullAsHighest: Boolean = false
19+
20+
"""
21+
If true empty strings (`""`) will be considered as the highest value, so
22+
appearing last in a `ASC` order, and first in a `DESC` order.
23+
"""
24+
emptyStringAsHighest: Boolean = false
1925
order: SortingOrder = ASC
2026
}
2127

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// all posts sorted by reversed title but with null title first
6+
return [
7+
'SELECT post1, CASE WHEN post1.title = \'\' THEN 1 ELSE 0 END AS HIDDEN sorting1 FROM GraphQLTests\Doctrine\Blog\Model\Post post1 ORDER BY sorting1 DESC, post1.title DESC',
8+
\GraphQLTests\Doctrine\Blog\Model\Post::class,
9+
[],
10+
[
11+
[
12+
'field' => 'title',
13+
'order' => 'DESC',
14+
'emptyStringAsHighest' => true,
15+
],
16+
],
17+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// all posts sorted by title but with null title last
6+
return [
7+
'SELECT post1, CASE WHEN post1.title = \'\' THEN 1 ELSE 0 END AS HIDDEN sorting1 FROM GraphQLTests\Doctrine\Blog\Model\Post post1 ORDER BY sorting1 ASC, post1.title ASC',
8+
\GraphQLTests\Doctrine\Blog\Model\Post::class,
9+
[],
10+
[
11+
[
12+
'field' => 'title',
13+
'emptyStringAsHighest' => true,
14+
'order' => 'ASC',
15+
],
16+
],
17+
];

0 commit comments

Comments
 (0)