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
10 changes: 7 additions & 3 deletions Relay/Connection/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ public function __construct(callable $fetcher)
/**
* @param Argument|array $args
* @param int|callable $total
* @param array $callableArgs
*
* @return Connection
*/
public function backward($args, $total)
public function backward($args, $total, array $callableArgs = [])
{
$total = is_callable($total) ? call_user_func_array($total, $callableArgs) : $total;

$args = $this->protectArgs($args);
$limit = $args['last'];
$offset = max(0, ConnectionBuilder::getOffsetWithDefault($args['before'], $total) - $limit);
Expand Down Expand Up @@ -79,15 +82,16 @@ public function forward($args)
/**
* @param Argument|array $args
* @param int|callable $total
* @param array $callableArgs
*
* @return Connection
*/
public function auto($args, $total)
public function auto($args, $total, $callableArgs = [])
{
$args = $this->protectArgs($args);

if ($args['last']) {
return $this->backward($args, is_callable($total) ? call_user_func($total) : $total);
return $this->backward($args, $total, $callableArgs);
} else {
return $this->forward($args);
}
Expand Down
42 changes: 42 additions & 0 deletions Resources/doc/helpers/relay-paginator.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,45 @@ The callback function will receive:
- `$limit = 3`

And it must return at least `['C','D','E']`

#### With a `last` relay parameter

```php
<?php

use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Relay\Connection\Paginator;

class DataBackend
{
public $data = ['A', 'B', 'C', 'D', 'E'];

public function getData($offset = 0)
{
return array_slice($this->data, $offset);
}

public function count($array)
{
return count($array);
}
}

$backend = new DataBackend();

$paginator = new Paginator(function ($offset, $limit) use ($backend) {
return $backend->getData($offset);
});

$result = $paginator->backward(
new Argument(
[
'last' => 4,
]
),
[$backend, 'count'],
['array' => $backend->getData]
);
```

You should get the 4 last items of the _data set_.
23 changes: 23 additions & 0 deletions Tests/Relay/Connection/PaginatorBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Tests\Relay\Connection;

/**
* Class PaginatorBackend.
*/
class PaginatorBackend
{
public function count($array)
{
return count($array);
}
}
29 changes: 29 additions & 0 deletions Tests/Relay/Connection/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,33 @@ public function testAutoBackwardWithCallable()
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result);
$this->assertTrue($result->pageInfo->hasPreviousPage);
}

public function testTotalCallableWithArguments()
{
$paginatorBackend = new PaginatorBackend();

$callable = [
$paginatorBackend,
'count',
];

$this->assertSame(5, call_user_func_array($callable, ['array' => $this->data]));

$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(1, $offset);
$this->assertSame(4, $limit);

return $this->getData($offset);
});

$result = $paginator->auto(
new Argument(['last' => 4]),
$callable,
['array' => $this->data]
);

$this->assertCount(4, $result->edges);
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result);
$this->assertTrue($result->pageInfo->hasPreviousPage);
}
}