66
77use ApiSkeletons \Doctrine \GraphQL \Config ;
88use ApiSkeletons \Doctrine \GraphQL \Criteria \Filters as FiltersDef ;
9+ use ApiSkeletons \Doctrine \GraphQL \Event \FilterCriteria ;
10+ use ApiSkeletons \Doctrine \GraphQL \Metadata \Metadata ;
911use ApiSkeletons \Doctrine \GraphQL \Type \Entity ;
1012use ApiSkeletons \Doctrine \GraphQL \Type \TypeManager ;
1113use Closure ;
1517use Doctrine \ORM \Mapping \ClassMetadata ;
1618use Doctrine \ORM \PersistentCollection ;
1719use GraphQL \Type \Definition \ResolveInfo ;
20+ use League \Event \EventDispatcher ;
1821
1922use function base64_decode ;
2023use function base64_encode ;
@@ -27,6 +30,8 @@ public function __construct(
2730 protected Config $ config ,
2831 protected FieldResolver $ fieldResolver ,
2932 protected TypeManager $ typeManager ,
33+ protected EventDispatcher $ eventDispatcher ,
34+ protected Metadata $ metadata ,
3035 ) {
3136 }
3237
@@ -51,21 +56,29 @@ public function parseArrayValue(ClassMetadata $metadata, string $field, array $v
5156
5257 public function get (Entity $ entity ): Closure
5358 {
54- return function ($ source , $ args , $ context , ResolveInfo $ resolveInfo ) {
59+ return function ($ source , array $ args , $ context , ResolveInfo $ info ) {
5560 $ fieldResolver = $ this ->fieldResolver ;
56- $ collection = $ fieldResolver ($ source , $ args , $ context , $ resolveInfo );
61+ $ collection = $ fieldResolver ($ source , $ args , $ context , $ info );
5762
5863 $ collectionMetadata = $ this ->entityManager ->getMetadataFactory ()
5964 ->getMetadataFor (
6065 (string ) $ this ->entityManager ->getMetadataFactory ()
6166 ->getMetadataFor (ClassUtils::getRealClass ($ source ::class))
62- ->getAssociationTargetClass ($ resolveInfo ->fieldName ),
67+ ->getAssociationTargetClass ($ info ->fieldName ),
6368 );
6469
70+ $ metadataConfig = $ this ->metadata ->getMetadataConfig ();
71+ $ entityClass = ClassUtils::getRealClass ($ source ::class);
72+
6573 return $ this ->buildPagination (
6674 $ args ['pagination ' ] ?? [],
6775 $ collection ,
6876 $ this ->buildCriteria ($ args ['filter ' ] ?? [], $ collectionMetadata ),
77+ $ metadataConfig [$ entityClass ]['fields ' ][$ info ->fieldName ]['filterCriteriaEventName ' ],
78+ $ source ,
79+ $ args ,
80+ $ context ,
81+ $ info ,
6982 );
7083 };
7184 }
@@ -116,8 +129,13 @@ private function buildCriteria(array $filter, ClassMetadata $collectionMetadata)
116129 *
117130 * @return mixed[]
118131 */
119- private function buildPagination (array $ pagination , PersistentCollection $ collection , Criteria $ criteria ): array
120- {
132+ private function buildPagination (
133+ array $ pagination ,
134+ PersistentCollection $ collection ,
135+ Criteria $ criteria ,
136+ string |null $ filterCriteriaEventName ,
137+ mixed ...$ resolve ,
138+ ): array {
121139 $ first = 0 ;
122140 $ after = 0 ;
123141 $ last = 0 ;
@@ -174,6 +192,19 @@ private function buildPagination(array $pagination, PersistentCollection $collec
174192 $ criteria ->setMaxResults ($ limit );
175193 }
176194
195+ /**
196+ * Fire the event dispatcher using the passed event name.
197+ */
198+ if ($ filterCriteriaEventName ) {
199+ $ this ->eventDispatcher ->dispatch (
200+ new FilterCriteria (
201+ $ criteria ,
202+ $ filterCriteriaEventName ,
203+ ...$ resolve ,
204+ ),
205+ );
206+ }
207+
177208 // Fetch slice of collection
178209 $ items = $ collection ->matching ($ criteria );
179210
0 commit comments