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 |
|
|
|
|
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(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$query = $resolveInfo |
|
|
|
|
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
|
|
|
|
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.