# Field Directives

Field directives can be applied to any FieldDefinition (opens new window)

# FieldResolver

Perhaps the most important directive interface, a \Nuwave\Lighthouse\Support\Contracts\FieldResolver (opens new window) lets you add a resolver for a field through a directive.

It can be a great way to reuse resolver logic within a schema.

# FieldMiddleware

A \Nuwave\Lighthouse\Support\Contracts\FieldMiddleware (opens new window) directive allows you to wrap around the field resolver, just like Laravel Middleware (opens new window).

You may use it to handle incoming values before reaching the final resolver as well as the outgoing result of resolving the field.

<?php

namespace App\GraphQL\Directives;

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class ExampleDirective extends BaseDirective implements FieldMiddleware, DefinedDirective
{
    public static function definition(): string
    {
        return /** @lang GraphQL */ <<<'GRAPHQL'
directive @example on FIELD_DEFINITION
GRAPHQL;
    }

    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
    {
        $resolver = $fieldValue->getResolver();

        $fieldValue->setResolver(function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
            // Do something before the resolver, e.g. validate $args, check authentication

            // Call the actual resolver
            $result = $resolver($root, $args, $context, $resolveInfo);

            // Do something with the result, e.g. transform some fields

            return $result;
        });

        // Keep the chain of adding field middleware going by calling the next handler.
        // Calling this before or after ->setResolver() allows you to control the
        // order in which middleware is wrapped around the field.
        return $next($fieldValue);
    }
}

# FieldManipulator

An \Nuwave\Lighthouse\Support\Contracts\FieldManipulator (opens new window) directive can be used to manipulate the schema AST.

# ValidationDirective

This directive type is implemented as an abstract class rather then a pure interface and allows you to define complex validation rules for a field with ease.

Read more about it in the Validation section.