Skip to content

Commit efdae1b

Browse files
committed
Moved pagination of Criteria after the event
1 parent a50b6ec commit efdae1b

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

docs/events.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ When an association is resolved from an entity or another association, you may
7979
listen to the Criteria Event to add additional criteria for filtering
8080
the association if you assigned an event name in the attributes.
8181

82+
Note that pagination limits are not applied to the Collection before this event
83+
is fired. That way you can add additional criteria to the Collection before
84+
the limit is applied. This is done by fetching the collection within the event
85+
and running additional filters on each element. This is not the most efficient
86+
way to filter data, but it is the most flexible.
87+
8288
.. code-block:: php
8389
8490
use ApiSkeletons\Doctrine\ORM\GraphQL\Attribute as GraphQL;

src/Event/Criteria.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class Criteria implements
2222
* @param mixed[] $args
2323
*/
2424
public function __construct(
25-
protected readonly DoctrineCriteria $criteria,
2625
protected readonly string $eventName,
26+
protected readonly DoctrineCriteria $criteria,
2727
protected readonly Collection $collection,
2828
protected readonly mixed $objectValue,
2929
protected readonly array $args,

src/Resolve/ResolveCollectionFactory.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,30 @@ protected function buildPagination(
151151

152152
$itemCount = count($collection->matching($criteria));
153153

154-
$offsetAndLimit = $this->calculateOffsetAndLimit($resolve[3]->fieldName, $entityClassName, $targetClassName, $paginationFields, $itemCount);
155-
if ($offsetAndLimit['offset']) {
156-
$criteria->setFirstResult($offsetAndLimit['offset']);
157-
}
158-
159-
if ($offsetAndLimit['limit']) {
160-
$criteria->setMaxResults($offsetAndLimit['limit']);
161-
}
162-
163154
/**
164155
* Fire the event dispatcher using the passed event name.
165156
*/
166157
if ($criteriaEventName) {
167158
$this->eventDispatcher->dispatch(
168159
new CriteriaEvent(
169-
$criteria,
170160
$criteriaEventName,
161+
$criteria,
171162
$collection,
172163
...$resolve,
173164
),
174165
);
175166
}
176167

168+
// Add offset and limit after Criteria event
169+
$offsetAndLimit = $this->calculateOffsetAndLimit($resolve[3]->fieldName, $entityClassName, $targetClassName, $paginationFields, $itemCount);
170+
if ($offsetAndLimit['offset']) {
171+
$criteria->setFirstResult($offsetAndLimit['offset']);
172+
}
173+
174+
if ($offsetAndLimit['limit']) {
175+
$criteria->setMaxResults($offsetAndLimit['limit']);
176+
}
177+
177178
$edgesAndCursors = $this->buildEdgesAndCursors($collection->matching($criteria), $offsetAndLimit, $itemCount);
178179

179180
// Return entities

0 commit comments

Comments
 (0)