|
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
|
|
|
} |
|
68
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.