PaginateDirective::resolveField()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 4
nc 1
nop 1
1
<?php
2
3
namespace DutchCodingCompany\CursorPagination\Directives;
4
5
use GraphQL\Language\AST\FieldDefinitionNode;
6
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
7
use GraphQL\Type\Definition\ResolveInfo;
8
use DutchCodingCompany\CursorPagination\Pagination\CursorArgs;
9
use DutchCodingCompany\CursorPagination\Pagination\PaginationManipulator;
10
use Nuwave\Lighthouse\Pagination\PaginationArgs;
11
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
12
use Nuwave\Lighthouse\Schema\Values\FieldValue;
13
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
14
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
15
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
16
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
17
18
class PaginateDirective extends \Nuwave\Lighthouse\Schema\Directives\PaginateDirective implements FieldResolver, FieldManipulator, DefinedDirective
0 ignored issues
show
Deprecated Code introduced by
The interface Nuwave\Lighthouse\Suppor...tracts\DefinedDirective has been deprecated with message: The method definition() will be moved to

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    public static function definition(): string
21
    {
22
        return /** @lang GraphQL */ <<<'SDL'
23
"""
24
Query multiple model entries as a paginated list.
25
"""
26
directive @paginate(
27
  """
28
  Which pagination style to use.
29
  Allowed values: `paginator`, `connection`.
30
  """
31
  type: String = "paginator"
32
33
  """
34
  Specify the class name of the model to use.
35
  This is only needed when the default model detection does not work.
36
  """
37
  model: String
38
39
  """
40
  Point to a function that provides a Query Builder instance.
41
  This replaces the use of a model.
42
  """
43
  builder: String
44
45
  """
46
  Apply scopes to the underlying query.
47
  """
48
  scopes: [String!]
49
50
  """
51
  Allow clients to query paginated lists without specifying the amount of items.
52
  Overrules the `pagination.default_count` setting from `lighthouse.php`.
53
  """
54
  defaultCount: Int
55
56
  """
57
  Limit the maximum amount of items that clients can request from paginated lists.
58
  Overrules the `pagination.max_count` setting from `lighthouse.php`.
59
  """
60
  maxCount: Int
61
) on FIELD_DEFINITION
62
SDL;
63
    }
64
65
    public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void
66
    {
67
        $paginationManipulator = new PaginationManipulator($documentAST);
68
69
        if ($this->directiveHasArgument('builder')) {
70
            // This is done only for validation
71
            $this->getResolverFromArgument('builder');
72
        } else {
73
            $paginationManipulator->setModelClass(
74
                $this->getModelClass()
75
            );
76
        }
77
78
        $paginationManipulator
79
            ->transformToPaginatedField(
80
                $this->paginationType(),
81
                $fieldDefinition,
82
                $parentType,
83
                $this->directiveArgValue('defaultCount')
84
                ?? config('lighthouse.pagination.default_count'),
85
                $this->paginateMaxCount()
86
            );
87
    }
88
89
    /**
90
     * Resolve the field directive.
91
     */
92
    public function resolveField(FieldValue $fieldValue): FieldValue
93
    {
94
        return $fieldValue->setResolver(
95
            function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
96
                if ($this->directiveHasArgument('builder')) {
97
                    $query = call_user_func(
98
                        $this->getResolverFromArgument('builder'),
99
                        $root,
100
                        $args,
101
                        $context,
102
                        $resolveInfo
103
                    );
104
                } else {
105
                    $query = $this->getModelClass()::query();
0 ignored issues
show
Bug introduced by
The method query cannot be called on $this->getModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
106
                }
107
108
                $query = $resolveInfo
0 ignored issues
show
Bug introduced by
The property argumentSet does not seem to exist in GraphQL\Type\Definition\ResolveInfo.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
109
                    ->argumentSet
110
                    ->enhanceBuilder(
111
                        $query,
112
                        $this->directiveArgValue('scopes', [])
113
                    );
114
115
                if ($this->paginationType()->isPaginator()) {
116
                    return PaginationArgs
117
                        ::extractArgs($args, $this->paginationType(), $this->paginateMaxCount())
118
                        ->applyToBuilder($query);
119
                }
120
                if ($this->paginationType()->isConnection()) {
121
                    return CursorArgs
122
                        ::extractArgs($args, $this->paginationType(), $this->paginateMaxCount())
123
                        ->applyToBuilder($query);
124
                }
125
            }
126
        );
127
    }
128
}
129