Skip to content

Commit ac792bf

Browse files
authored
Merge pull request #15 from moufmouf/resultiteratortype
Adding a result iterator type
2 parents cf9fb85 + 8d972d9 commit ac792bf

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/Registry/Registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* The role of the registry is to provide access to all GraphQL types.
1414
* If the type is not found, it can be queried from the container, or if not in the container, it can be created from the Registry itself.
1515
*
16+
* @deprecated Use TheCodingMachine\GraphQL\Controllers\Registry instead
1617
*/
1718
class Registry implements ContainerInterface
1819
{

src/ResultIteratorType.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Tdbm\GraphQL;
5+
6+
use TheCodingMachine\TDBM\ResultIterator;
7+
use Youshido\GraphQL\Config\Object\ObjectTypeConfig;
8+
use Youshido\GraphQL\Type\ListType\ListType;
9+
use Youshido\GraphQL\Type\NonNullType;
10+
use Youshido\GraphQL\Type\Object\AbstractObjectType;
11+
use Youshido\GraphQL\Type\Scalar\IntType;
12+
use Youshido\GraphQL\Type\Scalar\StringType;
13+
use Youshido\GraphQL\Type\TypeInterface;
14+
15+
/**
16+
* Type mapping a TDBM ResultIterator.
17+
* It allows easy pagination and sorting in the iterator.
18+
*/
19+
class ResultIteratorType extends AbstractObjectType
20+
{
21+
/**
22+
* @var TypeInterface
23+
*/
24+
private $beanType;
25+
26+
public function __construct(TypeInterface $beanType)
27+
{
28+
parent::__construct([]);
29+
$this->beanType = $beanType;
30+
}
31+
32+
/**
33+
* @param ObjectTypeConfig $config
34+
*/
35+
public function build($config)
36+
{
37+
$config->addField('count', [
38+
'type' => new IntType(),
39+
'description' => 'Returns the total number of items in the collection.',
40+
'resolve' => function (ResultIterator $source, $args, $info) {
41+
return $source->count();
42+
}
43+
]);
44+
$config->addField('items', [
45+
'type' => new NonNullType(new ListType(new NonNullType($this->beanType))),
46+
'description' => 'Returns the list of items in the collection.',
47+
'args' => [
48+
'limit' => new IntType(),
49+
'offset' => new IntType(),
50+
'order' => new StringType(),
51+
],
52+
'resolve' => function (ResultIterator $source, $args, $info) {
53+
if (isset($args['order'])) {
54+
$source = $source->withOrder($args['order']);
55+
}
56+
if (!isset($args['limit']) && isset($args['offset'])) {
57+
throw new GraphQLException('In "items" field, you can specify an offset without a limit.');
58+
}
59+
if (isset($args['limit']) || isset($args['offset'])) {
60+
$source = $source->take($args['offset'] ?? 0, $args['limit']);
61+
}
62+
63+
return $source->toArray();
64+
}
65+
]);
66+
}
67+
}

tests/GraphQLTypeGeneratorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
use TheCodingMachine\Tdbm\GraphQL\Fixtures\TestSchema;
1010
use TheCodingMachine\Tdbm\GraphQL\Registry\EmptyContainer;
1111
use TheCodingMachine\Tdbm\GraphQL\Registry\Registry;
12+
use TheCodingMachine\Tdbm\GraphQL\Tests\Beans\Country;
13+
use TheCodingMachine\Tdbm\GraphQL\Tests\DAOs\CountryDao;
1214
use TheCodingMachine\Tdbm\GraphQL\Tests\DAOs\UserDao;
15+
use TheCodingMachine\Tdbm\GraphQL\Tests\GraphQL\CountryType;
1316
use TheCodingMachine\Tdbm\GraphQL\Tests\GraphQL\Generated\AbstractCountryType;
1417
use TheCodingMachine\Tdbm\GraphQL\Tests\GraphQL\Generated\AbstractUserType;
1518
use TheCodingMachine\Tdbm\GraphQL\Tests\GraphQL\TdbmGraphQLTypeMapper;
1619
use TheCodingMachine\TDBM\TDBMService;
1720
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy as TdbmDefaultNamingStrategy;
1821
use PHPUnit\Framework\TestCase;
22+
use Youshido\GraphQL\Execution\Context\ExecutionContext;
1923
use Youshido\GraphQL\Execution\Processor;
24+
use Youshido\GraphQL\Execution\ResolveInfo;
25+
use Youshido\GraphQL\Schema\Schema;
2026
use Youshido\GraphQL\Type\Scalar\StringType;
2127

2228
class GraphQLTypeGeneratorTest extends TestCase
@@ -182,4 +188,32 @@ private function recursiveDelete(string $str) : bool
182188
}
183189
return false;
184190
}
191+
192+
public function testResultIteratorType()
193+
{
194+
$type = new ResultIteratorType(new CountryType(new Registry(new EmptyContainer())));
195+
196+
$tdbmService = self::getTDBMService();
197+
$countryDao = new CountryDao($tdbmService);
198+
199+
$countries = $countryDao->findAll();
200+
201+
$countField = $type->getField('count');
202+
$resolveInfo = $this->getMockBuilder(ResolveInfo::class)->disableOriginalConstructor()->getMock();
203+
$this->assertEquals(3, $countField->resolve($countries, [], $resolveInfo));
204+
205+
$itemsField = $type->getField('items');
206+
$result = $itemsField->resolve($countries, [], $resolveInfo);
207+
$this->assertCount(3, $result);
208+
$this->assertInstanceOf(Country::class, $result[0]);
209+
210+
$result = $itemsField->resolve($countries, ['order'=>'label'], $resolveInfo);
211+
$this->assertEquals('Jamaica', $result[1]->getLabel());
212+
213+
$result = $itemsField->resolve($countries, ['offset'=>1, 'limit'=>1], $resolveInfo);
214+
$this->assertCount(1, $result);
215+
216+
$this->expectException(GraphQLException::class);
217+
$result = $itemsField->resolve($countries, ['offset'=>1], $resolveInfo);
218+
}
185219
}

0 commit comments

Comments
 (0)