Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL service does not support interfaces #33

Open
robertlemke opened this issue Jan 18, 2021 · 6 comments
Open

GraphQL service does not support interfaces #33

robertlemke opened this issue Jan 18, 2021 · 6 comments

Comments

@robertlemke
Copy link
Contributor

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.

@simstern
Copy link
Contributor

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.

@johannessteu
Copy link
Contributor

@robertlemke didn't we tested that also 🤔

@johannessteu
Copy link
Contributor

ping @robertlemke

@dfeyer
Copy link

dfeyer commented Nov 18, 2021

@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
Copy link

dfeyer commented Nov 18, 2021

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

@dfeyer
Copy link

dfeyer commented Nov 18, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants