Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Pagination/NegativePerPageLengthAwarePaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting is off here and messed up in other places.


namespace Nuwave\Lighthouse\Pagination;

use Illuminate\Pagination\LengthAwarePaginator;

/** @extends \Illuminate\Pagination\LengthAwarePaginator<array-key, mixed> */
class NegativePerPageLengthAwarePaginator extends LengthAwarePaginator
{
public function __construct($results, int $total)
{
$this->total = $total;
$this->perPage = $total;
$this->lastPage = 1;
$this->currentPage = 1;
$this->items = $results;
}
}
16 changes: 11 additions & 5 deletions src/Pagination/PaginationArgs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Nuwave\Lighthouse\Pagination;

Expand Down Expand Up @@ -38,9 +40,7 @@ public static function extractArgs(array $args, ResolveInfo $resolveInfo, Pagina
// Handles cases "paginate" and "simple", which both take the same args.
: Arr::get($args, 'page') ?? 1;

if ($first < 0) {
throw new Error(self::requestedLessThanZeroItems($first));
}


// Make sure the maximum pagination count is not exceeded
if (
Expand Down Expand Up @@ -85,7 +85,7 @@ protected static function optimalPaginationType(PaginationType $proposedType, Re
// the total counts, following queries may need them - and use the same cached value.
$hasCacheDirective = $resolveInfo->argumentSet
->directives
->contains(static fn (Directive $directive): bool => $directive instanceof CacheDirective);
->contains(static fn(Directive $directive): bool => $directive instanceof CacheDirective);
if ($hasCacheDirective) {
return $proposedType;
}
Expand Down Expand Up @@ -126,6 +126,12 @@ public function applyToBuilder(QueryBuilder|ScoutBuilder|EloquentBuilder|Relatio
? 'simplePaginate'
: 'paginate';

if ($methodName == 'paginate' && $this->first < 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first: null seems much cleaner to me. Negative values could be the result of unchecked arithmetic errors, whereas null is much more explicit and discrete.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For support of first: null, We need to modify in PaginationManipulator from first: Int! to first: Int. I am not sure, if that breaks other already deployed.

Sorry that I am new to php-cs-fixer and I am trying that.
image
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Also found this Unit Test for checking if first is Int!.

So, I am pretty much confused.

$results = $builder->get(['*']);
$total = $results->count();
return new NegativePerPageLengthAwarePaginator($results, $total);
}

if ($builder instanceof ScoutBuilder) {
return $builder->{$methodName}($this->first, 'page', $this->page);
}
Expand Down