1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DutchCodingCompany\CursorPagination\Pagination; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use DutchCodingCompany\CursorPagination\CursorPaginator; |
8
|
|
|
use Nuwave\Lighthouse\Pagination\Cursor; |
9
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
10
|
|
|
|
11
|
|
|
class ConnectionField |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Resolve page info for connection. |
15
|
|
|
* |
16
|
|
|
* @return array<string, mixed> |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
public function pageInfoResolver(CursorPaginator $paginator): array |
19
|
|
|
{ |
20
|
|
|
return $paginator->toArray(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Resolve edges for connection. |
25
|
|
|
* |
26
|
|
|
* @param \Illuminate\Pagination\LengthAwarePaginator<mixed> $paginator |
|
|
|
|
27
|
|
|
* @param array<string, mixed> $args |
28
|
|
|
*/ |
29
|
|
|
public function edgeResolver(CursorPaginator $paginator, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
// We know this must be a list, as it is constructed this way during schema manipulation |
32
|
|
|
/** @var \GraphQL\Type\Definition\ListOfType $listOfType */ |
33
|
|
|
$listOfType = $resolveInfo->returnType; |
34
|
|
|
|
35
|
|
|
// We also know this is one of those two return types |
36
|
|
|
/** @var \GraphQL\Type\Definition\ObjectType|\GraphQL\Type\Definition\InterfaceType $objectLikeType */ |
37
|
|
|
$objectLikeType = $listOfType->ofType; |
38
|
|
|
$returnTypeFields = $objectLikeType->getFields(); |
39
|
|
|
|
40
|
|
|
// @phpstan-ignore-next-line static refers to the wrong class because it is a proxied method call |
41
|
|
|
return $paginator->values() |
42
|
|
|
->map(function ($item, $index) use ($returnTypeFields): array { |
|
|
|
|
43
|
|
|
$data = []; |
44
|
|
|
|
45
|
|
|
foreach ($returnTypeFields as $field) { |
46
|
|
|
switch ($field->name) { |
47
|
|
|
case 'node': |
48
|
|
|
$data['node'] = $item; |
49
|
|
|
break; |
50
|
|
|
|
51
|
|
|
default: |
52
|
|
|
// All other fields on the return type are assumed to be part |
53
|
|
|
// of the edge, so we try to locate them in the pivot attribute |
54
|
|
|
if (isset($item->pivot->{$field->name})) { |
55
|
|
|
$data[$field->name] = $item->pivot->{$field->name}; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $data; |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.