1
<?php
2

3
namespace Nuwave\Lighthouse\Execution\Arguments;
4

5
use GraphQL\Error\Error;
6
use Illuminate\Support\Arr;
7
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
8

9
class UpdateModel implements ArgResolver
10
{
11
    const MISSING_PRIMARY_KEY_FOR_UPDATE = 'Missing primary key for update.';
12

13
    /**
14
     * @var callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver
15
     */
16
    protected $previous;
17

18
    /**
19
     * @param callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver $previous
20
     */
21 1
    public function __construct(callable $previous)
22
    {
23 1
        $this->previous = $previous;
24
    }
25

26
    /**
27
     * @param  \Illuminate\Database\Eloquent\Model  $model
28
     * @param  \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet  $args
29
     */
30 1
    public function __invoke($model, $args)
31
    {
32
        /** @var \Nuwave\Lighthouse\Execution\Arguments\Argument|null $id */
33 1
        $id = Arr::pull($args->arguments, 'id')
34 1
            ?? Arr::pull($args->arguments, $model->getKeyName())
35 1
            ?? null;
36

37 1
        if ($id === null) {
38 1
            throw new Error(self::MISSING_PRIMARY_KEY_FOR_UPDATE);
39
        }
40

41 1
        $model = $model->newQuery()->findOrFail($id->value);
42

43 1
        return ($this->previous)($model, $args);
44
    }
45
}

Read our documentation on viewing source code .

Loading