-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathNestedManyToMany.php
More file actions
118 lines (96 loc) · 3.71 KB
/
Copy pathNestedManyToMany.php
File metadata and controls
118 lines (96 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php declare(strict_types=1);
namespace Nuwave\Lighthouse\Execution\Arguments;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
class NestedManyToMany implements ArgResolver
{
public function __construct(
protected string $relationName,
) {}
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param ArgumentSet $args
*/
public function __invoke($model, $args): void
{
$relation = $model->{$this->relationName}();
assert($relation instanceof BelongsToMany);
if ($args->has('sync')) {
$relation->sync(
$this->generateRelationArray($args->arguments['sync']),
);
}
if ($args->has('syncWithoutDetaching')) {
$relation->syncWithoutDetaching(
$this->generateRelationArray($args->arguments['syncWithoutDetaching']),
);
}
if ($args->has('create')) {
$saveModel = new ResolveNested(new SaveModel($relation));
foreach ($args->arguments['create']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$saveModel($relation->make(), $childArgs);
}
}
if ($args->has('update')) {
$updateModel = new ResolveNested(new UpdateModel(new SaveModel($relation)));
foreach ($args->arguments['update']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$updateModel($relation->make(), $childArgs);
}
}
if ($args->has('upsert')) {
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel($relation), $relation));
foreach ($args->arguments['upsert']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$upsertModel($relation->make(), $childArgs);
}
}
if ($args->has('delete')) {
$ids = $args->arguments['delete']->toPlain();
$relation->detach($ids);
$relation->getRelated()::destroy($ids);
}
if ($args->has('connect')) {
$relation->attach(
$this->generateRelationArray($args->arguments['connect']),
);
}
if ($args->has('disconnect')) {
$relation->detach(
$args->arguments['disconnect']->toPlain(),
);
}
}
/**
* Generate an array for passing into sync, syncWithoutDetaching or connect method.
*
* Those functions natively have the capability of passing additional
* data to store in the pivot table. That array expects passing the id's
* as keys, so we transform the passed arguments to match that.
*
* @return array<mixed>
*/
protected function generateRelationArray(Argument $args): array
{
$values = $args->toPlain();
if (empty($values)) {
return [];
}
// Since GraphQL inputs are monomorphic, we can look at the first
// given value for an argument and deduce the type of all values.
$exemplaryValue = $values[0];
// We assume that the values contain pivot information
if (is_array($exemplaryValue)) {
$relationArray = [];
foreach ($values as $value) {
$id = Arr::pull($value, 'id');
$relationArray[$id] = $value;
}
return $relationArray;
}
// The default case is simply a flat array of IDs which we don't have to transform
return $values;
}
}