|
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\Article; |
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
use App\GraphQL\Feature\Paginator; |
|
15
|
|
|
use App\GraphQL\Feature\SelectById; |
|
16
|
|
|
use GraphQL\Type\Definition\ListOfType; |
|
17
|
|
|
use App\GraphQL\Serializers\ArticleSerializer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ArticlesQuery. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ArticlesQuery extends AbstractQuery |
|
23
|
|
|
{ |
|
24
|
|
|
use Paginator; |
|
25
|
|
|
use SelectById; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $attributes = [ |
|
31
|
|
|
'name' => 'Article list query', |
|
32
|
|
|
'description' => 'Returns a list of available articles', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return ListOfType |
|
37
|
|
|
*/ |
|
38
|
|
|
public function type(): ListOfType |
|
39
|
|
|
{ |
|
40
|
|
|
return Type::listOf(\GraphQL::type('Article')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $root |
|
45
|
|
|
* @param array $args |
|
46
|
|
|
* @return \Traversable |
|
47
|
|
|
*/ |
|
48
|
|
|
public function resolve($root, array $args = []): \Traversable |
|
49
|
|
|
{ |
|
50
|
|
|
$query = $this->queryFor(Article::class, $args); |
|
51
|
|
|
|
|
52
|
|
|
$this->whenExists($args, 'slug', function (array $slug) use ($query) { |
|
53
|
|
|
return $query->whereIn('slug', $slug); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$count = $query->published()->count(); |
|
57
|
|
|
$query = $query->latestPublished(); |
|
58
|
|
|
|
|
59
|
|
|
return $this->paginate($query, $count) |
|
60
|
|
|
->withArgs($args) |
|
61
|
|
|
->use(ArticleSerializer::class) |
|
62
|
|
|
->as('articles'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function queryArguments(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
|
|
'slug' => [ |
|
72
|
|
|
'type' => Type::listOf(Type::string()), |
|
73
|
|
|
'description' => 'Article slug', |
|
74
|
|
|
], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|