|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bakery\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
|
6
|
|
|
use Bakery\Support\Arguments; |
|
7
|
|
|
use Bakery\Traits\OrdersQueries; |
|
8
|
|
|
use Bakery\Traits\FiltersQueries; |
|
9
|
|
|
use Bakery\Traits\SearchesQueries; |
|
10
|
|
|
use Bakery\Types\Definitions\RootType; |
|
11
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
12
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
13
|
|
|
use Bakery\Exceptions\PaginationMaxCountExceededException; |
|
14
|
|
|
|
|
15
|
|
|
class EloquentCollectionQuery extends EloquentQuery |
|
16
|
|
|
{ |
|
17
|
|
|
use FiltersQueries; |
|
18
|
|
|
use OrdersQueries; |
|
19
|
|
|
use SearchesQueries; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The fields to be fulltext searched on. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $tsFields; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get the name of the CollectionQuery. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function name(): string |
|
34
|
|
|
{ |
|
35
|
|
|
if (isset($this->name)) { |
|
36
|
|
|
return $this->name; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return Utils::plural($this->modelSchema->getTypename()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The type of the CollectionQuery. |
|
44
|
|
|
* |
|
45
|
|
|
* @return RootType |
|
46
|
|
|
*/ |
|
47
|
|
|
public function type(): RootType |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->registry->type($this->modelSchema->typename().'Collection'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The arguments for the CollectionQuery. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function args(): array |
|
58
|
|
|
{ |
|
59
|
|
|
$args = collect([ |
|
60
|
|
|
'page' => $this->registry->int()->nullable(), |
|
61
|
|
|
'count' => $this->registry->int()->nullable(), |
|
62
|
|
|
'filter' => $this->registry->type($this->modelSchema->typename().'Filter')->nullable(), |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
if ($this->modelSchema->isSearchable()) { |
|
66
|
|
|
$args->put('search', $this->registry->type($this->modelSchema->typename().'RootSearch')->nullable()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($this->modelSchema->isSortable()) { |
|
70
|
|
|
$args->put('orderBy', $this->registry->type($this->modelSchema->typename().'OrderBy')->nullable()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $args->toArray(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Resolve the CollectionQuery. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Arguments $args |
|
80
|
|
|
* @param mixed $root |
|
81
|
|
|
* @param mixed $context |
|
82
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
|
83
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
|
84
|
|
|
* @throws PaginationMaxCountExceededException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function resolve(Arguments $args, $root, $context, ResolveInfo $info): LengthAwarePaginator |
|
87
|
|
|
{ |
|
88
|
|
|
$page = $args->page ?? 1; |
|
|
|
|
|
|
89
|
|
|
$count = $args->count ?? 15; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$maxCount = config('bakery.security.paginationMaxCount'); |
|
92
|
|
|
|
|
93
|
|
|
if ($count > $maxCount) { |
|
94
|
|
|
throw new PaginationMaxCountExceededException($maxCount); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$query = $this->scopeQuery($this->modelSchema->getQuery()); |
|
98
|
|
|
|
|
99
|
|
|
$fields = $info->getFieldSelection(config('bakery.security.eagerLoadingMaxDepth')); |
|
100
|
|
|
$this->eagerLoadRelations($query, $fields['items'], $this->modelSchema); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
// Select all columns from the table. |
|
103
|
|
|
$query->addSelect($this->model->getTable().'.*'); |
|
104
|
|
|
|
|
105
|
|
|
if ($args->filter) { |
|
|
|
|
|
|
106
|
|
|
$query = $this->applyFilters($query, $args->filter); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($args->search) { |
|
|
|
|
|
|
110
|
|
|
$query = $this->applySearch($query, $args->search); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($args->orderBy) { |
|
|
|
|
|
|
114
|
|
|
$query = $this->applyOrderBy($query, $args->orderBy); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $query->paginate($count, ['*'], 'page', $page); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|