-
Notifications
You must be signed in to change notification settings - Fork 223
Added paginator helper to convert first/after and before/last to offset/limit #78
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Overblog\GraphQLBundle\Relay\Connection; | ||
|
||
use Overblog\GraphQLBundle\Definition\Argument; | ||
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; | ||
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; | ||
|
||
class Paginator | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $fetcher; | ||
|
||
/** | ||
* @param callable $fetcher | ||
*/ | ||
public function __construct(callable $fetcher) | ||
{ | ||
$this->fetcher = $fetcher; | ||
} | ||
|
||
/** | ||
* @param Argument|array $args | ||
* @param int|callable $total | ||
* | ||
* @return Connection | ||
*/ | ||
public function backward($args, $total) | ||
{ | ||
$args = $this->protectArgs($args); | ||
$limit = $args['last']; | ||
$offset = max(0, ConnectionBuilder::getOffsetWithDefault($args['before'], $total) - $limit); | ||
|
||
$entities = call_user_func($this->fetcher, $offset, $limit); | ||
|
||
return ConnectionBuilder::connectionFromArraySlice($entities, $args, [ | ||
'sliceStart' => $offset, | ||
'arrayLength' => $total, | ||
]); | ||
} | ||
|
||
/** | ||
* @param Argument|array $args | ||
* | ||
* @return Connection | ||
*/ | ||
public function forward($args) | ||
{ | ||
$args = $this->protectArgs($args); | ||
$limit = $args['first']; | ||
$offset = ConnectionBuilder::getOffsetWithDefault($args['after'], 0); | ||
|
||
// The extra fetched element is here to determine if there is a next page. | ||
$entities = call_user_func($this->fetcher, $offset, $limit + 1); | ||
|
||
return ConnectionBuilder::connectionFromArraySlice($entities, $args, [ | ||
'sliceStart' => $offset, | ||
'arrayLength' => $offset + count($entities), | ||
]); | ||
} | ||
|
||
/** | ||
* @param Argument|array $args | ||
* @param int|callable $total | ||
* | ||
* @return Connection | ||
*/ | ||
public function auto($args, $total) | ||
{ | ||
$args = $this->protectArgs($args); | ||
|
||
if ($args['last']) { | ||
return $this->backward($args, is_callable($total) ? call_user_func($total) : $total); | ||
} else { | ||
return $this->forward($args); | ||
} | ||
} | ||
|
||
/** | ||
* @param Argument|array $args | ||
* | ||
* @return Argument | ||
*/ | ||
private function protectArgs($args) | ||
{ | ||
return $args instanceof Argument ? $args : new Argument($args); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Overblog\GraphQLBundle\Tests\Relay\Connection; | ||
|
||
use Overblog\GraphQLBundle\Definition\Argument; | ||
use Overblog\GraphQLBundle\Relay\Connection\Paginator; | ||
|
||
class PaginatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testForward() | ||
{ | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(0, $offset); | ||
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available | ||
|
||
return array_fill(0, 6, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->forward(new Argument(['first' => 5]))->edges); | ||
} | ||
|
||
public function testForwardAfter() | ||
{ | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(5, $offset); | ||
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available | ||
|
||
return array_fill(0, 6, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->forward(new Argument(['first' => 5, 'after' => base64_encode('arrayconnection:5') ]))->edges); | ||
} | ||
|
||
public function testBackward() | ||
{ | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(5, $offset); | ||
$this->assertSame(5, $limit); | ||
|
||
return array_fill(0, 5, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->backward(new Argument(['last' => 5]), 10)->edges); | ||
} | ||
|
||
public function testBackwardBefore() | ||
{ | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(0, $offset); | ||
$this->assertSame(5, $limit); | ||
|
||
return array_fill(0, 5, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->backward(new Argument(['last' => 5, 'before' => base64_encode('arrayconnection:5')]), 10)->edges); | ||
} | ||
|
||
public function testAuto() | ||
{ | ||
// Backward | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(5, $offset); | ||
$this->assertSame(5, $limit); | ||
|
||
return array_fill(0, 5, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->auto(new Argument(['last' => 5]), 10)->edges); | ||
|
||
// Forward | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(0, $offset); | ||
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available | ||
|
||
return array_fill(0, 5, 'item'); | ||
}); | ||
|
||
$this->assertCount(5, $paginator->auto(new Argument(['first' => 5]), 10)->edges); | ||
|
||
// Backward + callable | ||
$paginator = new Paginator(function ($offset, $limit) { | ||
$this->assertSame(5, $offset); | ||
$this->assertSame(5, $limit); | ||
|
||
return array_fill(0, 5, 'item'); | ||
}); | ||
|
||
$countCalled = false; | ||
$result = $paginator->auto(new Argument(['last' => 5]), function () use (&$countCalled) { | ||
$countCalled = true; | ||
return 10; | ||
}); | ||
|
||
$this->assertTrue($countCalled); | ||
$this->assertCount(5, $result->edges); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
toArgument
would be a better nameThere 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.
private method we can renames it easily before next release without BC