Skip to content

Commit aa7ee06

Browse files
committed
Move count of collection after Criteria event.
Document that when using a collection filter you must first resolve the criteria on the collection.
1 parent 156a1ca commit aa7ee06

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

docs/events.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ Using the Criteria object is the most efficient way to filter the collection.
128128
$driver->get(EventDispatcher::class)->subscribeTo(
129129
Artist::class . '.performances.criteria',
130130
function (Criteria $event): void {
131-
$event->setCollection($event->getCollection()->filter(
131+
// Match the collection with the criteria FIRST
132+
$matchingCollection = $event->getCollection()->matching($event->getCriteria());
133+
// Then filter the collection
134+
$event->setCollection($matchingCollection->filter(
132135
static function ($performance) {
133136
return $performance->getIsDeleted() === false;
134137
}

src/Resolve/ResolveCollectionFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ protected function buildPagination(
149149
$paginationFields[$field] = (int) base64_decode($value, true);
150150
}
151151

152-
$itemCount = count($collection->matching($criteria));
153-
154152
/**
155153
* Fire the event dispatcher using the passed event name.
156154
*/
@@ -166,6 +164,8 @@ protected function buildPagination(
166164
$collection = $event->getCollection();
167165
}
168166

167+
$itemCount = count($collection->matching($criteria));
168+
169169
// Add offset and limit after Criteria event
170170
$offsetAndLimit = $this->calculateOffsetAndLimit($resolve[3]->fieldName, $entityClassName, $targetClassName, $paginationFields, $itemCount);
171171
if ($offsetAndLimit['offset']) {

test/Entity/Performance.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Performance
7979
#[GraphQL\Field(group: 'LimitTest')]
8080
#[GraphQL\Field(group: 'AttributeLimit')]
8181
#[GraphQL\Field(group: 'ExtractionMap', alias: 'key')]
82+
#[GraphQL\Field(group: 'CriteriaEvent')]
8283

8384
#[ORM\Id]
8485
#[ORM\Column(type: 'integer')]

test/Feature/Event/CriteriaTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ public function testEventFilterCollection(): void
101101
function (CriteriaEvent $event): void {
102102
$this->assertInstanceOf(Criteria::class, $event->getCriteria());
103103

104-
$event->setCollection($event->getCollection()->filter(
104+
$matchingCollection = $event->getCollection()->matching($event->getCriteria());
105+
$event->setCollection($matchingCollection->filter(
105106
static function ($performance) {
106-
return $performance->getVenue() === 'Delta Center';
107+
return $performance->getVenue() === 'Delta Center'
108+
|| $performance->getVenue() === 'Soldier Field';
107109
},
108110
));
109111

@@ -137,9 +139,10 @@ static function ($performance) {
137139
node {
138140
id
139141
name
140-
performances {
142+
performances (filter: { venue: { sort: "DESC" } } ) {
141143
edges {
142144
node {
145+
id
143146
venue
144147
}
145148
}
@@ -160,8 +163,12 @@ static function ($performance) {
160163
$this->assertEquals(1, count($data['artist']['edges']));
161164
$this->assertEquals(1, count($data['artist']['edges'][0]['node']['performances']));
162165
$this->assertEquals(
163-
'Delta Center',
166+
'Soldier Field',
164167
$data['artist']['edges'][0]['node']['performances']['edges'][0]['node']['venue'],
165168
);
169+
$this->assertEquals(
170+
'Delta Center',
171+
$data['artist']['edges'][0]['node']['performances']['edges'][1]['node']['venue'],
172+
);
166173
}
167174
}

test/Feature/Resolve/CollectionFilterTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ public function setUp(): void
2828
'query' => new ObjectType([
2929
'name' => 'query',
3030
'fields' => [
31-
'artist' => [
32-
'type' => $driver->connection(Artist::class),
33-
'args' => [
34-
'filter' => $driver->filter(Artist::class),
35-
'pagination' => $driver->pagination(),
36-
],
37-
'resolve' => $driver->resolve(Artist::class),
38-
],
31+
'artist' => $driver->completeConnection(Artist::class),
3932
],
4033
]),
4134
]);

0 commit comments

Comments
 (0)