1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Lighthouse\Directives; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Formularium\DatatypeFactory; |
7
|
|
|
use Formularium\Validator\MinLength; |
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
9
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory; |
10
|
|
|
use Nuwave\Lighthouse\Exceptions\ValidationException; |
11
|
|
|
use Nuwave\Lighthouse\Schema\Values\FieldValue; |
12
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
13
|
|
|
use Nuwave\Lighthouse\Support\Contracts\ProvidesRules; |
14
|
|
|
use Nuwave\Lighthouse\Support\Traits\HasResolverArguments; |
15
|
|
|
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
16
|
|
|
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective; |
17
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; |
18
|
|
|
|
19
|
|
|
class MinLengthDirective extends BaseDirective implements FieldMiddleware, DefinedDirective |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
use HasResolverArguments; |
22
|
|
|
|
23
|
|
|
public static function definition(): string |
24
|
|
|
{ |
25
|
|
|
return /** @lang GraphQL */ <<<'SDL' |
26
|
|
|
""" |
27
|
|
|
Base class to extend on model. |
28
|
|
|
""" |
29
|
|
|
directive @minLength( |
30
|
|
|
""" |
31
|
|
|
The base class name with namespace |
32
|
|
|
""" |
33
|
|
|
value: Int! |
34
|
|
|
) on OBJECT |
35
|
|
|
SDL; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Resolve the field directive. |
40
|
|
|
*/ |
41
|
|
|
public function handleField(FieldValue $fieldValue, Closure $next): FieldValue |
42
|
|
|
{ |
43
|
|
|
$previousResolver = $fieldValue->getResolver(); |
44
|
|
|
|
45
|
|
|
// Wrap around the resolver |
46
|
|
|
$wrappedResolver = function ($root, array $args, GraphQLContext $context, ResolveInfo $info) use ($previousResolver): string { |
47
|
|
|
error_log("xxxxxx"); |
48
|
|
|
|
49
|
|
|
// Call the resolver, passing along the resolver arguments |
50
|
|
|
/** @var string $result */ |
51
|
|
|
$result = $previousResolver($root, $args, $context, $info); |
52
|
|
|
|
53
|
|
|
MinLength::validate( |
|
|
|
|
54
|
|
|
$result, |
55
|
|
|
['value' => $this->directiveArgValue('value')] |
56
|
|
|
); |
57
|
|
|
return $result; |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
// Place the wrapped resolver back upon the FieldValue |
61
|
|
|
// It is not resolved right now - we just prepare it |
62
|
|
|
$fieldValue->setResolver($wrappedResolver); |
63
|
|
|
|
64
|
|
|
// Keep the middleware chain going |
65
|
|
|
return $next($fieldValue); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.