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\Model\OrderBy; |
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
|
|
|
//keep orderBy for BC |
55
|
|
|
$orderBy = array_merge($args['orderBy'] ?? [], $args['order'] ?? []); |
56
|
|
|
|
57
|
|
|
$this->initialize(); |
58
|
|
|
$qb = $this->createQuery(); |
59
|
|
|
$this->applyOrderBy($qb, $orderBy); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->configureQuery($qb); |
62
|
|
|
foreach ($this->extensions as $extension) { |
63
|
|
|
$extension->configureQuery($qb, $this, $this->context); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $qb->getQuery()->getResult(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* initialize |
71
|
|
|
*/ |
72
|
|
|
public function initialize() |
73
|
|
|
{ |
74
|
|
|
$this->queryDefinition = $this->context->getDefinition(); |
75
|
|
|
$this->entity = $this->context->getNode()->getClass(); |
76
|
|
|
$this->objectDefinition = $this->context->getNode(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param QueryBuilder $qb |
81
|
|
|
*/ |
82
|
|
|
public function configureQuery(QueryBuilder $qb) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
//implements on childs to customize the query |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param QueryBuilder $qb |
89
|
|
|
* @param array|OrderBy[] $orderBy |
90
|
|
|
* |
91
|
|
|
* @throws Error |
92
|
|
|
* |
93
|
|
|
* @deprecated only All nodes with pagination should have orderBy |
94
|
|
|
*/ |
95
|
|
|
protected function applyOrderBy(QueryBuilder $qb, $orderBy) |
96
|
|
|
{ |
97
|
|
|
foreach ($orderBy as $order) { |
98
|
|
|
$column = $order->getField(); |
99
|
|
|
if (strpos($column, '.') !== false) { |
100
|
|
|
[$relation, $column] = explode('.', $column); |
101
|
|
|
$childAlias = 'orderBy'.ucfirst($relation); |
102
|
|
|
$qb->leftJoin("{$this->queryAlias}.{$relation}", $childAlias); |
103
|
|
|
$qb->addOrderBy("$childAlias.$column", $order->getDirection()); |
104
|
|
|
} else { |
105
|
|
|
if ($this->objectDefinition && $this->objectDefinition->hasField($column)) { |
106
|
|
|
$column = $this->objectDefinition->getField($column)->getOriginName() ?: $column; |
107
|
|
|
} |
108
|
|
|
$qb->addOrderBy("{$this->queryAlias}.$column", $order->getDirection()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return QueryBuilder |
115
|
|
|
*/ |
116
|
|
|
protected function createQuery(): QueryBuilder |
117
|
|
|
{ |
118
|
|
|
return $this->getManager() |
119
|
|
|
->getRepository($this->entity) |
120
|
|
|
->createQueryBuilder($this->queryAlias); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.