1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\GraphQL\Directives; |
4
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
6
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
8
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
9
|
|
|
use Laravel\Scout\Builder as ScoutBuilder; |
10
|
|
|
use Nuwave\Lighthouse\Pagination\PaginateDirective; |
11
|
|
|
use Nuwave\Lighthouse\Pagination\PaginationArgs; |
12
|
|
|
use Nuwave\Lighthouse\Pagination\PaginationManipulator; |
13
|
|
|
use Nuwave\Lighthouse\Pagination\PaginationType; |
14
|
|
|
use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
15
|
|
|
use Nuwave\Lighthouse\Schema\Values\FieldValue; |
16
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
17
|
|
|
|
18
|
|
|
class PaginateTranslationsDirective extends PaginateDirective |
19
|
|
|
{ |
20
|
|
|
use Traits\LocaleFilters; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function name(): string |
26
|
|
|
{ |
27
|
|
|
return 'paginateTranslations'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public static function definition(): string |
34
|
|
|
{ |
35
|
|
|
return /* @lang GraphQL */ <<<'SDL' |
36
|
|
|
directive @paginateTranslations( |
37
|
|
|
"Specify the class name of the model to use." |
38
|
|
|
model: String |
39
|
|
|
"Specify the GraphQL type to add the 'locale' field (if GraphQL type is different from model class basename)." |
40
|
|
|
type: String |
41
|
|
|
"Which pagination style to use. Allowed values: paginator, connection." |
42
|
|
|
paginatorType: String = "paginator" |
43
|
|
|
"Apply scopes to the underlying query." |
44
|
|
|
scopes: [String!] |
45
|
|
|
"Overwrite the paginate_max_count setting value to limit the amount of items that a user can request per page." |
46
|
|
|
maxCount: Int |
47
|
|
|
"Use a default value for the amount of returned items in case the client does not request it explicitly?." |
48
|
|
|
defaultCount: Int |
49
|
|
|
) on FIELD_DEFINITION |
50
|
|
|
SDL; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void |
57
|
|
|
{ |
58
|
|
|
$paginationManipulator = new PaginationManipulator($documentAST); |
59
|
|
|
|
60
|
|
|
$paginationManipulator |
61
|
|
|
->setModelClass($this->getModelClass()) |
62
|
|
|
->transformToPaginatedField( |
63
|
|
|
$this->paginationType(), |
64
|
|
|
$fieldDefinition, |
65
|
|
|
$parentType, |
66
|
|
|
$this->directiveArgValue('defaultCount'), |
67
|
|
|
$this->paginateMaxCount() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function resolveField(FieldValue $fieldValue): FieldValue |
75
|
|
|
{ |
76
|
|
|
return $fieldValue->setResolver( |
77
|
|
|
function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): LengthAwarePaginator { |
78
|
|
|
[$first, $page] = PaginationArgs::extractArgs($args, $this->paginationType(), $this->paginateMaxCount()); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$query = $resolveInfo |
|
|
|
|
81
|
|
|
->argumentSet |
82
|
|
|
->enhanceBuilder( |
83
|
|
|
$this->localeFilters($this->getModelClass(), $args), |
84
|
|
|
$this->directiveArgValue('scopes', []) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if ($query instanceof ScoutBuilder) { |
|
|
|
|
88
|
|
|
return $query->paginate($first, 'page', $page); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $query->paginate($first, ['*'], 'page', $page); |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function paginationType(): PaginationType |
97
|
|
|
{ |
98
|
|
|
return new PaginationType( |
99
|
|
|
$this->directiveArgValue('paginatorType', PaginationType::PAGINATOR) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.