Symfony 6.4 - Update graphql collection query return as paginator

This commit is contained in:
Clemente Raposo 2024-02-20 14:41:14 +00:00
parent 8597c7fea2
commit 4e7cdd780d
2 changed files with 10 additions and 6 deletions

View file

@ -29,6 +29,7 @@ namespace App\SystemConfig\DataProvider;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\ArrayPaginator;
use ApiPlatform\State\ProviderInterface;
use App\SystemConfig\Entity\SystemConfig;
use App\SystemConfig\Service\SystemConfigProviderInterface;
@ -58,12 +59,13 @@ final class SystemConfigStateProvider implements ProviderInterface
* @param Operation $operation
* @param array $uriVariables
* @param array $context
* @return array|SystemConfig|null
* @return ArrayPaginator|SystemConfig|null
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|SystemConfig|null
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ArrayPaginator|SystemConfig|null
{
if ($operation instanceof CollectionOperationInterface) {
return $this->systemConfigProvider->getAllSystemConfigs();
$systemConfigs = $this->systemConfigProvider->getAllSystemConfigs();
return new ArrayPaginator($systemConfigs, 0, count($systemConfigs));
}
return $this->systemConfigProvider->getSystemConfig($uriVariables['id'] ?? '');

View file

@ -29,6 +29,7 @@ namespace App\UserPreferences\DataProvider;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\ArrayPaginator;
use ApiPlatform\State\ProviderInterface;
use App\UserPreferences\Entity\UserPreference;
use App\UserPreferences\Service\UserPreferencesProviderInterface;
@ -58,12 +59,13 @@ final class UserPreferenceStateProvider implements ProviderInterface
* @param Operation $operation
* @param array $uriVariables
* @param array $context
* @return iterable|UserPreference|null
* @return ArrayPaginator|UserPreference|null
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|UserPreference|null
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ArrayPaginator|UserPreference|null
{
if ($operation instanceof CollectionOperationInterface) {
return $this->userPreferenceService->getAllUserPreferences();
$preferences = $this->userPreferenceService->getAllUserPreferences();
return new ArrayPaginator($preferences, 0, count($preferences));
}
return $this->userPreferenceService->getUserPreference($uriVariables['id'] ?? '');