1
<?php
2

3
namespace Nuwave\Lighthouse\SoftDeletes;
4

5
use GraphQL\Language\AST\FieldDefinitionNode;
6
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
7
use Illuminate\Database\Eloquent\Model;
8
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
9
use Nuwave\Lighthouse\Schema\Directives\ModifyModelExistenceDirective;
10
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
11

12
class RestoreDirective extends ModifyModelExistenceDirective implements FieldManipulator
13
{
14
    public const MODEL_NOT_USING_SOFT_DELETES = 'Use the @restore directive only for Model classes that use the SoftDeletes trait.';
15

16 1
    public static function definition(): string
17
    {
18
        return /** @lang GraphQL */ <<<'GRAPHQL'
19 1
"""
20
Un-delete one or more soft deleted models by their ID.
21
The field must have a single non-null argument that may be a list.
22
"""
23
directive @restore(
24
  """
25
  DEPRECATED use @globalId, will be removed in v6
26

27
  Set to `true` to use global ids for finding the model.
28
  If set to `false`, regular non-global ids are used.
29
  """
30
  globalId: Boolean = false
31

32
  """
33
  Specify the class name of the model to use.
34
  This is only needed when the default model detection does not work.
35
  """
36
  model: String
37
) on FIELD_DEFINITION
38
GRAPHQL;
39
    }
40

41 1
    protected function find(string $modelClass, $idOrIds)
42
    {
43
        /** @var \Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\SoftDeletes $modelClass */
44 1
        return $modelClass::withTrashed()->find($idOrIds);
45
    }
46

47 1
    protected function modifyExistence(Model $model): bool
48
    {
49
        /** @var \Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\SoftDeletes $model */
50 1
        return (bool) $model->restore();
51
    }
52

53
    /**
54
     * Manipulate the AST based on a field definition.
55
     */
56 1
    public function manipulateFieldDefinition(
57
        DocumentAST &$documentAST,
58
        FieldDefinitionNode &$fieldDefinition,
59
        ObjectTypeDefinitionNode &$parentType
60
    ): void {
61 1
        parent::manipulateFieldDefinition($documentAST, $fieldDefinition, $parentType);
62

63 1
        SoftDeletesServiceProvider::assertModelUsesSoftDeletes(
64 1
            $this->getModelClass(),
65 1
            self::MODEL_NOT_USING_SOFT_DELETES
66
        );
67
    }
68
}

Read our documentation on viewing source code .

Loading