Skip to content

[2.x] Custom data provider #506

@soyuka

Description

@soyuka

This issue will lead to a documentation pull-request.

In 2.x, the data provider uses decorations. Actually, the default data provider will call it's decorated data providers and return the according method, or skip silently when a ResourceClassNotSupportedException is thrown.

Based on the CollectionDataProvider, my natural implementation (with symfony) would be to:

  1. declare a service
services:
    my_custom.collection_data_provider:
        class: 'Acme\DataProvider\BlogCollectionDataProvider'
        decorates: 'api_platform.doctrine.orm.default.collection_data_provider'
        arguments: 
            - '@doctrine'
            - '@my_custom.collection_data_provider.inner'
  1. Have a class that throws a ResourceClassNotSupportedException if the current resource class is not supported:
//skipping the class/constructor declaration which implements the CollectionDataProviderInterface

public function getCollection(string $resourceClass, string $operationName)
{
        if ($resourceClass !== 'Acme\Entity\BlogEntity') {
            throw new ResourceClassNotSupportedException();
        }

        //fetch data
}

What I don't understand, is that if my custom data provider decorates the api_platform.doctrine.orm.default.collection_data_provider, I get something like:

return $this->services['my_custom.collection_data_provider'] = new \Acme\DataProvider\BlogCollectionDataProvider(
  $a, 
  new \ApiPlatform\Core\Bridge\Doctrine\Orm\CollectionDataProvider($a, ...)
);

The default collection data provider will therefore never get any decorations, and my custom data provider will always throw an un-catched ResourceClassNotSupportedException.

Is this a matter of decoration priorities or am I doing something wrong?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions