Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Relay/Connection/Output/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ final class Connection
/** @var PageInfo */
public $pageInfo;

/** @var int */
public $totalCount;

public function __construct(array $edges, PageInfo $pageInfo)
{
$this->edges = $edges;
Expand Down
38 changes: 35 additions & 3 deletions Relay/Connection/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Paginator
*/
private $promise;

/**
* @var int
*/
private $totalCount;

/**
* @param callable $fetcher
* @param bool $promise
Expand All @@ -49,7 +54,7 @@ public function __construct(callable $fetcher, $promise = self::MODE_REGULAR)
*/
public function backward($args, $total, array $callableArgs = [])
{
$total = is_callable($total) ? call_user_func_array($total, $callableArgs) : $total;
$total = $this->computeTotalCount($total, $callableArgs);

$args = $this->protectArgs($args);
$limit = $args['last'];
Expand Down Expand Up @@ -107,10 +112,20 @@ public function auto($args, $total, $callableArgs = [])
$args = $this->protectArgs($args);

if ($args['last']) {
return $this->backward($args, $total, $callableArgs);
$connection = $this->backward($args, $total, $callableArgs);
} else {
return $this->forward($args);
$connection = $this->forward($args);
}

if ($this->promise) {
$connection->then(function (Connection $connection) use ($total, $callableArgs) {
$connection->totalCount = $this->computeTotalCount($total, $callableArgs);
});
} else {
$connection->totalCount = $this->computeTotalCount($total, $callableArgs);
}

return $connection;
}

/**
Expand All @@ -137,4 +152,21 @@ private function protectArgs($args)
{
return $args instanceof Argument ? $args : new Argument($args);
}

/**
* @param int $total
* @param array $callableArgs
*
* @return int|mixed
*/
private function computeTotalCount($total, array $callableArgs = [])
{
if ($this->totalCount !== null) {
return $this->totalCount;
}

$this->totalCount = is_callable($total) ? call_user_func_array($total, $callableArgs) : $total;

return $this->totalCount;
}
}
7 changes: 6 additions & 1 deletion Tests/Relay/Connection/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ public function testTotalCallableWithArguments()
['array' => $this->data]
);

$this->assertSame(count($this->data), $result->totalCount);

$this->assertCount(4, $result->edges);
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result);
$this->assertTrue($result->pageInfo->hasPreviousPage);
Expand All @@ -292,7 +294,10 @@ public function testPromiseMode()
->getMock()
;

$promise->expects($this->once())->method('then');
$promise
->expects($this->exactly(2))
->method('then')
->willReturnSelf();

$paginator = new Paginator(function ($offset, $limit) use ($promise) {
$this->assertSame(0, $offset);
Expand Down