1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use Closure;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use Nuwave\Lighthouse\Schema\Values\FieldValue;
8
use Nuwave\Lighthouse\Support\Contracts\ArgDirective;
9
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
10
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
11

12
class SpreadDirective extends BaseDirective implements ArgDirective, FieldMiddleware
13
{
14 1
    public static function definition(): string
15
    {
16
        return /** @lang GraphQL */ <<<'GRAPHQL'
17 1
"""
18
Merge the fields of a nested input object into the arguments of its parent
19
when processing the field arguments given by a client.
20
"""
21
directive @spread on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
22
GRAPHQL;
23
    }
24

25 1
    public function handleField(FieldValue $fieldValue, Closure $next)
26
    {
27 1
        $resolver = $fieldValue->getResolver();
28

29 1
        return $next(
30 1
            $fieldValue->setResolver(
31
                function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
32 1
                    $resolveInfo->argumentSet = $resolveInfo->argumentSet->spread();
33

34 1
                    return $resolver(
35 1
                        $root,
36 1
                        $resolveInfo->argumentSet->toArray(),
37
                        $context,
38
                        $resolveInfo
39
                    );
40
                }
41
            )
42
        );
43
    }
44
}

Read our documentation on viewing source code .

Loading