-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathUpdateModel.php
More file actions
76 lines (63 loc) · 2.03 KB
/
UpdateModel.php
File metadata and controls
76 lines (63 loc) · 2.03 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
<?php
namespace Nuwave\Lighthouse\Execution\Arguments;
use GraphQL\Error\Error;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
class UpdateModel implements ArgResolver
{
public const MISSING_PRIMARY_KEY_FOR_UPDATE = 'Missing primary key for update.';
/**
* @var callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver
*/
protected $previous;
/**
* @param callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver $previous
*/
public function __construct(callable $previous)
{
$this->previous = $previous;
}
/**
* @param Model $model
* @param \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet $args
*/
public function __invoke($model, $args)
{
$id = self::pullId($args, $model);
$instance = $model->newQuery()->findOrFail($id);
return ($this->previous)($instance, $args);
}
/**
* Extract and remove the model ID from the given args.
*
* @return mixed any non-null ID value
*/
public static function pullId(ArgumentSet $args, Model $model)
{
/** @var \Nuwave\Lighthouse\Execution\Arguments\Argument|null $id */
$id = Arr::pull($args->arguments, 'id')
?? Arr::pull($args->arguments, $model->getKeyName())
?? null;
if (null === $id) {
throw new Error(self::MISSING_PRIMARY_KEY_FOR_UPDATE);
}
return $id->value;
}
/**
* Extract and remove the model ID from the given args.
*
* @return mixed any non-null ID value
*/
public static function getId(ArgumentSet $args, Model $model)
{
/** @var \Nuwave\Lighthouse\Execution\Arguments\Argument|null $id */
$id = Arr::get($args->arguments, 'id')
?? Arr::get($args->arguments, $model->getKeyName())
?? null;
if (null === $id) {
throw new Error(self::MISSING_PRIMARY_KEY_FOR_UPDATE);
}
return $id->value;
}
}