-
-
Notifications
You must be signed in to change notification settings - Fork 465
Support unlimited pagination results when first is -1 or negative #2739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| 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; | ||
|
|
||
|
|
@@ -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 ( | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -126,6 +126,12 @@ public function applyToBuilder(QueryBuilder|ScoutBuilder|EloquentBuilder|Relatio | |
| ? 'simplePaginate' | ||
| : 'paginate'; | ||
|
|
||
| if ($methodName == 'paginate' && $this->first < 0) { | ||
|
||
| $results = $builder->get(['*']); | ||
| $total = $results->count(); | ||
| return new NegativePerPageLengthAwarePaginator($results, $total); | ||
| } | ||
|
|
||
| if ($builder instanceof ScoutBuilder) { | ||
| return $builder->{$methodName}($this->first, 'page', $this->page); | ||
| } | ||
|
|
||



There was a problem hiding this comment.
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.