Return custom collection in graphQL with Api Platform using custom data provider

Return custom collection in graphQL with Api Platform using custom data provider


0

I have a custom DTO class, and I want to return a collection of that using graphQL. With REST it works fine. I’m using Api Platform 2.6 and PHP 8.2
There is my DTO class :

<?php

declare(strict_types=1);

namespace AppDto;

use ApiPlatformCoreAnnotationApiProperty;
use ApiPlatformCoreAnnotationApiResource;

#[ApiResource(
    collectionOperations: [
        'get' => [
            'method' => 'GET',
            'path' => '/settings',
        ],
    ],
    itemOperations: [
        'get' => [
            'method' => 'GET',
            'path' => '/settings/{key}',
        ],
    ],
    routePrefix: '/admin',
)]
final class SettingDto
{
    #[ApiProperty(identifier: true)]
    public string $key;

    public string $value;
}

There is my custom data provider :

<?php

declare(strict_types=1);

namespace AppApiDataProvider;

use ApiPlatformCoreDataProviderCollectionDataProviderInterface;
use ApiPlatformCoreDataProviderItemDataProviderInterface;
use ApiPlatformCoreDataProviderRestrictedDataProviderInterface;
use AppDtoSettingDto;
use SymfonyComponentDependencyInjectionParameterBagParameterBagInterface;
use SymfonyComponentPropertyAccessPropertyAccessorInterface;

final readonly class SettingDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
{
    public function __construct(
        private ParameterBagInterface $parameterBag,
        private PropertyAccessorInterface $propertyAccessor,
    )
    {}

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return is_a($resourceClass, SettingDto::class, true);
    }

    /**
     * @inheritDoc
     */
    public function getCollection(string $resourceClass, string $operationName = null): array
    {
        return array_map(static function ($key, $value) {
            $settingDto = new SettingDto();
            $settingDto->key = $key;
            $settingDto->value = $value;

            return $settingDto;
        }, array_keys($this->parameterBag->get('exposed.parameters')), $this->parameterBag->get('exposed.parameters'));
    }

    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?SettingDto
    {
        $setting = $this->propertyAccessor->getValue($this->parameterBag->get('exposed.parameters'), "[$id]");
        if (null === $setting) {
            return null;
        }

        $settingDto = new SettingDto();
        $settingDto->key = $id;
        $settingDto->value = $setting;

        return $settingDto;
    }
}

It works fine using REST but when I try to use graphQL I have this error :

Collection returned by the collection data provider must implement ApiPlatform\Core\DataProvider\PaginatorInterface or ApiPlatform\Core\DataProvider\PartialPaginatorInterface

I have also tried to use custom resolver like mentioned here instead but it’s not working either. Any ideas?


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *