|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Query\Node; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
14
|
|
|
use GraphQL\Error\Error; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Extension\ExtensionManager; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Resolver to fetch a simple list of nodes without pagination, edges etc |
|
22
|
|
|
*/ |
|
23
|
|
|
class AllNodes extends AbstractResolver |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $queryAlias = 'o'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $entity; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var QueryDefinition |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $queryDefinition; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ObjectDefinition |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $objectDefinition; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array[] $args |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
* |
|
50
|
|
|
* @throws Error |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __invoke($args = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$orderBy = $args['orderBy'] ?? []; |
|
55
|
|
|
|
|
56
|
|
|
$this->initialize(); |
|
57
|
|
|
$qb = $this->createQuery(); |
|
58
|
|
|
$this->applyOrderBy($qb, $orderBy); |
|
59
|
|
|
|
|
60
|
|
|
$this->configureQuery($qb); |
|
61
|
|
|
foreach ($this->container->get(ExtensionManager::class)->getExtensions() as $extension) { |
|
62
|
|
|
$extension->configureQuery($qb, $this, $this->context); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $qb->getQuery()->getResult(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* initialize |
|
70
|
|
|
*/ |
|
71
|
10 |
|
public function initialize() |
|
72
|
|
|
{ |
|
73
|
10 |
|
$this->queryDefinition = $this->context->getDefinition(); |
|
74
|
10 |
|
$this->entity = $this->context->getNodeDefinition()->getClass(); |
|
75
|
10 |
|
$this->objectDefinition = $this->context->getNodeDefinition(); |
|
76
|
10 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param QueryBuilder $qb |
|
80
|
|
|
*/ |
|
81
|
9 |
|
public function configureQuery(QueryBuilder $qb) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
//implements on childs to customize the query |
|
84
|
9 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param QueryBuilder $qb |
|
88
|
|
|
* @param array $orderBy |
|
89
|
|
|
* |
|
90
|
|
|
* @throws Error |
|
91
|
|
|
*/ |
|
92
|
10 |
|
protected function applyOrderBy(QueryBuilder $qb, $orderBy) |
|
93
|
|
|
{ |
|
94
|
10 |
|
$refClass = new \ReflectionClass($this->entity); |
|
95
|
|
|
//TODO: allow sort using nested entities, e.g. profile.username |
|
96
|
10 |
|
foreach ($orderBy as $order) { |
|
97
|
7 |
|
$order->getField(); |
|
98
|
7 |
|
if ($this->objectDefinition->hasField($order->getField())) { |
|
99
|
7 |
|
$fieldDefinition = $this->objectDefinition->getField($order->getField()); |
|
100
|
7 |
|
if ($fieldDefinition->getOriginType() === \ReflectionProperty::class) { |
|
101
|
7 |
|
if ($refClass->hasProperty($fieldDefinition->getOriginName())) { |
|
102
|
7 |
|
$qb->addOrderBy($this->queryAlias.'.'.$fieldDefinition->getOriginName(), $order->getDirection()); |
|
103
|
|
|
|
|
104
|
7 |
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new Error(sprintf('The field "%s" its not valid to order in "%s"', $order->getField(), $this->queryDefinition->getName())); |
|
110
|
|
|
} |
|
111
|
10 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return QueryBuilder |
|
115
|
|
|
*/ |
|
116
|
10 |
|
protected function createQuery(): QueryBuilder |
|
117
|
|
|
{ |
|
118
|
10 |
|
return $this->getManager() |
|
119
|
10 |
|
->getRepository($this->entity) |
|
120
|
10 |
|
->createQueryBuilder($this->queryAlias); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.