Skip to content

Change default-resolver lookup according to @namespace directive #2405

@DelDaGhost

Description

@DelDaGhost

What problem does this feature proposal attempt to solve?

When creating a big graph I would like to split the graph into multiple-files.
In order to also keep the resolvers of this graph clean, I would like to have them sorted in multiple folders.

This is currently possible by using the @field-directive on each field(query/mutation).
But it is a bit of repetitive code if you strictly follow the same structure.

Which possible solutions should be considered?

When the default lookup of the resolver also takes the @namespace-directive into account it would be possible to just declare the namespace and still use the default-lookup of the resolvers.
Maybe there would also be a way to define the default-path or better yet a postfix for the default-path for a whole graphql-file since then it would also work for validators?

Example:

extend type Mutation @namespace(field:"App\\GraphQL\\Mutations\\User") {
    registerUser(input: RegisterUserInput!): User!
}

Then lighthouse would look for 'App\GraphQL\Mutations\User\RegisterUser.php' instead of 'App\GraphQL\Mutations\User\RegisterUser.php'.

I've currently overwriten the ResolverProvider in my project in order to achieve this.
Thx @thekonz for pointing me in the right direction here :)

Code-Example:

namespace App\Extension\Nuwave\Lighthouse\Schema;

use Illuminate\Support\Str;
use Nuwave\Lighthouse\Schema\ResolverProvider;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Utils;

class CustomResolverProvider extends ResolverProvider
{
    protected function findResolverClass(FieldValue $fieldValue, string $methodName): ?string
    {
        $directives = $fieldValue->getField()->directives;

        foreach ($directives as $directive) {
            if ($directive->name->value === 'namespace') {
                return Utils::namespaceClassname(
                    Str::studly($fieldValue->getFieldName()),
                    [$directive->arguments[0]->value->value],
                    static fn (string $class): bool => method_exists($class, $methodName),
                );
            }
        }
        
        return parent::findResolverClass($fieldValue, $methodName);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionRequires input from multiple people

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions