GraphQLBundle icon indicating copy to clipboard operation
GraphQLBundle copied to clipboard

ResolverMap is not inherited

Open akomm opened this issue 7 years ago • 2 comments

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Version/Branch dev-master

Example:

NodeInterface:
  type: 'interface'
  config:
    fields:
      id: {type: 'ID!'}

Node:
  type: 'object'
  config:
    interfaces: ['NodeInterface']
    fields:
      id: {type: 'ID!'}

Product:
  type: 'object'
  inherits: ['Node']
  config:
    fields:
      name:
        type: 'String!'
# ...

And the following ResolverMap:

<?php declare(strict_types=1);
namespace App\GraphQL\Resolver\Query;

use App\GraphQL\Relay\GlobalId;
use App\GraphQL\Relay\NodeInterface;
use Overblog\GraphQLBundle\Resolver\ResolverMap;
use Overblog\GraphQLBundle\Resolver\UnresolvableException;

final class NodeResolver extends ResolverMap
{
    protected function map()
    {
        return [
            'NodeInterface' => [
                static::RESOLVE_TYPE => function($value) {
                    if ($value instanceof NodeInterface) {
                        return $value->getTypeName();
                    }
                    throw new UnresolvableException();
                },
            ],
            'Node' => [
                'id' => function($value) {
                    if ($value instanceof NodeInterface) {
                        return GlobalId::toGlobalId($value->getTypeName(), $value->getId());
                    }
                    throw new UnresolvableException();
                },
            ],
        ];
    }
}

It is also added in the graphql extension as "resolver_map".

The RESOLVE_TYPE works, but the Node.id is not invoked and the plain ID is returned.

Contrary, the resolver is correctly inherited, when defining it using the expression language:

NodeInterface:
  type: 'interface'
  config:
    fields:
      id: {type: 'ID!'}

Node:
  type: 'object'
  config:
    interfaces: ['NodeInterface']
    fields:
      id: {type: 'ID!', resolver: '@=resolver("myresolver", [value])'}

Product:
  type: 'object'
  inherits: ['Node']
  config:
    fields:
      name:
        type: 'String!'
# ...

I started using ResolverMap instead of ResolverInterface, because it safes me implementing two interfaces to achieve the exact same result, even though it was added as a requirement for the new graphql type configuration.

akomm avatar Jan 30 '18 12:01 akomm

Thanks for feedback! Their was just not designed to work together :D

mcg-web avatar Jan 30 '18 12:01 mcg-web

I know, probably work different, the one merges config, the other just matched against on resolve? ;) But I think nothing speaks against it working in this case the same way? :)

akomm avatar Jan 30 '18 13:01 akomm