Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Commit ae0b3bc

Browse files
authored
Merge pull request #206 from treschelet/feature/relay
add field total to ConnectionType
2 parents b5f7a9d + 5c31527 commit ae0b3bc

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

src/Folklore/GraphQL/Relay/EdgesCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66

77
class EdgesCollection extends Collection
88
{
9+
protected $total = 0;
910
protected $startCursor = null;
1011
protected $endCursor = null;
1112
protected $hasNextPage = false;
1213
protected $hasPreviousPage = false;
1314

15+
public function setTotal($total)
16+
{
17+
$this->total = $total;
18+
}
19+
20+
public function getTotal()
21+
{
22+
return $this->total;
23+
}
24+
1425
public function setHasNextPage($hasNextPage)
1526
{
1627
$this->hasNextPage = $hasNextPage;

src/Folklore/GraphQL/Relay/Support/ConnectionType.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ protected function edgeType()
2323
protected function fields()
2424
{
2525
return [
26+
'total' => [
27+
'type' => Type::int(),
28+
'resolve' => function ($root) {
29+
return $this->getTotalFromRoot($root);
30+
}
31+
],
2632
'edges' => [
2733
'type' => Type::listOf($this->getEdgeObjectType()),
2834
'resolve' => function ($root) {
@@ -70,6 +76,16 @@ protected function getCursorFromNode($edge)
7076
return $resolveId($edge);
7177
}
7278

79+
protected function getTotalFromRoot($root)
80+
{
81+
$total = 0;
82+
if ($root instanceof EdgesCollection) {
83+
$total = $root->getTotal();
84+
}
85+
return $total;
86+
}
87+
88+
7389
protected function getEdgesFromRoot($root)
7490
{
7591
$cursor = $this->getStartCursorFromRoot($root);

src/Folklore/GraphQL/Relay/Support/Traits/ResolvesFromQueryBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ protected function resolveItemsFromQueryBuilder($query)
7777
return $query->get();
7878
}
7979

80-
protected function getCollectionFromItems($items, $offset, $limit, $hasPreviousPage, $hasNextPage)
80+
protected function getCollectionFromItems($items, $offset, $limit, $total, $hasPreviousPage, $hasNextPage)
8181
{
8282
$collection = new EdgesCollection($items);
83+
$collection->setTotal($total);
8384
$collection->setStartCursor($offset);
8485
$collection->setEndCursor($offset + $limit - 1);
8586
$collection->setHasNextPage($hasNextPage);
@@ -143,7 +144,7 @@ public function resolve($root, $args)
143144

144145
$resolveItemsArguments = array_merge([$query], $arguments);
145146
$items = call_user_func_array([$this, 'resolveItemsFromQueryBuilder'], $resolveItemsArguments);
146-
$collection = $this->getCollectionFromItems($items, $offset, $limit, $hasPreviousPage, $hasNextPage);
147+
$collection = $this->getCollectionFromItems($items, $offset, $limit, $count, $hasPreviousPage, $hasNextPage);
147148

148149
return $collection;
149150
}

tests/Relay/RelayEdgesCollectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ protected function getEnvironmentSetUp($app)
1717
$this->collection = new EdgesCollection();
1818
}
1919

20+
/**
21+
* Test set and get total
22+
*
23+
* @test
24+
* @covers ::setTotal
25+
* @covers ::getTotal
26+
*/
27+
public function testGetTotal()
28+
{
29+
$this->assertEquals(0, $this->collection->getTotal());
30+
$this->collection->setTotal(1);
31+
$this->assertEquals(1, $this->collection->getTotal());
32+
}
33+
2034
/**
2135
* Test set and get hasNextPage
2236
*

tests/Relay/RelayTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use GraphQL\Type\Definition\Type;
4+
use Folklore\GraphQL\Support\Type as BaseType;
35
use Folklore\GraphQL\Relay\Support\ConnectionField;
46

57
/**
@@ -54,4 +56,28 @@ public function testConnectionField()
5456
$this->assertInstanceOf(ConnectionField::class, $field);
5557
$this->assertEquals($name, $field->name);
5658
}
59+
60+
/**
61+
* Test connection field from edge type
62+
*
63+
* @test
64+
* @covers ::connectionFieldFromEdgeType
65+
*/
66+
public function testConnectionFieldFromEdgeType()
67+
{
68+
$name = 'testConnectionField';
69+
$edgeObjectType = new BaseType([
70+
'name' => 'Test',
71+
'fields' => [
72+
'id' => [
73+
'type' => Type::int()
74+
]
75+
]
76+
]);
77+
$field = $this->relay->connectionFieldFromEdgeType($edgeObjectType, [
78+
'name' => $name
79+
]);
80+
$this->assertInstanceOf(ConnectionField::class, $field);
81+
$this->assertEquals($name, $field->name);
82+
}
5783
}

0 commit comments

Comments
 (0)