@@ -54,7 +54,9 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
5454 if (!$ resourceMetadata ->getGraphqlAttribute ($ operationName , 'serialize ' , true , true )) {
5555 if ($ isCollection ) {
5656 if ($ this ->pagination ->isGraphQlEnabled ($ resourceClass , $ operationName , $ context )) {
57- return $ this ->getDefaultPaginatedData ();
57+ return 'cursor ' === $ this ->pagination ->getGraphQlPaginationType ($ resourceClass , $ operationName ) ?
58+ $ this ->getDefaultCursorBasedPaginatedData () :
59+ $ this ->getDefaultPageBasedPaginatedData ();
5860 }
5961
6062 return [];
@@ -87,7 +89,9 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
8789 $ data [$ index ] = $ this ->normalizer ->normalize ($ object , ItemNormalizer::FORMAT , $ normalizationContext );
8890 }
8991 } else {
90- $ data = $ this ->serializePaginatedCollection ($ itemOrCollection , $ normalizationContext , $ context );
92+ $ data = 'cursor ' === $ this ->pagination ->getGraphQlPaginationType ($ resourceClass , $ operationName ) ?
93+ $ this ->serializeCursorBasedPaginatedCollection ($ itemOrCollection , $ normalizationContext , $ context ) :
94+ $ this ->serializePageBasedPaginatedCollection ($ itemOrCollection , $ normalizationContext );
9195 }
9296 }
9397
@@ -108,7 +112,7 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
108112 * @throws \LogicException
109113 * @throws \UnexpectedValueException
110114 */
111- private function serializePaginatedCollection (iterable $ collection , array $ normalizationContext , array $ context ): array
115+ private function serializeCursorBasedPaginatedCollection (iterable $ collection , array $ normalizationContext , array $ context ): array
112116 {
113117 $ args = $ context ['args ' ];
114118
@@ -138,7 +142,7 @@ private function serializePaginatedCollection(iterable $collection, array $norma
138142 }
139143 $ offset = 0 > $ offset ? 0 : $ offset ;
140144
141- $ data = $ this ->getDefaultPaginatedData ();
145+ $ data = $ this ->getDefaultCursorBasedPaginatedData ();
142146
143147 if (($ totalItems = $ collection ->getTotalItems ()) > 0 ) {
144148 $ data ['totalCount ' ] = $ totalItems ;
@@ -161,11 +165,37 @@ private function serializePaginatedCollection(iterable $collection, array $norma
161165 return $ data ;
162166 }
163167
164- private function getDefaultPaginatedData (): array
168+ /**
169+ * @throws \LogicException
170+ */
171+ private function serializePageBasedPaginatedCollection (iterable $ collection , array $ normalizationContext ): array
172+ {
173+ if (!($ collection instanceof PaginatorInterface)) {
174+ throw new \LogicException (sprintf ('Collection returned by the collection data provider must implement %s. ' , PaginatorInterface::class));
175+ }
176+
177+ $ data = $ this ->getDefaultPageBasedPaginatedData ();
178+ $ data ['paginationInfo ' ]['totalCount ' ] = $ collection ->getTotalItems ();
179+ $ data ['paginationInfo ' ]['lastPage ' ] = $ collection ->getLastPage ();
180+ $ data ['paginationInfo ' ]['itemsPerPage ' ] = $ collection ->getItemsPerPage ();
181+
182+ foreach ($ collection as $ object ) {
183+ $ data ['collection ' ][] = $ this ->normalizer ->normalize ($ object , ItemNormalizer::FORMAT , $ normalizationContext );
184+ }
185+
186+ return $ data ;
187+ }
188+
189+ private function getDefaultCursorBasedPaginatedData (): array
165190 {
166191 return ['totalCount ' => 0. , 'edges ' => [], 'pageInfo ' => ['startCursor ' => null , 'endCursor ' => null , 'hasNextPage ' => false , 'hasPreviousPage ' => false ]];
167192 }
168193
194+ private function getDefaultPageBasedPaginatedData (): array
195+ {
196+ return ['collection ' => [], 'paginationInfo ' => ['itemsPerPage ' => 0. , 'totalCount ' => 0. , 'lastPage ' => 0. ]];
197+ }
198+
169199 private function getDefaultMutationData (array $ context ): array
170200 {
171201 return ['clientMutationId ' => $ context ['args ' ]['input ' ]['clientMutationId ' ] ?? null ];
0 commit comments