如何在 API 平台中拦截 GraphQL 请求



我正在使用 API 平台 2.3.5,但我找不到拦截 graphQl 请求的方法。

的意思是,假设我正在进行更改(更新),并且还想记录数据或发送电子邮件。我该怎么做?

我确实阅读了 api-platform 文档,但关于他们对 graphQl 的实现知之甚少。它自动做了很多事情。

事件尚未实现 (https://github.com/api-platform/core/pull/2329)

我也发现了这个 - https://github.com/api-platform/core/blob/master/src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php#L101但我宁愿不碰它。有没有更简单的方法?

我知道

这有点旧,但如果只是供参考。

API 平台graphql的推荐方法是使用 stage。请参阅此文档。

在发送电子邮件的情况下,您可以使用序列化阶段。请参阅下面的示例,了解如何发送电子邮件以通知用户他已收到消息。

<?php
namespace AppStage;
use ApiPlatformCoreGraphQlResolverStageSerializeStageInterface;
use AppEmailMailer;
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 === 'AppEntityMessage' && $operationName === 'create') {
        $this->mailer->sendMessageNotificationEmail($itemOrCollection->getReceiver());
    } 
    return $serializedObject;
}
}

然后我们需要装饰原生阶段:

/

config/services.yaml

    AppStageSerializeStage:
    decorates: api_platform.graphql.resolver.stage.serialize

最新更新