|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Petecoop\LaravelActionsLighthouse; |
| 4 | + |
| 5 | +use ReflectionFunction; |
| 6 | +use GraphQL\Language\Parser; |
| 7 | +use GraphQL\Language\AST\Node; |
| 8 | +use Nuwave\Lighthouse\Schema\AST\ASTHelper; |
| 9 | +use GraphQL\Language\AST\FieldDefinitionNode; |
| 10 | +use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
| 11 | +use Nuwave\Lighthouse\Schema\Values\TypeValue; |
| 12 | +use Nuwave\Lighthouse\Schema\Values\FieldValue; |
| 13 | +use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
| 14 | +use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
| 15 | +use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; |
| 16 | +use Nuwave\Lighthouse\Support\Contracts\ArgumentSetValidation; |
| 17 | + |
| 18 | +class ActionValidatorDirective extends BaseDirective implements ArgumentSetValidation, FieldManipulator |
| 19 | +{ |
| 20 | + protected $action; |
| 21 | + |
| 22 | + public static function definition(): string |
| 23 | + { |
| 24 | + return /** @lang GraphQL */ <<<'GRAPHQL' |
| 25 | +directive @actionValidator(class: String) on FIELD_DEFINITION |
| 26 | +GRAPHQL; |
| 27 | + } |
| 28 | + |
| 29 | + public function rules(): array |
| 30 | + { |
| 31 | + $action = $this->action(); |
| 32 | + if (method_exists($action, 'rules')) { |
| 33 | + return $action->rules(); |
| 34 | + } |
| 35 | + |
| 36 | + return []; |
| 37 | + } |
| 38 | + |
| 39 | + public function messages(): array |
| 40 | + { |
| 41 | + $action = $this->action(); |
| 42 | + if (method_exists($action, 'getValidationMessages')) { |
| 43 | + return $action->getValidationMessages(); |
| 44 | + } |
| 45 | + |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + public function attributes(): array |
| 50 | + { |
| 51 | + $action = $this->action(); |
| 52 | + if (method_exists($action, 'getValidationAttributes')) { |
| 53 | + return $action->getValidationAttributes(); |
| 54 | + } |
| 55 | + |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + protected function action() |
| 60 | + { |
| 61 | + if ($this->action === null) { |
| 62 | + $this->action = app($this->directiveArgValue('class')); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->action; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Give the validator the action class to use later |
| 70 | + */ |
| 71 | + public function manipulateFieldDefinition( |
| 72 | + DocumentAST &$documentAST, |
| 73 | + FieldDefinitionNode &$fieldDefinition, |
| 74 | + ObjectTypeDefinitionNode &$parentType |
| 75 | + ) { |
| 76 | + $type = new TypeValue($parentType); |
| 77 | + $fieldValue = new FieldValue($type, $fieldDefinition); |
| 78 | + $fieldValue->useDefaultResolver(); |
| 79 | + $actionClosure = $fieldValue->getResolver(); |
| 80 | + |
| 81 | + $reflect = new ReflectionFunction($actionClosure); |
| 82 | + $decorator = $reflect->getClosureThis(); |
| 83 | + $action = $decorator->getAction(); |
| 84 | + |
| 85 | + $this->setFullClassnameOnDirective($fieldDefinition, get_class($action)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the full classname of the validator class on the directive. |
| 90 | + * |
| 91 | + * This allows accessing it straight away when resolving the query. |
| 92 | + * |
| 93 | + * @param (\GraphQL\Language\AST\TypeDefinitionNode&\GraphQL\Language\AST\Node)|\GraphQL\Language\AST\FieldDefinitionNode $definition |
| 94 | + */ |
| 95 | + protected function setFullClassnameOnDirective(Node &$definition, string $class): void |
| 96 | + { |
| 97 | + // @phpstan-ignore-next-line The passed in Node types all have the property $directives |
| 98 | + foreach ($definition->directives as $directive) { |
| 99 | + if ($directive->name->value === $this->name()) { |
| 100 | + $directive->arguments = ASTHelper::mergeUniqueNodeList( |
| 101 | + $directive->arguments, |
| 102 | + [Parser::argument('class: "'.addslashes($class).'"')], |
| 103 | + true |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments