1414namespace ApiPlatform \Core \DataProvider ;
1515
1616use ApiPlatform \Core \Exception \InvalidArgumentException ;
17+ use ApiPlatform \Core \Exception \ResourceClassNotFoundException ;
1718use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
1819
1920/**
2425final class Pagination
2526{
2627 private $ options ;
28+ private $ graphQlOptions ;
2729 private $ resourceMetadataFactory ;
2830
29- public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , array $ options = [])
31+ public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , array $ options = [], array $ graphQlOptions = [] )
3032 {
3133 $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
3234 $ this ->options = array_merge ([
@@ -43,6 +45,9 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
4345 'client_partial ' => false ,
4446 'partial_parameter_name ' => 'partial ' ,
4547 ], $ options );
48+ $ this ->graphQlOptions = array_merge ([
49+ 'enabled ' => true ,
50+ ], $ graphQlOptions );
4651 }
4752
4853 /**
@@ -171,6 +176,14 @@ public function isEnabled(string $resourceClass = null, string $operationName =
171176 return $ this ->getEnabled ($ context , $ resourceClass , $ operationName );
172177 }
173178
179+ /**
180+ * Is the pagination enabled for GraphQL?
181+ */
182+ public function isGraphQlEnabled (?string $ resourceClass = null , ?string $ operationName = null , array $ context = []): bool
183+ {
184+ return $ this ->getGraphQlEnabled ($ resourceClass , $ operationName );
185+ }
186+
174187 /**
175188 * Is the partial pagination enabled?
176189 */
@@ -198,6 +211,23 @@ private function getEnabled(array $context, string $resourceClass = null, string
198211 return filter_var ($ this ->getParameterFromContext ($ context , $ this ->options [$ partial ? 'partial_parameter_name ' : 'enabled_parameter_name ' ], $ enabled ), FILTER_VALIDATE_BOOLEAN );
199212 }
200213
214+ return (bool ) $ enabled ;
215+ }
216+
217+ private function getGraphQlEnabled (?string $ resourceClass , ?string $ operationName ): bool
218+ {
219+ $ enabled = $ this ->graphQlOptions ['enabled ' ];
220+
221+ if (null !== $ resourceClass ) {
222+ try {
223+ $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
224+ } catch (ResourceClassNotFoundException $ e ) {
225+ return $ enabled ;
226+ }
227+
228+ return (bool ) $ resourceMetadata ->getGraphqlAttribute ($ operationName , 'pagination_enabled ' , $ enabled , true );
229+ }
230+
201231 return $ enabled ;
202232 }
203233
0 commit comments