Graphqlbundle: Resolve object to enum

Created on 24 Mar 2019  路  2Comments  路  Source: overblog/GraphQLBundle

| 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?

question

Most helpful comment

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()'

All 2 comments

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()'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SebastienTainon picture SebastienTainon  路  3Comments

rpander93 picture rpander93  路  3Comments

apoberez picture apoberez  路  3Comments

VincentClair picture VincentClair  路  4Comments

Vodzo picture Vodzo  路  4Comments