Skip to content

Commit a50b6ec

Browse files
committed
Added collection to Criteria event
1 parent 25e2e4c commit a50b6ec

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

docs/events.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ the association if you assigned an event name in the attributes.
109109
},
110110
);
111111
112-
The ``Criteria`` event has one function in addition to getters for
112+
The ``Criteria`` event has two functions in addition to getters for
113113
all resolve parameters:
114114

115115
* ``getCriteria`` - Will return a Criteria object with the user specified
116116
filters already applied.
117+
* ``getCollection`` - Will return the unfetched collection object. This is useful
118+
if you need to fetch the collection to apply additional criteria.
117119

118120

119121
Modify an Entity Definition

src/Event/Criteria.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace ApiSkeletons\Doctrine\ORM\GraphQL\Event;
66

7+
use Doctrine\Common\Collections\Collection;
78
use Doctrine\Common\Collections\Criteria as DoctrineCriteria;
9+
use Doctrine\ORM\PersistentCollection;
810
use GraphQL\Type\Definition\ResolveInfo;
911
use League\Event\HasEventName;
1012

@@ -15,10 +17,14 @@
1517
class Criteria implements
1618
HasEventName
1719
{
18-
/** @param mixed[] $args */
20+
/**
21+
* @param PersistentCollection<array-key, mixed> $collection
22+
* @param mixed[] $args
23+
*/
1924
public function __construct(
2025
protected readonly DoctrineCriteria $criteria,
2126
protected readonly string $eventName,
27+
protected readonly Collection $collection,
2228
protected readonly mixed $objectValue,
2329
protected readonly array $args,
2430
protected readonly mixed $context,
@@ -36,6 +42,12 @@ public function getCriteria(): DoctrineCriteria
3642
return $this->criteria;
3743
}
3844

45+
/** @return PersistentCollection<array-key, mixed> */
46+
public function getCollection(): Collection
47+
{
48+
return $this->collection;
49+
}
50+
3951
public function getObjectValue(): mixed
4052
{
4153
return $this->objectValue;

src/Resolve/ResolveCollectionFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ protected function buildPagination(
168168
new CriteriaEvent(
169169
$criteria,
170170
$criteriaEventName,
171+
$collection,
171172
...$resolve,
172173
),
173174
);

test/Feature/Event/CriteriaTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use ApiSkeletons\Doctrine\ORM\GraphQL\Event\Criteria as CriteriaEvent;
1010
use ApiSkeletonsTest\Doctrine\ORM\GraphQL\AbstractTest;
1111
use ApiSkeletonsTest\Doctrine\ORM\GraphQL\Entity\Artist;
12+
use Doctrine\Common\Collections\Collection;
1213
use Doctrine\Common\Collections\Criteria;
1314
use GraphQL\GraphQL;
1415
use GraphQL\Type\Definition\ObjectType;
@@ -33,6 +34,7 @@ function (CriteriaEvent $event): void {
3334
$event->getCriteria()->expr()->eq('venue', 'Delta Center'),
3435
);
3536

37+
$this->assertInstanceOf(Collection::class, $event->getCollection());
3638
$this->assertInstanceOf(Artist::class, $event->getObjectValue());
3739
$this->assertEquals('contextTest', $event->getContext());
3840
$this->assertIsArray($event->getArgs());

0 commit comments

Comments
 (0)