Skip to content

Commit 2e63dff

Browse files
committed
Exclude property from GraphQL sorting
This should have been excluded already since 4e52511
1 parent b821d4d commit 2e63dff

File tree

5 files changed

+63
-20
lines changed

5 files changed

+63
-20
lines changed

src/Factory/AbstractFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
use Doctrine\Common\Annotations\Reader;
88
use Doctrine\ORM\EntityManager;
9+
use Doctrine\ORM\Mapping\ClassMetadata;
910
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver;
1011
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
12+
use GraphQL\Doctrine\Annotation\Exclude;
1113
use GraphQL\Doctrine\Exception;
1214
use GraphQL\Doctrine\Factory\MetadataReader\MappingDriverChainAdapter;
1315
use GraphQL\Doctrine\Types;
@@ -51,4 +53,20 @@ final protected function getAnnotationReader(): Reader
5153

5254
throw new Exception('graphql-doctrine requires Doctrine to be configured with a `' . AnnotationDriver::class . '`.');
5355
}
56+
57+
/**
58+
* Returns whether the property is excluded
59+
*
60+
* @param ClassMetadata $metadata
61+
* @param string $propertyName
62+
*
63+
* @return bool
64+
*/
65+
final protected function isPropertyExcluded(ClassMetadata $metadata, string $propertyName): bool
66+
{
67+
$property = $metadata->getReflectionProperty($propertyName);
68+
$exclude = $this->getAnnotationReader()->getPropertyAnnotation($property, Exclude::class);
69+
70+
return $exclude !== null;
71+
}
5472
}

src/Factory/Type/FilterGroupConditionTypeFactory.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace GraphQL\Doctrine\Factory\Type;
66

7-
use GraphQL\Doctrine\Annotation\Exclude;
87
use GraphQL\Doctrine\Annotation\Filter;
98
use GraphQL\Doctrine\Annotation\Filters;
10-
119
use GraphQL\Doctrine\Definition\Operator\AbstractOperator;
1210
use GraphQL\Doctrine\Definition\Operator\BetweenOperatorType;
1311
use GraphQL\Doctrine\Definition\Operator\EmptyOperatorType;
@@ -26,7 +24,6 @@
2624
use GraphQL\Type\Definition\LeafType;
2725
use GraphQL\Type\Definition\Type;
2826
use ReflectionClass;
29-
use ReflectionProperty;
3027

3128
/**
3229
* A factory to create an InputObjectType from a Doctrine entity to
@@ -64,8 +61,7 @@ public function create(string $className, string $typeName): Type
6461
foreach ($metadata->fieldMappings as $mapping) {
6562

6663
// Skip exclusion specified by user
67-
$property = $metadata->getReflectionProperty($mapping['fieldName']);
68-
if ($this->isExcluded($property)) {
64+
if ($this->isPropertyExcluded($metadata, $mapping['fieldName'])) {
6965
continue;
7066
}
7167

@@ -284,18 +280,4 @@ private function getOperatorFieldName(string $className): string
284280

285281
return lcfirst($name);
286282
}
287-
288-
/**
289-
* Returns whether the property is excluded
290-
*
291-
* @param ReflectionProperty $property
292-
*
293-
* @return bool
294-
*/
295-
private function isExcluded(ReflectionProperty $property): bool
296-
{
297-
$exclude = $this->getAnnotationReader()->getPropertyAnnotation($property, Exclude::class);
298-
299-
return $exclude !== null;
300-
}
301283
}

src/Factory/Type/SortingTypeFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public function create(string $className, string $typeName): Type
7979
private function getPossibleValues(string $className): array
8080
{
8181
$metadata = $this->entityManager->getClassMetadata($className);
82-
$standard = array_values($metadata->fieldNames);
82+
$standard = array_values(array_filter($metadata->fieldNames, function ($fieldName) use ($metadata) {
83+
return !$this->isPropertyExcluded($metadata, $fieldName);
84+
}));
8385
$custom = $this->getCustomSortingNames($className);
8486

8587
return array_merge($standard, $custom);

tests/SortingTypesTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GraphQLTests\Doctrine\Blog\Model\Post;
88
use GraphQLTests\Doctrine\Blog\Model\Special\ModelWithTraits;
9+
use GraphQLTests\Doctrine\Blog\Model\User;
910

1011
final class SortingTypesTest extends \PHPUnit\Framework\TestCase
1112
{
@@ -17,6 +18,12 @@ public function testCanGetPostSorting(): void
1718
$this->assertAllTypes('tests/data/PostSorting.graphqls', $actual);
1819
}
1920

21+
public function testCanGetUserSorting(): void
22+
{
23+
$actual = $this->types->getSorting(User::class);
24+
$this->assertAllTypes('tests/data/UserSorting.graphqls', $actual);
25+
}
26+
2027
public function testCanInheritSortingFromTraits(): void
2128
{
2229
$actual = $this->types->getSorting(ModelWithTraits::class);

tests/data/UserSorting.graphqls

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
schema {
2+
query: query
3+
}
4+
5+
"""Order to be used in DQL"""
6+
enum SortingOrder {
7+
ASC
8+
DESC
9+
}
10+
11+
input UserSorting {
12+
field: UserSortingField!
13+
14+
"""
15+
If true `NULL` values will be considered as the highest value, so appearing
16+
last in a `ASC` order, and first in a `DESC` order.
17+
"""
18+
nullAsHighest: Boolean = false
19+
order: SortingOrder = ASC
20+
}
21+
22+
"""Fields available for `UserSorting`"""
23+
enum UserSortingField {
24+
creationDate
25+
name
26+
email
27+
isAdministrator
28+
id
29+
pseudoRandom
30+
}
31+
32+
type query {
33+
defaultField(defaultArg: [UserSorting!]): Boolean
34+
}

0 commit comments

Comments
 (0)