1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\GraphQL\Queries; |
11
|
|
|
|
12
|
|
|
use App\Models\Tip; |
13
|
|
|
use App\GraphQL\Types\TipType; |
14
|
|
|
use GraphQL\Type\Definition\Type; |
15
|
|
|
use Illuminate\Database\Query\Builder; |
16
|
|
|
use Illuminate\Support\Collection; |
17
|
|
|
use Folklore\GraphQL\Support\Query; |
18
|
|
|
use GraphQL\Type\Definition\ListOfType; |
19
|
|
|
use App\GraphQL\Serializers\TipSerializer; |
20
|
|
|
use App\GraphQL\Queries\Support\QueryLimit; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TipsQuery. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
class TipsQuery extends Query |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
use QueryLimit; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $attributes = [ |
33
|
|
|
'name' => 'Tips list query', |
34
|
|
|
'description' => 'Returns a list of available tips', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return ListOfType |
39
|
|
|
*/ |
40
|
|
|
public function type(): ListOfType |
41
|
|
|
{ |
42
|
|
|
return Type::listOf(\GraphQL::type(TipType::getName())); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function args(): array |
49
|
|
|
{ |
50
|
|
|
return $this->argumentsWithLimit([ |
51
|
|
|
'id' => [ |
52
|
|
|
'type' => Type::id(), |
53
|
|
|
'description' => 'Tip identifier', |
54
|
|
|
], |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $root |
60
|
|
|
* @param array $args |
61
|
|
|
* @return Collection |
62
|
|
|
*/ |
63
|
|
|
public function resolve($root, array $args = []): Collection |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
/** @var Builder $query */ |
66
|
|
|
$query = Tip::with('likes', 'dislikes', 'user'); |
67
|
|
|
|
68
|
|
|
$query = $this->paginate($query, $args); |
69
|
|
|
|
70
|
|
|
foreach ($args as $field => $value) { |
71
|
|
|
$query = $query->where($field, $value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return TipSerializer::collection($query->get()); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.