graphql-doctrine icon indicating copy to clipboard operation
graphql-doctrine copied to clipboard

Support Embeddable classes

Open popovserhii opened this issue 5 years ago • 2 comments

Doctrine 2 has a great feature named as Embeddable classes. I try to use it in the pair with graphql-doctrine and got error No type registered with key Position. Either correct the usage, or register it in your custom types container when instantiating GraphQL\Doctrine\Types, where Position is a simple object

namespace Stagem\Product\Model\Monitor;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Embeddable
 */
class Position
{
    /**
     * @ORM\Column(type="integer")
     */
    private $line = 1;

    /**
     * @ORM\Column(type="integer")
     */
    private $column = 1;
    // ...
}

and RankMonitor model

namespace Stagem\Product\Model;

use Doctrine\ORM\Mapping as ORM;
use GraphQL\Doctrine\Annotation as API;
use Stagem\Product\Model\Monitor\Position;

/**
 * @ORM\Entity()
 * @ORM\Table(name="product_rank_monitor")
 */
class RankMonitor
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", options={"unsigned":true})
     */
    private $id;

    /**
     * @var Position
     * @ORM\Embedded(class="Stagem\Product\Model\Monitor\Position", columnPrefix="position_")
     */
    private $position;
    // ...
}

Do you plan to add native support for Embeddable classes or maybe can explain how to implement this feature?

popovserhii avatar Jun 18 '19 08:06 popovserhii

I was not aware of the Embeddable feature. That sounds like it could be quite useful for some our use-cases as well, (storing money with value and currency, or time and timezone). While I do not have plan to implement it right now, it could be implemented some day if we end up using it.

In the meantime, I assume you could workaround the limitation by implementing a custom GraphQL type and declaring it when creating an instance of Types.

As I don't have experience working with Embeddable I cannot give good advice on how to implement it properly. Maybe the workaround is enough ? Maybe there could be a way to automate it in a similar way that we do for entities ?

PowerKiKi avatar Jun 19 '19 22:06 PowerKiKi

Implementing a custom GraphQL type require much manual work which is redundant when there are many Embeddable entities that's why I discarded from it, but automation is the exactly what many will expect.

popovserhii avatar Jun 30 '19 08:06 popovserhii