Skip to content

Events ​

Each query dispatches events through the Symfony event dispatcher, to plug metrics, tracing or alerting without decorating the GraphQL client. The entities written by flush() dispatch their own.

EventDispatchedTrace content
QueryStartedEventbefore the query is sentid, query, variables, AST
QueryExecutedEventonce the response is hydrated+ endpoint, duration, response size, hydration stats
QueryFailedEventwhen sending or hydrating fails, before rethrowas available at the failure, + $exception

All of them extend QueryEvent, which holds the trace (a GraphqlQueryTrace). They are dispatched under their own class name: a listener of QueryEvent itself is never called, so register one method for each event, or one method typed QueryEvent several times, e.g. #[AsEventListener(event: QueryExecutedEvent::class)] and #[AsEventListener(event: QueryFailedEvent::class)]. The trace->id is shared by the events of a same query, e.g. to match the start and the end of a tracing span.

php
use GraphqlOrm\Event\QueryExecutedEvent;
use GraphqlOrm\Event\QueryFailedEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final readonly class GraphqlMetricsListener
{
    public function __construct(private MetricsClient $metrics) // your own metrics client
    {
    }

    #[AsEventListener]
    public function onExecuted(QueryExecutedEvent $event): void
    {
        $this->metrics->timing('graphql.query', $event->trace->duration, ['endpoint' => $event->trace->endpoint]);
    }

    #[AsEventListener]
    public function onFailed(QueryFailedEvent $event): void
    {
        $this->metrics->increment('graphql.failure', ['exception' => $event->exception::class]);
    }
}

The events are read-only: the query cannot be changed from a listener. Query values are masked in the trace, as in the logs, unless redact_query_values is disabled (see logging), so that a listener exporting it does not leak personal data.

Writing entities ​

flush() dispatches the lifecycle events of the entities it writes, as Doctrine:

EventDispatched
PrePersistEventby persist() on a new entity
PreRemoveEventby remove() on an entity a query read
PreFlushEventwhen flush() starts, before it looks for the entities to write
PreUpdateEventbefore updating an entity, with its changedProperties
PostPersistEventonce the entity is created, its identifier set
PostUpdateEventonce the entity is updated
PostRemoveEventonce the entity is deleted
PostFlushEventonce every entity is written, even when there was none

The events of an entity extend LifecycleEvent, holding the entity and the manager; PreFlushEvent and PostFlushEvent hold the manager. What a listener of PrePersistEvent, PreFlushEvent or PreUpdateEvent sets on an entity is written:

php
use GraphqlOrm\Event\PreUpdateEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
public function onPreUpdate(PreUpdateEvent $event): void
{
    if ($event->entity instanceof Task && \in_array('done', $event->changedProperties, true)) {
        $event->entity->doneAt = new \DateTimeImmutable();
    }
}

A post event of a failed mutation is not dispatched, nor PostFlushEvent when flush() throws. Calling flush() from a listener of the flush throws a LogicException: call it once the flush is done, from a PostFlushEvent listener.

Lifecycle callbacks ​

An entity reacts to its own lifecycle with public methods without parameter, marked as Doctrine lifecycle callbacks, called before the listeners of the same step:

php
use GraphqlOrm\Attribute\PrePersist;
use GraphqlOrm\Attribute\PreUpdate;

#[GraphqlOrm\Entity(name: 'tasks')]
class Task
{
    #[GraphqlOrm\Field(mappedFrom: 'updated_at')]
    public ?\DateTimeImmutable $updatedAt = null;

    #[PrePersist]
    #[PreUpdate]
    public function touch(): void
    {
        $this->updatedAt = new \DateTimeImmutable();
    }
}

The attributes are #[PrePersist], #[PostPersist], #[PreUpdate], #[PostUpdate], #[PreRemove] and #[PostRemove], in the GraphqlOrm\Attribute namespace as the hydration hooks.

Released under the MIT License.