Wide banner showcasing playful tech illustrations, including a quirky spider-like character representing API Platform, over a gentle blue and white background.

API Platform: How to Implement Custom Collection Data Providers with Filters?

03/03/2023

In most cases, when you are trying to filter data from your endpoint, default ApiPlatform filters are more than enough to cover all your needs. Sometimes though, additional actions are needed before your collection can be returned to the world.

How to Implement Custom Collection Data Providers with Filters?

This article explains how to implement a custom collection data provider with filters in API Platform version 2.7.4 and is a part of our series of articles on API Platform which you can find on our blog.

Imagine the following scenario: Let's say we have a collection named transactions. We already created endpoints to fetch data GET /api/transactions:

collectionOperations={
    "get"={
        "normalization_context"={"groups"={"transaction:read"}}
    },
}

We also have some filters; for example, employee exact.

>@ApiFilter(SearchFilter::class, properties={"employee"="exact"})

By default when you will call your endpoint with an additional param in the query string project=1, your list will be filtered by only these transactions that are related to a specific project - in this example id 1. The problem starts when you want to do something extra in the DataProvider in this collection. For example, let's filter collection in a data provider based on logged users. If a user has a specific role, then they see all transactions, if they’re a "normal" user then they can only see transactions related to his user.

public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
    $queryBuilder = $this->managerRegistry->getRepository(Transaction::class)->createQueryBuilder('t');

    if (!$this->security->isGranted('ROLE_MANAGER')) {
        $queryBuilder
            ->andWhere('ewh.employee = :employee')
            ->setParameter('employee', $this->security->getUser())
        ;
    }

    return $queryBuilder->getQuery()->getResult();
}

However, what’s important to know is that this will remove default filters. Hence, you will not be able to filter by project anymore. What’s the solution?

To force DataProvider to use default filters. In order to do that, we need to inject extensions collection from the api platform.

public function __construct(
    private object $collectionExtensions,
    private ManagerRegistry $managerRegistry,
    private Security $security,
)
{}

Next, we need to inject a specific collection into our service.

App\DataProvider\TransactionsDataProvider:
    tags:
        - { name: 'api_platform.collection_data_provider', priority: 3 }
    autoconfigure: false
    arguments:
        $collectionExtensions: !tagged api_platform.doctrine.orm.query_extension.collection

From this moment forward, we will have all extensions available in our service. Some of them are filters, other ones could be paginations. Now, we can use them to filter our Query by additional criteria - by project in our example. To do that let's use the following code where we are applying each extension.

$queryNameGenerator = new QueryNameGenerator();

/** @var QueryCollectionExtensionInterface $extension */
foreach ($this->collectionExtensions as $extension) {
    $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);

    if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
        return $extension->getResult($queryBuilder);
    }
}

Now, we can finally call our endpoint to check if everything works well. All class including all previous code snippets is available here:

<?php

declare(strict_types=1);

namespace App\DataProvider;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Transaction;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Security;

final class TransactionsDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private ManagerRegistry $managerRegistry;

    private Security $security;

    private object $collectionExtensions;

    public function __construct(
        private object $collectionExtensions,
        private ManagerRegistry $managerRegistry,
        private Security $security,
    )
    {}

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

    public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
    {
        $queryBuilder = $this->managerRegistry->getRepository(Transaction::class)->createQueryBuilder('t');

        if (!$this->security->isGranted('ROLE_MANAGER')) {
            $queryBuilder
                ->andWhere('t.employee = :employee')
                ->setParameter('employee', $this->security->getUser())
            ;
        }

        $queryNameGenerator = new QueryNameGenerator();

        /** @var QueryCollectionExtensionInterface $extension */
        foreach ($this->collectionExtensions as $extension) {
            $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);

            if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
                return $extension->getResult($queryBuilder);
            }
        }

        return $queryBuilder->getQuery()->getResult();
    }
}

Hope this helps!

Ma
Wizerunek Marka Krokwy, CEO Primotly, pewnie pozującego ze skrzyżowanymi rękami. Marek ma na sobie ciemną koszulę z kołnierzykiem i okulary, co prezentuje elegancki i przystępny wygląd na czystym białym tle
Marek Krokwa
CEO

Najnowsze artykuły

Ilustracja do artykułu o NLP (przetwarzaniu języka naturalnego)

Development | 11/10/2024

Czym jest przetwarzanie języka naturalnego (NLP)?

Łukasz Kopaczewski

Rozwiązania NLP stają się coraz bardziej popularne, ponieważ sztuczna inteligencja ewoluuje i znajduje zastosowania w różnych branżach. Od przekształcania obsługi klienta w sklepach internetowych dzięki natychmiastowym odpowiedziom po zapewnianie dokładnych tłumaczeń językowych, NLP zmienia zasady gry w wielu obszarach.

Szczegóły
Obrazek nawiązujący do aspektów ESG i etyki AI.

Innovations | 09/10/2024

Etyka AI w ESG - Odpowiedzialna innowacja

Bernhard Huber

Połączenie sztucznej inteligencji i strategii ESG stwarza zarówno możliwości, jak i dylematy etyczne. Z jednej strony sztuczna inteligencja może być potężnym narzędziem do realizacji celów zrównoważonego rozwoju, optymalizacji wykorzystania zasobów i wspierania odpowiedzialnych praktyk biznesowych.

Szczegóły
Podgląd pojedynczego artykułu

Innovations | 03/10/2024

Chatbot AI - wirtualny asystent do zautomatyzowanej obsługi klienta

Agata Pater

Posiadanie asystenta opartego na sztucznej inteligencji stało się naturalną częścią naszego codziennego życia - od sterowania urządzeniami domowymi i robienia zakupów po zarządzanie naszymi finansami. Szybko się do nich przyzwyczailiśmy, ponieważ używają naturalnego języka, są intuicyjne w obsłudze i wspierają nasze codzienne zadania z niezwykłą dokładnością. Ale jak dokładnie działają i jak możemy w pełni wykorzystać ich potencjał?

Szczegóły