Skip to content

Commit 27940d0

Browse files
committed
init
0 parents  commit 27940d0

9 files changed

+411
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "petecoop/laravel-actions-lighthouse",
3+
"license": "MIT",
4+
"authors": [
5+
{
6+
"name": "Pete Cooper",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"require": {
11+
"illuminate/contracts": "^8.15",
12+
"lorisleiva/laravel-actions": "^2.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Petecoop\\LaravelActionsLighthouse\\": "src"
17+
}
18+
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"Petecoop\\LaravelActionsLighthouse\\ActionServiceProvider"
23+
]
24+
}
25+
}
26+
}

composer.lock

Lines changed: 240 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ActionServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Petecoop\LaravelActionsLighthouse;
4+
5+
use Lorisleiva\Actions\ActionManager;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class ActionServiceProvider extends ServiceProvider
9+
{
10+
public function register()
11+
{
12+
$this->app->extend(ActionManager::class, function ($actionManager) {
13+
$designPatterns = $actionManager->getDesignPatterns();
14+
$designPatterns[] = new GraphQLDesignPattern();
15+
$actionManager->setDesignPatterns($designPatterns);
16+
});
17+
}
18+
}

src/ActionValidationDirective.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Petecoop\LaravelActionsLighthouse;
4+
5+
use Closure;
6+
use Nuwave\Lighthouse\Schema\Values\FieldValue;
7+
use Nuwave\Lighthouse\Schema\Directives\ValidationDirective;
8+
use ReflectionFunction;
9+
10+
class ActionValidationDirective extends ValidationDirective
11+
{
12+
protected array $rules = [];
13+
protected array $messages = [];
14+
15+
/**
16+
* Gets the validation info from the action through reflection...
17+
* Not pretty but it works!
18+
*/
19+
public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
20+
{
21+
$resolver = $fieldValue->getResolver();
22+
23+
$fieldValue->useDefaultResolver();
24+
$actionClosure = $fieldValue->getResolver();
25+
$fieldValue->setResolver($resolver);
26+
27+
$reflect = new ReflectionFunction($actionClosure);
28+
$decorator = $reflect->getClosureThis();
29+
$action = $decorator->getAction();
30+
31+
if (method_exists($action, 'rules')) {
32+
$this->rules = $action->rules();
33+
}
34+
35+
if (method_exists($action, 'getValidationMessages')) {
36+
$this->messages = $action->getValidationMessages();
37+
}
38+
39+
return parent::handleField($fieldValue, $next);
40+
}
41+
42+
public function rules(): array
43+
{
44+
return $this->rules;
45+
}
46+
47+
public function messages(): array
48+
{
49+
return $this->messages;
50+
}
51+
}

src/AsGraphQL.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Petecoop\LaravelActionsLighthouse;
4+
5+
trait AsGraphQL
6+
{
7+
8+
}

src/GraphQLDecorator.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Petecoop\LaravelActionsLighthouse;
4+
5+
use Illuminate\Contracts\Container\Container;
6+
use Lorisleiva\Actions\Concerns\DecorateActions;
7+
8+
class GraphQLDecorator
9+
{
10+
use DecorateActions;
11+
12+
public function __construct($action, Container $container)
13+
{
14+
$this->setAction($action);
15+
$this->setContainer($container);
16+
}
17+
18+
public function __invoke($_, $args)
19+
{
20+
if ($this->hasMethod('asGraphQL')) {
21+
return $this->fromActionMethod('asGraphQL', [$_, $args]);
22+
}
23+
24+
if ($this->hasMethod('handle')) {
25+
return $this->fromActionMethod('handle', [$args]);
26+
}
27+
}
28+
29+
public function getAction()
30+
{
31+
return $this->action;
32+
}
33+
}

0 commit comments

Comments
 (0)