-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathSimplePaginationType.php
89 lines (81 loc) · 3.12 KB
/
SimplePaginationType.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Rebing\GraphQL\Support;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Rebing\GraphQL\Support\Facades\GraphQL;
class SimplePaginationType extends ObjectType
{
public function __construct(string $typeName, string $customName = null)
{
$name = $customName ?: $typeName.'SimplePagination';
$config = [
'name' => $name,
'fields' => $this->getPaginationFields($typeName),
];
$underlyingType = GraphQL::type($typeName);
if (isset($underlyingType->config['model'])) {
$config['model'] = $underlyingType->config['model'];
}
parent::__construct($config);
}
/**
* @param string $typeName
*
* @return array<string, array<string,mixed>>
*/
protected function getPaginationFields(string $typeName): array
{
return [
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'description' => 'List of items on the current page',
'resolve' => function (Paginator $data): Collection {
return $data->getCollection();
},
],
'per_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Number of items returned per page',
'resolve' => function (Paginator $data): int {
return $data->perPage();
},
'selectable' => false,
],
'current_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Current page of the cursor',
'resolve' => function (Paginator $data): int {
return $data->currentPage();
},
'selectable' => false,
],
'from' => [
'type' => GraphQLType::int(),
'description' => 'Number of the first item returned',
'resolve' => function (Paginator $data): ?int {
return $data->firstItem();
},
'selectable' => false,
],
'to' => [
'type' => GraphQLType::int(),
'description' => 'Number of the last item returned',
'resolve' => function (Paginator $data): ?int {
return $data->lastItem();
},
'selectable' => false,
],
'has_more_pages' => [
'type' => GraphQLType::nonNull(GraphQLType::boolean()),
'description' => 'Determines if cursor has more pages after the current page',
'resolve' => function (Paginator $data): bool {
return $data->hasMorePages();
},
'selectable' => false,
],
];
}
}