api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.43k stars 866 forks source link

Idea : Messager in operations #3836

Closed nicodmf closed 3 years ago

nicodmf commented 3 years ago

As messager requests are mainly linked to a resource (ex : in the doc, the request password is clearly relative to an user), it could be a good idea to defined messager directly in a operation not only for POST method but too for DELETE, PATCH, PUT. In this context, the resource could be better defined, as an action on a resource should be a part of this resource. This feature permits too to not use custom controllers.

Exemple :

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource(
 *     itemOperations={
 *         "get"={},
 *         "put"={},
 *         "promote"={
 *             "method"="POST",
 *             "path"="/users/{id}/action",
 *             "input"=UserPromoteInput::class,
 *             "messager"="input",
 *         },
 *         "purge"={
 *             "method"="DELETE",
 *             "path"="/dossiers/purge",
 *             "input"=UserPurgeInput::class,
 *             "output"=false,
 *             "messager"="input",
 *         },
 *     }
 * )
 * @ORM\Entity()
 */
class User
{
   //...
}

The handler could be finded by recognise the input.

Maybe this kind of definition is possible now. But i don't find how.

soyuka commented 3 years ago

are you talking about messenger? Yes it's available per-operation IIRC.

https://api-platform.com/docs/core/messenger/#dispatching-a-resource-through-the-message-bus

nicodmf commented 3 years ago

It is not indicated in the documentation, but, this works for a builtin post action. For an item action, in my test a patch operation, the handler isn't dispatched. Another impossible case : a delete on collection which by exemple delete all order begin and not ended in the last year : the delete on collection demand a controller and a route, and the handler isn't called at least in my tests.