1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.ru 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\GraphQL\Serializers\SearchResultsSerializer; |
12
|
|
|
use GraphQL\Type\Definition\Type; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
use Folklore\GraphQL\Support\Query; |
15
|
|
|
use App\GraphQL\Types\SearchResultType; |
16
|
|
|
use GraphQL\Type\Definition\ListOfType; |
17
|
|
|
use Service\SearchService\SearchService; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class SearchQuery |
21
|
|
|
* @package App\GraphQL\Queries |
22
|
|
|
*/ |
23
|
|
|
class SearchQuery extends Query |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $attributes = [ |
29
|
|
|
'name' => 'Article list query', |
30
|
|
|
'description' => 'Returns a list of available articles', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return ListOfType |
35
|
|
|
*/ |
36
|
|
|
public function type(): ListOfType |
37
|
|
|
{ |
38
|
|
|
return Type::listOf(\GraphQL::type(SearchResultType::getName())); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $root |
43
|
|
|
* @param array $args |
44
|
|
|
* @return Collection |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function resolve($root, array $args = []): Collection |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$search = app(SearchService::class); |
50
|
|
|
|
51
|
|
|
$repo = $search->findByName($args['type']); |
52
|
|
|
|
53
|
|
|
if ($repo === null) { |
54
|
|
|
$message = 'Invalid type %s. Available types: %s'; |
55
|
|
|
$types = implode(', ', $search->getCategories()); |
56
|
|
|
|
57
|
|
|
throw new \InvalidArgumentException(sprintf($message, $args['type'], $types)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$result = $repo->getSearchResults($args['query'], $this->formatLimit($args)); |
62
|
|
|
|
63
|
|
|
return SearchResultsSerializer::collection($result); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $args |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
private function formatLimit(array $args = []): int |
71
|
|
|
{ |
72
|
|
|
$limit = $args['_limit'] ?? 10; |
73
|
|
|
|
74
|
|
|
return max(10, min(100, $limit)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function args(): array |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
'_limit' => [ |
84
|
|
|
'type' => Type::int(), |
85
|
|
|
'description' => 'Search results limit', |
86
|
|
|
], |
87
|
|
|
'type' => [ |
88
|
|
|
'type' => Type::nonNull(Type::string()), |
89
|
|
|
'description' => 'Search category', |
90
|
|
|
], |
91
|
|
|
'query' => [ |
92
|
|
|
'type' => Type::nonNull(Type::string()), |
93
|
|
|
'description' => 'Search query', |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.