|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of laravel.su package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace App\GraphQL\Queries; |
|
10
|
|
|
|
|
11
|
|
|
use App\Models\Docs; |
|
12
|
|
|
use App\GraphQL\Types\DocsType; |
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
15
|
|
|
use Illuminate\Support\Collection; |
|
16
|
|
|
use GraphQL\Type\Definition\ListOfType; |
|
17
|
|
|
use App\GraphQL\Serializers\DocsSerializer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class DocsQuery. |
|
21
|
|
|
*/ |
|
22
|
|
|
class DocsQuery extends AbstractCollectionQuery |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $attributes = [ |
|
28
|
|
|
'name' => 'Docs list query', |
|
29
|
|
|
'description' => 'Returns a list of available documentation repositories', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return ListOfType |
|
34
|
|
|
*/ |
|
35
|
|
|
public function type(): ListOfType |
|
36
|
|
|
{ |
|
37
|
|
|
return Type::listOf(\GraphQL::type(DocsType::getName())); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $root |
|
42
|
|
|
* @param array $args |
|
43
|
|
|
* @return Collection |
|
44
|
|
|
*/ |
|
45
|
|
|
public function resolve($root, array $args = []): Collection |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$query = $this->queryFor(Docs::class, $args) |
|
48
|
|
|
->with('pages'); |
|
49
|
|
|
|
|
50
|
|
|
$this->checkVersion($query, $args); |
|
51
|
|
|
$this->checkProject($query, $args); |
|
52
|
|
|
|
|
53
|
|
|
return DocsSerializer::collection($query->get()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Builder $query |
|
58
|
|
|
* @param array $args |
|
59
|
|
|
* @return Builder|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private function checkProject(Builder $query, array $args): ?Builder |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->whenExists($args, 'project', function(string $project) use ($query) { |
|
64
|
|
|
return $query->where('slug', $project); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Builder $query |
|
70
|
|
|
* @param array $args |
|
71
|
|
|
* @return Builder |
|
72
|
|
|
*/ |
|
73
|
|
|
private function checkVersion(Builder $query, array $args): ?Builder |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->whenExists($args, 'version', function (string $version) use ($query) { |
|
76
|
|
|
if ($version === 'latest') { |
|
77
|
|
|
return $query->orderBy('version', 'desc') |
|
|
|
|
|
|
78
|
|
|
->take(1); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $query->where('version', $version); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function queryArguments(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
'version' => [ |
|
92
|
|
|
'name' => 'version', |
|
93
|
|
|
'type' => Type::string(), |
|
94
|
|
|
], |
|
95
|
|
|
'project' => [ |
|
96
|
|
|
'name' => 'project', |
|
97
|
|
|
'type' => Type::string(), |
|
98
|
|
|
], |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.