1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use Closure;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use Illuminate\Support\Collection;
8
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;
9
use Nuwave\Lighthouse\Schema\Values\FieldValue;
10
use Nuwave\Lighthouse\Support\Contracts\ArgDirective;
11
use Nuwave\Lighthouse\Support\Contracts\ArgDirectiveForArray;
12
use Nuwave\Lighthouse\Support\Contracts\Directive;
13
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
14
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
15
use Nuwave\Lighthouse\Support\Utils;
16

17
abstract class ArgTraversalDirective extends BaseDirective implements FieldMiddleware
18
{
19 1
    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
20
    {
21 1
        $resolver = $fieldValue->getResolver();
22

23 1
        return $next(
24 1
            $fieldValue->setResolver(
25
                function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
26 1
                    $resolveInfo->argumentSet = $this->transformRecursively($resolveInfo->argumentSet);
27

28 1
                    return $resolver(
29 1
                        $root,
30 1
                        $resolveInfo->argumentSet->toArray(),
31
                        $context,
32
                        $resolveInfo
33
                    );
34
                }
35
            )
36
        );
37
    }
38

39 1
    public function transformRecursively(ArgumentSet $argumentSet): ArgumentSet
40
    {
41 1
        foreach ($argumentSet->arguments as $argument) {
42 1
            $directivesForArray = $argument->directives->filter(
43 1
                Utils::instanceofMatcher(ArgDirectiveForArray::class)
44
            );
45 1
            $argument->value = $this->transform($argument->value, $directivesForArray);
46

47 1
            $directivesForArgument = $argument->directives->filter(
48 1
                Utils::instanceofMatcher(ArgDirective::class)
49
            );
50

51 1
            $argument->value = Utils::applyEach(
52
                function ($value) use ($directivesForArgument) {
53 1
                    if ($value instanceof ArgumentSet) {
54 1
                        $value = $this->transform($value, $directivesForArgument);
55

56 1
                        return $this->transformRecursively($value);
57
                    } else {
58 1
                        return $this->transform($value, $directivesForArgument);
59
                    }
60
                },
61 1
                $argument->value
62
            );
63
        }
64

65 1
        return $argumentSet;
66
    }
67

68
    /**
69
     * @param  mixed  $value The client given value
70
     * @return mixed The transformed value
71
     */
72 1
    protected function transform($value, Collection $directivesForArgument)
73
    {
74 1
        foreach ($directivesForArgument as $directive) {
75 1
            $value = $this->applyDirective($directive, $value);
76
        }
77

78 1
        return $value;
79
    }
80

81
    /**
82
     * @param  mixed  $value The client given value
83
     * @return mixed The transformed value
84
     */
85
    abstract protected function applyDirective(Directive $directive, $value);
86
}

Read our documentation on viewing source code .

Loading