1

我正在使用 api-platform 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 但我不想碰它。有没有更简单的方法?

4

1 回答 1

1

我知道这有点旧,但仅供参考。

API-Platform 的推荐方法graphql是使用 stage。请参阅本文档

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

<?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
于 2019-12-28T10:46:40.850 回答