how to apply middleware to grouped queries or mutations in nuwave/lighthouse version 5

Viewed 245

Before in lighthouse 3 and 4, I can do something like this

extend type Mutation @group(
    namespace: "Api\\Jobs\\Resolvers",
    middleware: ["verified"]
) {
    createJob(
      payload: CreateJobInput
    ): Job @field(resolver: "Api\\Jobs\\Resolvers\\JobResolver@create")

    updateJob(
      payload: UpdateJobInput
    ): Job @field(resolver: "Api\\Jobs\\Resolvers\\JobResolver@update")

    ...
}

But in lighthouse 5, @group and @middleware directives are removed. I tried to create custom directive on OBJECT like this but it didn't work

namespace App\GraphQL\Directives;

use ...

final class VerifiedDirective extends BaseDirective implements FieldMiddleware, TypeManipulator
{
    public function handleField(FieldValue $fieldValue, Closure $next) 
    {
        // verify
        return $next($fieldValue);
    }

    public static function definition(): string
    {
        return /** @lang GraphQL */ <<<'GRAPHQL'
            directive @verified on OBJECT
        GRAPHQL;
    }

    // I copied from other OBJECT directive
    public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefinitionNode &$typeDefinition): void
    {
        ASTHelper::addDirectiveToFields($this->directiveNode, $typeDefinition);
    }
}

And my graphql file is now

extend type Mutation @namespace(field: "Api\\Companies\\Resolvers") @verified {
    ...
}

But it said that No directive found for "verified".

The lighthouse 5 docs says that I should use FieldMiddleware instead, but I don't want to add that directive to every field. Is there any way I can apply middlewares to grouped mutations and queries?

Thanks in advance.

0 Answers
Related