1
<?php
2

3
namespace Nuwave\Lighthouse\Execution\Arguments;
4

5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
7

8
class NestedBelongsTo implements ArgResolver
9
{
10
    /**
11
     * @var \Illuminate\Database\Eloquent\Relations\BelongsTo
12
     */
13
    protected $relation;
14

15 1
    public function __construct(BelongsTo $relation)
16
    {
17 1
        $this->relation = $relation;
18
    }
19

20
    /**
21
     * @param  \Illuminate\Database\Eloquent\Model  $parent
22
     * @param  \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet  $args
23
     */
24 1
    public function __invoke($parent, $args): void
25
    {
26 1
        if ($args->has('create')) {
27 1
            $saveModel = new ResolveNested(new SaveModel($this->relation));
28

29 1
            $related = $saveModel(
30 1
                $this->relation->make(),
31 1
                $args->arguments['create']->value
32
            );
33 1
            $this->relation->associate($related);
34
        }
35

36 1
        if ($args->has('connect')) {
37 1
            $this->relation->associate($args->arguments['connect']->value);
38
        }
39

40 1
        if ($args->has('update')) {
41 1
            $updateModel = new ResolveNested(new UpdateModel(new SaveModel($this->relation)));
42

43 1
            $related = $updateModel(
44 1
                $this->relation->make(),
45 1
                $args->arguments['update']->value
46
            );
47 1
            $this->relation->associate($related);
48
        }
49

50 1
        if ($args->has('upsert')) {
51 1
            $upsertModel = new ResolveNested(new UpsertModel(new SaveModel($this->relation)));
52

53 1
            $related = $upsertModel(
54 1
                $this->relation->make(),
55 1
                $args->arguments['upsert']->value
56
            );
57 1
            $this->relation->associate($related);
58
        }
59

60 1
        self::disconnectOrDelete($this->relation, $args);
61
    }
62

63 1
    public static function disconnectOrDelete(BelongsTo $relation, ArgumentSet $args): void
64
    {
65
        // We proceed with disconnecting/deleting only if the given $values is truthy.
66
        // There is no other information to be passed when issuing those operations,
67
        // but GraphQL forces us to pass some value. It would be unintuitive for
68
        // the end user if the given value had no effect on the execution.
69
        if (
70 1
            $args->has('disconnect')
71 1
            && $args->arguments['disconnect']->value
72
        ) {
73 1
            $relation->dissociate();
74
        }
75

76
        if (
77 1
            $args->has('delete')
78 1
            && $args->arguments['delete']->value
79
        ) {
80 1
            $relation->dissociate();
81 1
            $relation->delete();
82
        }
83
    }
84
}

Read our documentation on viewing source code .

Loading