graphql icon indicating copy to clipboard operation
graphql copied to clipboard

GraphQL service does not support interfaces

Open robertlemke opened this issue 5 years ago • 6 comments

As far as I can see – and I tried and played with this for a couple of hours – interfaces are not yet supported.

My concrete use case is implementing the server specification for Relay, which enables me to provide cursor-based pagination and universal object identification.

Given the following minimal GraphQL schema:

interface Node {
    id: ID!
}

type CustomerAccount implements Node {
    id: ID!
    customerName: String!
}

type Query {
    node(
        id: ID!
    ): Node
}

resolveType

When I submit any query, I will receive an exception:

Type "Node" is missing a "__resolveType" resolver.

Pass false into "resolverValidationOptions.requireResolversForResolveType" to disable this warning.

GraphQLTools\Generate\SchemaError
Packages/Libraries/t3n/graphql-tools/src/Generate/CheckForResolveTypeResolver.php 34

Just for good measure, I tried setting that validation option to false, but there is currently no way to do that in the Flow package (it is possible programmatically in the GraphQLTools package).

To fix this, we need a way to provide a custom resolveType function. Ideally, Flow would automatically recognize such a function in my Resolver class.

internal error in response

I temporarily solved the first problem by hardcoding the resolve type as follows:

namespace GraphQLTools\Generate;
…
class CheckForResolveTypeResolver
{
    /**
     * @throws SchemaError
     */
    public static function invoke(Schema $schema, ?bool $requireResolversForResolveType = null) : void
    {
        /**
         * @var UnionType|InterfaceType $typeName
         */
        foreach ($schema->getTypeMap() as $typeName => $type) {
            if (! ($type instanceof UnionType || $type instanceof InterfaceType)) {
                continue;
            }

            if (!isset($type->config['resolveType'])) {
                $type->config['resolveType'] = function () {
                    return 'CustomerAccount';
                };
            }
…
        }
    }
}

Now the schema warning is gone.

When I try to query a node as follows:

query Query {
  node(id: "1234") {
    id
  }
}

I receive a inexpressive error:

{
  "errors":
   [
     {"message":"Internal server error",
      "extensions":
        {"category":"internal"},
      "locations":[
         {"line":2,"column":5}
       ],
       "path":["node"]}
     ],
     "data":{"node":null}
}

In the resolver, I tried solving the issue by returning different kinds of data. Among them, a CustomerAccount class (which supports JsonSerialize), an array based on CustomerAccount, a literal, an array with just an id:

    /**
     * @param $_
     * @param array $arguments
     */
    public function node($_, array $arguments)
    {
        return [
            'id' => '1234'
        ];
    }

I'm a bit clueless about what the correct data would be to return in the query function.

robertlemke avatar Jan 18 '21 07:01 robertlemke

I got help on this one recently from @johannessteu , and I still owe an update on the readme on how to do that :-)

You have to simply implement a NodeResolver implenting the __resolveType($entity) function to return the type.

For your case that would look something like this:

class NodeResolver implements ResolverInterface
{
    public function __resolveType(entity): string
    {
        // TOOD: return different type names for different implementations
        return 'CustomerAccount';
    }
}

Hope that helps.

simstern avatar Jan 18 '21 14:01 simstern

@robertlemke didn't we tested that also 🤔

johannessteu avatar Jan 27 '21 07:01 johannessteu

ping @robertlemke

johannessteu avatar Aug 26 '21 12:08 johannessteu

@johannessteu @robertlemke I have the same error ...

final class VideoResolver implements ResolverInterface
{
    public function __resolveType($entity): string
    {
        return 'Movie';
    }
}

Error: Type "Video" is missing a "__resolveType" resolver

dfeyer avatar Nov 18 '21 13:11 dfeyer

It was a configuration my resolver was not loaded correctly, everything works fine now

dfeyer avatar Nov 18 '21 16:11 dfeyer

final class VideoResolver implements ResolverInterface
{
    public function __resolveType($data, $context, ResolveInfo $info): ?string
    {
        return match (get_class($data)) {
            MovieCollection::class => 'MovieCollection',
            Movie::class => 'Movie',
            TvShow::class => 'TvShow',
            TvShowSeason::class => 'TvShowSeason',
            TvShowEpisode::class => 'TvShowEpisode',
            default => null
        };
    }
}

My final resolver looks like this

dfeyer avatar Nov 18 '21 16:11 dfeyer