-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathPaginationType.php
100 lines (92 loc) · 3.73 KB
/
PaginationType.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
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types = 1);
namespace Rebing\GraphQL\Support;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Rebing\GraphQL\Support\Facades\GraphQL;
class PaginationType extends ObjectType
{
public function __construct(string $typeName, ?string $customName = null)
{
$name = $customName ?: $typeName . 'Pagination';
$underlyingType = GraphQL::type($typeName);
$config = [
'name' => $name,
'fields' => $this->getPaginationFields($underlyingType),
];
if (isset($underlyingType->config['model'])) {
$config['model'] = $underlyingType->config['model'];
}
parent::__construct($config);
}
protected function getPaginationFields(GraphQLType $underlyingType): array
{
return [
'data' => [
'type' => GraphQLType::nonNull(GraphQLType::listOf(GraphQLType::nonNull($underlyingType))),
'description' => 'List of items on the current page',
'resolve' => function (LengthAwarePaginator $data): Collection {
return $data->getCollection();
},
],
'total' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Number of total items selected by the query',
'resolve' => function (LengthAwarePaginator $data): int {
return $data->total();
},
'selectable' => false,
],
'per_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Number of items returned per page',
'resolve' => function (LengthAwarePaginator $data): int {
return $data->perPage();
},
'selectable' => false,
],
'current_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Current page of the cursor',
'resolve' => function (LengthAwarePaginator $data): int {
return $data->currentPage();
},
'selectable' => false,
],
'from' => [
'type' => GraphQLType::int(),
'description' => 'Number of the first item returned',
'resolve' => function (LengthAwarePaginator $data): ?int {
return $data->firstItem();
},
'selectable' => false,
],
'to' => [
'type' => GraphQLType::int(),
'description' => 'Number of the last item returned',
'resolve' => function (LengthAwarePaginator $data): ?int {
return $data->lastItem();
},
'selectable' => false,
],
'last_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'The last page (number of pages)',
'resolve' => function (LengthAwarePaginator $data): int {
return $data->lastPage();
},
'selectable' => false,
],
'has_more_pages' => [
'type' => GraphQLType::nonNull(GraphQLType::boolean()),
'description' => 'Determines if cursor has more pages after the current page',
'resolve' => function (LengthAwarePaginator $data): bool {
return $data->hasMorePages();
},
'selectable' => false,
],
];
}
}