Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion docs/strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Hydrator Strategies
Some hydrator strategies are supplied with this library. You may also add your own hydrator
strategies.

All strategies are in the namespace ``ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\Strategy``
Included strategies are in the namespace ``ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\Strategy``

FieldDefault
============
Expand All @@ -31,6 +31,66 @@ ToBoolean
Similar to ``ToInteger``, this will convert the field value to a boolean to be handled as a boolean internal to PHP.


Add a custom hydrator strategy
==============================

To add a custom hydrator strategy, create a class that implements the interface
``Laminas\Hydrator\Strategy\StrategyInterface``. Add the class to the
hydrator strategy container after creating the driver.

.. code-block:: php

use ApiSkeletons\Doctrine\ORM\GraphQL\Driver;
use ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\HydratorContainer;
use App\GraphQL\Hydrator\Strategy\S3Url;

$driver = new Driver($entityManager);

$driver->get(HydratorContainer::class)
->set(S3Url::class, static fn () => new S3Url());

The S3Url class would look something like this:

.. code-block:: php

namespace App\GraphQL\Hydrator\Strategy;

use Illuminate\Support\Facades\Storage;
use Laminas\Hydrator\Strategy\StrategyInterface;

/**
* Resolve the token to an S3 url
*/
class S3Url implements
StrategyInterface
{
public function extract(mixed $value, object|null $object = null): mixed
{
if (! $value) {
return $value;
}

return Storage::disk('s3')->url($value);
}

/**
* This library does not hydrate using the hydrator but this method is required
* @param mixed[]|null $data
*/
public function hydrate(mixed $value, array|null $data): mixed
{
return $value;
}
}

Then add the hydratorStrategy to the entity field you wish to custom extract.

.. code-block:: php

#[GraphQL\Field(hydratorStrategy: S3Url::class)]
#[ORM\Column(type: "text", nullable: true)]
public $favicon;

.. role:: raw-html(raw)
:format: html

Expand Down
Loading