1
<?php
2

3
namespace Nuwave\Lighthouse\SoftDeletes;
4

5
use GraphQL\Language\Parser;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Illuminate\Support\ServiceProvider;
9
use Nuwave\Lighthouse\Events\ManipulateAST;
10
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
11
use Nuwave\Lighthouse\Exceptions\DefinitionException;
12
use Nuwave\Lighthouse\Support\Utils;
13

14
class SoftDeletesServiceProvider extends ServiceProvider
15
{
16
    /**
17
     * Ensure a model uses the SoftDeletes trait.
18
     *
19
     * @param  class-string<\Illuminate\Database\Eloquent\Model>  $modelClass
20
     * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
21
     * @see \Illuminate\Database\Eloquent\SoftDeletes
22
     */
23 1
    public static function assertModelUsesSoftDeletes(string $modelClass, string $exceptionMessage): void
24
    {
25 1
        if (! Utils::classUsesTrait($modelClass, SoftDeletes::class)) {
26 1
            throw new DefinitionException($exceptionMessage);
27
        }
28
    }
29

30
    /**
31
     * Bootstrap any application services.
32
     */
33 1
    public function boot(Dispatcher $dispatcher): void
34
    {
35 1
        $dispatcher->listen(
36 1
            ManipulateAST::class,
37
            function (ManipulateAST $manipulateAST): void {
38 1
                $manipulateAST->documentAST
39 1
                    ->setTypeDefinition(
40 1
                        Parser::enumTypeDefinition('
41
                            "Specify if you want to include or exclude trashed results from a query."
42
                            enum Trashed {
43
                                "Only return trashed results."
44
                                ONLY @enum(value: "only")
45

46
                                "Return both trashed and non-trashed results."
47
                                WITH @enum(value: "with")
48

49
                                "Only return non-trashed results."
50
                                WITHOUT @enum(value: "without")
51
                            }
52
                        ')
53
                    );
54
            }
55
        );
56

57 1
        $dispatcher->listen(
58 1
            RegisterDirectiveNamespaces::class,
59
            function (RegisterDirectiveNamespaces $registerDirectiveNamespaces): string {
60 1
                return __NAMESPACE__;
61
            }
62
        );
63
    }
64
}

Read our documentation on viewing source code .

Loading