【问题标题】:how to intercept graphql request in api-platform?如何在 api-platform 中拦截 graphql 请求?
【发布时间】:2019-06-04 05:47:29
【问题描述】:
【问题讨论】:
标签:
api-platform.com
graphql-php
【解决方案1】:
我知道这有点旧,但仅供参考。
API-Platform 的graphql 推荐的方式是使用stage。见this documentation。
在发送电子邮件的情况下,您可以使用序列化阶段。请参阅下面的示例发送电子邮件以通知用户他已收到消息。
<?php
namespace App\Stage;
use ApiPlatform\Core\GraphQl\Resolver\Stage\SerializeStageInterface;
use App\Email\Mailer;
final class SerializeStage implements SerializeStageInterface
{
/**
* @var Mailer
*/
private $mailer;
private $serializeStage;
public function __construct(
SerializeStageInterface $serializeStage,
Mailer $mailer)
{
$this->serializeStage = $serializeStage;
$this->mailer = $mailer;
}
/**
* @param object|iterable|null $itemOrCollection
*/
public function __invoke($itemOrCollection, string $resourceClass, string $operationName, array $context): ?array
{
// Call the decorated serialized stage (this syntax calls the __invoke method).
$serializedObject = ($this->serializeStage)($itemOrCollection, $resourceClass, $operationName, $context);
// send notification e-mail if creating message
if ($resourceClass === 'App\Entity\Message' && $operationName === 'create') {
$this->mailer->sendMessageNotificationEmail($itemOrCollection->getReceiver());
}
return $serializedObject;
}
}
然后我们需要装饰原生舞台:
/config/services.yaml
App\Stage\SerializeStage:
decorates: api_platform.graphql.resolver.stage.serialize