1
<?php
2

3
namespace Nuwave\Lighthouse\OrderBy;
4

5
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
6
use GraphQL\Language\Parser;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Support\ServiceProvider;
9
use Nuwave\Lighthouse\Events\ManipulateAST;
10
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
11

12
class OrderByServiceProvider extends ServiceProvider
13
{
14
    public const DEFAULT_ORDER_BY_CLAUSE = 'OrderByClause';
15

16
    /**
17
     * Bootstrap any application services.
18
     */
19 1
    public function boot(Dispatcher $dispatcher): void
20
    {
21 1
        $dispatcher->listen(
22 1
            RegisterDirectiveNamespaces::class,
23
            function (RegisterDirectiveNamespaces $registerDirectiveNamespaces): string {
24 1
                return __NAMESPACE__;
25
            }
26
        );
27

28 1
        $dispatcher->listen(
29 1
            ManipulateAST::class,
30
            function (ManipulateAST $manipulateAST): void {
31 1
                $manipulateAST->documentAST
32 1
                    ->setTypeDefinition(
33 1
                        Parser::enumTypeDefinition(/** @lang GraphQL */ '
34
                            "The available directions for ordering a list of records."
35
                            enum SortOrder {
36
                                "Sort records in ascending order."
37
                                ASC
38

39
                                "Sort records in descending order."
40
                                DESC
41
                            }
42
                        '
43
                        )
44
                    )
45 1
                    ->setTypeDefinition(
46 1
                        static::createOrderByClauseInput(
47 1
                            static::DEFAULT_ORDER_BY_CLAUSE,
48 1
                            'Allows ordering a list of records.',
49 1
                            'String'
50
                        )
51
                    );
52
            }
53
        );
54
    }
55

56 1
    public static function createOrderByClauseInput(string $name, string $description, string $columnType): InputObjectTypeDefinitionNode
57
    {
58 1
        return Parser::inputObjectTypeDefinition(/** @lang GraphQL */ <<<GRAPHQL
59 1
            "$description"
60 1
            input $name {
61
                "The column that is used for ordering."
62 1
                column: $columnType!
63

64
                "The direction that is used for ordering."
65
                order: SortOrder!
66
            }
67
GRAPHQL
68
        );
69
    }
70
}

Read our documentation on viewing source code .

Loading