| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | maybe?
| BC Break report? |no
| Version/Branch | dev-master
In our application, we create Enum's as objects:
<?php
final class Action
{
public const ACTION_NONE = 'none';
public const ACTION_DELETE = 'delete';
private $action;
public function __construct(string $action)
{
$this->action = $action;
}
public function getAction() : string
{
return $this->action;
}
}
Our entities and models can then return an instance of Action. For example new Action(Action::ACTION_DELETE)
What I would like is that whenever GraphQL stumbles upon an object of type Action, that it takes the getAction() : string and uses that to represent a GraphQL Enum.
This is my Enum:
Action:
type: enum
config:
values:
NONE: 'none'
DELETE: 'delete'
I tried to add isTypeOf, and resolve but every time I get this error:
Expected a value of type \"Action\" but received: instance of My\\App\\Action
Am I missing something?
Turns out it's possible to override the serialize like this:
public function serialize($value)
{
if ($value instanceof Action) {
$value = $value->getAction();
}
return parent::serialize($value);
}
If you do not want the serialize method, you could maybe do something like this too:
SomeType:
type: object
config:
fields:
action:
type: Action!
resolve: '@=value.action.getAction()'
Most helpful comment
If you do not want the serialize method, you could maybe do something like this too: