@@ -54,7 +54,9 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
54
54
if (!$ resourceMetadata ->getGraphqlAttribute ($ operationName , 'serialize ' , true , true )) {
55
55
if ($ isCollection ) {
56
56
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 ();
58
60
}
59
61
60
62
return [];
@@ -87,7 +89,9 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
87
89
$ data [$ index ] = $ this ->normalizer ->normalize ($ object , ItemNormalizer::FORMAT , $ normalizationContext );
88
90
}
89
91
} 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 );
91
95
}
92
96
}
93
97
@@ -108,7 +112,7 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
108
112
* @throws \LogicException
109
113
* @throws \UnexpectedValueException
110
114
*/
111
- private function serializePaginatedCollection (iterable $ collection , array $ normalizationContext , array $ context ): array
115
+ private function serializeCursorBasedPaginatedCollection (iterable $ collection , array $ normalizationContext , array $ context ): array
112
116
{
113
117
$ args = $ context ['args ' ];
114
118
@@ -138,7 +142,7 @@ private function serializePaginatedCollection(iterable $collection, array $norma
138
142
}
139
143
$ offset = 0 > $ offset ? 0 : $ offset ;
140
144
141
- $ data = $ this ->getDefaultPaginatedData ();
145
+ $ data = $ this ->getDefaultCursorBasedPaginatedData ();
142
146
143
147
if (($ totalItems = $ collection ->getTotalItems ()) > 0 ) {
144
148
$ data ['totalCount ' ] = $ totalItems ;
@@ -161,11 +165,37 @@ private function serializePaginatedCollection(iterable $collection, array $norma
161
165
return $ data ;
162
166
}
163
167
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
165
190
{
166
191
return ['totalCount ' => 0. , 'edges ' => [], 'pageInfo ' => ['startCursor ' => null , 'endCursor ' => null , 'hasNextPage ' => false , 'hasPreviousPage ' => false ]];
167
192
}
168
193
194
+ private function getDefaultPageBasedPaginatedData (): array
195
+ {
196
+ return ['collection ' => [], 'paginationInfo ' => ['itemsPerPage ' => 0. , 'totalCount ' => 0. , 'lastPage ' => 0. ]];
197
+ }
198
+
169
199
private function getDefaultMutationData (array $ context ): array
170
200
{
171
201
return ['clientMutationId ' => $ context ['args ' ]['input ' ]['clientMutationId ' ] ?? null ];
0 commit comments