Skip to content

Hydration hooks ​

GraphQL ORM provides two lifecycle hooks that run around the hydration of an entity.

HookRunsArgumentsUse case
#[BeforeHydrate]Before field assignmentarray $data (raw GraphQL data)Capture unmapped fields, raw metadata
#[AfterHydrate]After field assignmentnoneCompute virtual fields, post-processing

#[BeforeHydrate] ​

Called just before fields are assigned to the entity. Receives the raw GraphQL data array, making it possible to capture fields that are not mapped (e.g. __typename, pagination metadata).

php
use GraphqlOrm\Attribute\BeforeHydrate;
use GraphqlOrm\Mapping\Entity;
use GraphqlOrm\Mapping\Field;
use GraphqlOrm\Mapping\Identifier;

#[Entity(name: 'products', repositoryClass: ProductRepository::class)]
class Product
{
    #[Field(mappedFrom: 'id')]
    #[Identifier]
    public int $id;

    #[Field(mappedFrom: 'name')]
    public string $name;

    // Populated from raw data — not a mapped field
    public string $graphqlType = '';

    /**
     * @param array<string, mixed> $data
     */
    #[BeforeHydrate]
    public function onBeforeHydrate(array $data): void
    {
        $this->graphqlType = $data['__typename'] ?? 'unknown';
    }
}

Rules ​

  • The method must be public and accept a single array parameter
  • Multiple #[BeforeHydrate] methods are supported — all are called
  • Always runs when the entity is first created — no partial hydration check
  • At the time the hook runs, no fields are assigned yet

#[AfterHydrate] ​

Called after all fields have been assigned. Use it to compute virtual fields or run any post-hydration logic.

php
use GraphqlOrm\Attribute\AfterHydrate;
use GraphqlOrm\Mapping\Entity;
use GraphqlOrm\Mapping\Field;
use GraphqlOrm\Mapping\Identifier;

#[Entity(name: 'tasks', repositoryClass: TaskRepository::class)]
class Task
{
    #[Field(mappedFrom: 'id')]
    #[Identifier]
    public int $id;

    #[Field(mappedFrom: 'title')]
    public string $title;

    #[Field(mappedFrom: 'dueDate')]
    public ?\DateTimeImmutable $dueDate = null;

    // Virtual field — not in the GraphQL schema
    public bool $isOverdue = false;

    #[AfterHydrate]
    public function compute(): void
    {
        $this->isOverdue = $this->dueDate !== null
            && $this->dueDate < new \DateTimeImmutable();
    }
}

Rules ​

  • The method must be public and take no arguments
  • Multiple #[AfterHydrate] methods are supported — all are called
  • Skipped if any mapped field (#[Field]) is not initialized — this happens on partial hydration (e.g. a nested relation with only a subset of fields selected)
  • Virtual fields (no #[Field]) are ignored in this check — they are meant to be set by the hook

Partial hydration ​

When a relation is loaded with only a subset of fields, #[AfterHydrate] is skipped to avoid accessing uninitialized properties:

graphql
query {
  users {
    items {
      id
      tasks {
        items {
          id   # only id selected — AfterHydrate on Task is skipped
        }
      }
    }
  }
}

Using both hooks together ​

The two hooks can coexist on the same entity. Execution order is always: BeforeHydrate → field assignment → AfterHydrate.

php
#[Entity(name: 'products', repositoryClass: ProductRepository::class)]
class Product
{
    #[Field(mappedFrom: 'id')]
    #[Identifier]
    public int $id;

    #[Field(mappedFrom: 'price')]
    public float $price;

    #[Field(mappedFrom: 'taxRate')]
    public float $taxRate;

    public string $graphqlType = '';
    public float $priceWithTax = 0.0;

    /**
     * @param array<string, mixed> $data
     */
    #[BeforeHydrate]
    public function captureMetadata(array $data): void
    {
        $this->graphqlType = $data['__typename'] ?? 'unknown';
    }

    #[AfterHydrate]
    public function computePrices(): void
    {
        $this->priceWithTax = $this->price * (1 + $this->taxRate / 100);
    }
}

Released under the MIT License.