Skip to content

feat(state): resolve resource objects for purge and Mercure - #8868

Open
soyuka wants to merge 3 commits into
api-platform:mainfrom
soyuka:feat/runtime-resources
Open

soyuka wants to merge 3 commits into
api-platform:mainfrom
soyuka:feat/runtime-resources

Conversation

@soyuka

@soyuka soyuka commented Nov 24, 2026 •

Copy link
Copy Markdown
Member
Q A
Branch? main
Tickets ~
License MIT
Doc PR ~

When one persisted object backs several resources (stateOptions, ObjectMapper #[Map]), the Doctrine listeners only know about the entity class. HTTP cache purge and Mercure then either skip the resources or publish the entity itself.

ResourceObjectResolverInterface turns a persisted object into the resource objects that expose it:

interface ResourceObjectResolverInterface
{
    /** @return iterable<object> */
    public function resolve(object $data): iterable;
}

The default api_platform.state.resource_object_resolver returns the object itself when it is a resource. Otherwise it maps the object to each #[Map] target. The purge listener uses it, and now also purges collections that have uri variables (subresources). Mercure uses it when you opt in:

api_platform:
    mercure:
        resolve_resource_objects: true

When the target depends on the data, decorate it:

#[AsDecorator('api_platform.state.resource_object_resolver')]
final readonly class ProductResourceObjectResolver implements ResourceObjectResolverInterface
{
    public function __construct(
        private ResourceObjectResolverInterface $inner,
        private ObjectMapperInterface $objectMapper,
    ) {
    }

    public function resolve(object $data): iterable
    {
        if (!$data instanceof ProductEntity) {
            return $this->inner->resolve($data);
        }

        return [$this->objectMapper->map($data, $data->isBundle ? Bundle::class : Product::class)];
    }
}

Updating a ProductEntity now purges /bundles/1 (or /products/1) and its collection, and publishes the Mercure update on that resource's topic.

Part of the runtime-defined resources work, the rest is split in:

@soyuka
soyuka force-pushed the feat/runtime-resources branch 2 times, most recently from 1d3f9e2 to 1f44711 Compare November 24, 2026 13:10
@soyuka soyuka added this to the 5.1 milestone Nov 24, 2026
An entity behind N resources (stateOptions) only purged the entity
class tags. Collections with uri variables (/parents/{id}/children)
were never purged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant