1
<?php
2

3
namespace Nuwave\Lighthouse\Pagination;
4

5
use GraphQL\Type\Definition\ResolveInfo;
6
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
7
use Illuminate\Support\Collection;
8
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
9

10
class ConnectionField
11
{
12
    /**
13
     * Resolve page info for connection.
14
     *
15
     * @return array<string, mixed>
16
     */
17 1
    public function pageInfoResolver(LengthAwarePaginator $paginator): array
18
    {
19
        /** @var int|null $firstItem Laravel type-hints are inaccurate here */
20 1
        $firstItem = $paginator->firstItem();
21
        /** @var int|null $lastItem Laravel type-hints are inaccurate here */
22 1
        $lastItem = $paginator->lastItem();
23

24
        return [
25 1
            'total' => $paginator->total(),
26 1
            'count' => $paginator->count(),
27 1
            'currentPage' => $paginator->currentPage(),
28 1
            'lastPage' => $paginator->lastPage(),
29 1
            'hasNextPage' => $paginator->hasMorePages(),
30 1
            'hasPreviousPage' => $paginator->currentPage() > 1,
31
            'startCursor' => $firstItem !== null
32 1
                ? Cursor::encode($firstItem)
33
                : null,
34
            'endCursor' => $lastItem !== null
35 1
                ? Cursor::encode($lastItem)
36
                : null,
37
        ];
38
    }
39

40
    /**
41
     * Resolve edges for connection.
42
     *
43
     * @param  \Illuminate\Pagination\LengthAwarePaginator<mixed>  $paginator
44
     * @param  array<string, mixed>  $args
45
     */
46 1
    public function edgeResolver(LengthAwarePaginator $paginator, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection
47
    {
48
        // We know this must be a list, as it is constructed this way during schema manipulation
49
        /** @var \GraphQL\Type\Definition\ListOfType $listOfType */
50 1
        $listOfType = $resolveInfo->returnType;
51

52
        // We also know this is one of those two return types
53
        /** @var \GraphQL\Type\Definition\ObjectType|\GraphQL\Type\Definition\InterfaceType $objectLikeType */
54 1
        $objectLikeType = $listOfType->ofType;
55 1
        $returnTypeFields = $objectLikeType->getFields();
56

57
        /** @var int|null $firstItem Laravel type-hints are inaccurate here */
58 1
        $firstItem = $paginator->firstItem();
59

60
        return $paginator
61 1
            ->values()
62
            ->map(function ($item, int $index) use ($returnTypeFields, $firstItem): array {
63 1
                $data = [];
64

65 1
                foreach ($returnTypeFields as $field) {
66 1
                    switch ($field->name) {
67 1
                        case 'cursor':
68 1
                            $data['cursor'] = Cursor::encode((int) $firstItem + $index);
69 1
                            break;
70

71 1
                        case 'node':
72 1
                            $data['node'] = $item;
73 1
                            break;
74

75
                        default:
76
                            // All other fields on the return type are assumed to be part
77
                            // of the edge, so we try to locate them in the pivot attribute
78 1
                            if (isset($item->pivot->{$field->name})) {
79 1
                                $data[$field->name] = $item->pivot->{$field->name};
80
                            }
81
                    }
82
                }
83

84 1
                return $data;
85 1
            });
86
    }
87
}

Read our documentation on viewing source code .

Loading