Repository navigation
Expand file tree
/
Copy pathSearchQuery.php
More file actions
61 lines (52 loc) · 1.65 KB
/
Copy pathSearchQuery.php
File metadata and controls
61 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace App\GraphQL\Queries;
use App\Models\Organization;
use App\Models\Post;
use App\Models\User;
use Ayimdomnic\Laragraph\Facades\Laragraph;
use Ayimdomnic\Laragraph\Support\Query;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
/**
* `search` — returns a union, validates its input and declares a cost.
*/
class SearchQuery extends Query
{
public function type(): Type
{
return Type::nonNull(Type::listOf(Type::nonNull(Laragraph::type('SearchResult'))));
}
public function args(): array
{
return [
'term' => ['type' => Type::nonNull(Type::string())],
'limit' => ['type' => Type::int(), 'defaultValue' => 10],
];
}
public function rules(array $args = []): array
{
return [
'term' => ['required', 'string', 'min:2', 'max:100'],
'limit' => ['integer', 'between:1,25'],
];
}
/** Counts towards laragraph.security.query_max_complexity. */
public function complexity(): ?int
{
return 20;
}
public function resolve(mixed $root, array $args, mixed $context, ResolveInfo $info): mixed
{
$like = '%'.$args['term'].'%';
$limit = $args['limit'];
return [
...Organization::where('name', 'like', $like)->limit($limit)->get(),
...Post::visibleTo($context->user())->where('title', 'like', $like)->limit($limit)->get(),
...User::where('name', 'like', $like)
->where('organization_id', $context->user()?->organization_id ?? 0)
->limit($limit)
->get(),
];
}
}