Skip to content

Commit 6ad0f04

Browse files
committed
Support sorting that ends with Type
There is no reason to have any restriction on sorting names
1 parent 91db05d commit 6ad0f04

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Factory/Type/SortingTypeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private function getFromAnnotation(ReflectionClass $class): array
136136
foreach ($sorting->classes as $className) {
137137
$this->throwIfInvalidAnnotation($classWithAnnotation, 'Sorting', SortingInterface::class, $className);
138138

139-
$name = lcfirst(preg_replace('~Type$~', '', Utils::getTypeName($className)));
139+
$name = lcfirst(Utils::getTypeName($className));
140140
$result[$name] = new $className();
141141
}
142142
}

tests/Blog/Model/Post.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
* A blog post with title and body
1414
*
1515
* @ORM\Entity
16-
* @API\Sorting({"GraphQLTests\Doctrine\Blog\Sorting\UserName"})
16+
* @API\Sorting({
17+
* "GraphQLTests\Doctrine\Blog\Sorting\UserName",
18+
* "GraphQLTests\Doctrine\Blog\Sorting\PostType",
19+
* })
1720
* @API\Filters({
1821
* @API\Filter(field="custom", operator="GraphQLTests\Doctrine\Blog\Filtering\SearchOperatorType", type="string"),
1922
* })

tests/Blog/Sorting/PostType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Blog\Sorting;
6+
7+
use Doctrine\ORM\Mapping\ClassMetadata;
8+
use Doctrine\ORM\QueryBuilder;
9+
use GraphQL\Doctrine\Factory\UniqueNameFactory;
10+
use GraphQL\Doctrine\Sorting\SortingInterface;
11+
12+
final class PostType implements SortingInterface
13+
{
14+
public function __construct()
15+
{
16+
}
17+
18+
public function __invoke(UniqueNameFactory $uniqueNameFactory, ClassMetadata $metadata, QueryBuilder $queryBuilder, string $alias, string $order): void
19+
{
20+
$queryBuilder->addOrderBy($alias . '.type', $order);
21+
}
22+
}

tests/data/PostSorting.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum PostSortingField {
2222
status
2323
id
2424
userName
25+
postType
2526
pseudoRandom
2627
}
2728

0 commit comments

Comments
 (0)