nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.37k stars 439 forks source link

Allow GraphQL types named like PHP reserved keywords in federation entity resolver #2582

Open Maxwell2022 opened 4 months ago

Maxwell2022 commented 4 months ago

What problem does this feature proposal attempt to solve?

At the moment it seems that lighthouse is not able to resolve entities reference for types that are reserved PHP words out of the box.

For instance we are using this type Class in our graph which we didn't have problem with at first, but now that we need EntityReference resolver this is a problem because lighthouse is looking for a class with the same __typename which is not possible in PHP class Class {}.

Which possible solutions should be considered?

Option 1.

Entity reference resolver could be suffixed by Resolver or ReferenceResolver to allow fo this to work.

namespace App\GraphQL\ReferenceResolvers;

use Nuwave\Lighthouse\Federation\BatchedEntityResolver;

final class ClassResolver implements BatchedEntityResolver
{
...
}

Option 2.

Another solution would be to provide a mapping in the lighthouse configuration

// config/lighthouse.php

return [

   'referenceResolvers' => [
        // '__typename' => 'FQN',
        'class' => 'App\GraphQL\ReferenceResolvers\ClassResolver',
    ],

   ...
];

Option 3.

Another option would be to use a custom EntityResolverProvider (we went down this path)

$this->app->bind(EntityResolverProvider::class, CustomEntityResolverProvider::class);
spawnia commented 4 months ago

Does any passed in __typename have to be present as a type in the schema 100% of the time? If so, we could add a mapping at the schema level through a directive, such as:

type Class @class(name: "ClassType") { ... }