|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
|
8
|
|
|
use Application\Model\Card; |
|
9
|
|
|
use Application\Model\Collection; |
|
10
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
11
|
|
|
use GraphQL\Type\Definition\Type; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Custom implementation to have an optional collection. |
|
15
|
|
|
*/ |
|
16
|
|
|
class CreateCard implements FieldInterface |
|
17
|
|
|
{ |
|
18
|
7 |
|
public static function build(): iterable |
|
19
|
|
|
{ |
|
20
|
1 |
|
yield 'createCard' => fn () => [ |
|
21
|
1 |
|
'type' => Type::nonNull(_types()->getOutput(Card::class)), |
|
22
|
1 |
|
'description' => 'Create a new card', |
|
23
|
1 |
|
'args' => [ |
|
24
|
1 |
|
'input' => Type::nonNull(_types()->getInput(Card::class)), |
|
25
|
1 |
|
'collection' => _types()->getId(Collection::class), |
|
26
|
1 |
|
], |
|
27
|
1 |
|
'resolve' => function ($root, array $args): Card { |
|
28
|
|
|
// Check ACL |
|
29
|
7 |
|
$object = new Card(); |
|
30
|
|
|
|
|
31
|
|
|
// Be sure that site is set first |
|
32
|
7 |
|
$input = $args['input']; |
|
33
|
7 |
|
Helper::hydrate($object, ['site' => $input['site']]); |
|
34
|
|
|
|
|
35
|
|
|
// Check ACL |
|
36
|
7 |
|
Helper::throwIfDenied($object, 'create'); |
|
37
|
|
|
|
|
38
|
|
|
// Do it |
|
39
|
7 |
|
Helper::hydrate($object, $input); |
|
40
|
|
|
|
|
41
|
|
|
// Affect optional collection |
|
42
|
7 |
|
if ($args['collection'] ?? false) { |
|
43
|
|
|
/** @var Collection $collection */ |
|
44
|
5 |
|
$collection = $args['collection']->getEntity(); |
|
45
|
3 |
|
Helper::throwIfDenied($collection, 'linkCard'); |
|
46
|
|
|
|
|
47
|
2 |
|
$object->addCollection($collection); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
_em()->persist($object); |
|
51
|
4 |
|
_em()->flush(); |
|
52
|
|
|
|
|
53
|
4 |
|
return $object; |
|
54
|
1 |
|
}, |
|
55
|
1 |
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|