1 | <?php |
||
2 | |||
3 | namespace Bakery\Mutations; |
||
4 | |||
5 | use Bakery\Fields\Field; |
||
6 | use Bakery\Support\Arguments; |
||
7 | use Illuminate\Support\Facades\DB; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | |||
10 | class UpdateMutation extends EloquentMutation |
||
11 | { |
||
12 | /** |
||
13 | * Get the name of the mutation. |
||
14 | * |
||
15 | * @return string |
||
16 | */ |
||
17 | public function name(): string |
||
18 | { |
||
19 | if (isset($this->name)) { |
||
20 | return $this->name; |
||
21 | } |
||
22 | |||
23 | return 'update'.$this->modelSchema->getTypename(); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Get the arguments of the mutation. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | public function args(): array |
||
32 | { |
||
33 | return array_merge( |
||
34 | parent::args(), |
||
35 | $this->modelSchema->getLookupFields()->map(function (Field $field) { |
||
36 | return $field->getType(); |
||
37 | })->toArray() |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Resolve the mutation. |
||
43 | * |
||
44 | * @param Arguments $args |
||
45 | * @return Model |
||
46 | */ |
||
47 | public function resolve(Arguments $args): Model |
||
48 | { |
||
49 | $input = $args->input->toArray(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | $model = $this->findOrFail($args); |
||
51 | |||
52 | return DB::transaction(function () use ($input, $model) { |
||
53 | $modelSchema = $this->registry->getSchemaForModel($model); |
||
54 | |||
55 | return $modelSchema->updateIfAuthorized($input); |
||
56 | }); |
||
57 | } |
||
58 | } |
||
59 |