|
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\Common\Util\Inflector; |
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Annotation as GraphQL; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @GraphQL\Query(type="[]") |
|
23
|
|
|
* @GraphQL\Argument(name="ids", type="[ID!]!") |
|
24
|
|
|
*/ |
|
25
|
|
|
class Nodes extends AbstractResolver |
|
26
|
|
|
{ |
|
27
|
|
|
protected $fetchBy = 'id'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ID[]|mixed[] $ids |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function __invoke($ids) |
|
35
|
|
|
{ |
|
36
|
3 |
|
if (empty($ids)) { |
|
37
|
|
|
return []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
$types = []; |
|
41
|
3 |
|
$expectedResultsOrder = []; |
|
42
|
|
|
|
|
43
|
3 |
|
if (current($ids) instanceof ID) { |
|
44
|
2 |
|
foreach ($ids as $id) { |
|
45
|
2 |
|
$types[$id->getNodeType()][] = $id->getDatabaseId(); |
|
46
|
2 |
|
$expectedResultsOrder[md5($id->getNodeType().$id->getDatabaseId())] = null; |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
|
|
//when use a different field to fetch, |
|
50
|
|
|
//@see QueryGet::fetchBy |
|
51
|
1 |
|
$type = $this->getContext()->getDefinition()->getType(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var FieldsAwareDefinitionInterface $objectDefinition */ |
|
54
|
1 |
|
$objectDefinition = $this->getContext()->getEndpoint()->getType($type); |
|
55
|
|
|
|
|
56
|
|
|
/** @var ArgumentDefinition $arg */ |
|
57
|
1 |
|
$arg = array_values($this->getContext()->getDefinition()->getArguments())[0]; |
|
58
|
|
|
|
|
59
|
1 |
|
$field = null; |
|
60
|
1 |
|
if ($objectDefinition->hasField($arg->getName())) { |
|
61
|
|
|
$field = $objectDefinition->getField($arg->getName()); |
|
62
|
1 |
|
} elseif ($objectDefinition->hasField(Inflector::singularize($arg->getName()))) { //by convention, singularize |
|
63
|
1 |
|
$field = $objectDefinition->getField(Inflector::singularize($arg->getName())); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if (null === $field) { |
|
67
|
|
|
throw new \RuntimeException(sprintf('Can`t resolve the field `%s` inside type `%s`', $arg->getName(), $type)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$types[$type] = $ids; |
|
71
|
1 |
|
foreach ($ids as $identifier) { |
|
72
|
1 |
|
$expectedResultsOrder[md5($type.$identifier)] = null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
foreach ($types as $type => $searchValues) { |
|
77
|
3 |
|
if ($this->getContext()->getEndpoint()->hasType($type)) { |
|
78
|
3 |
|
$entity = $this->getContext()->getEndpoint()->getClassForType($type); |
|
79
|
|
|
|
|
80
|
|
|
/** @var EntityRepository $repo */ |
|
81
|
3 |
|
$repo = $this->getManager()->getRepository($entity); |
|
82
|
|
|
|
|
83
|
3 |
|
$findBy = sprintf('o.%s', $this->fetchBy); |
|
84
|
|
|
|
|
85
|
3 |
|
$qb = $repo->createQueryBuilder('o', $findBy); |
|
86
|
3 |
|
$entities = $qb->where($qb->expr()->in($findBy, $searchValues)) |
|
87
|
3 |
|
->getQuery() |
|
88
|
3 |
|
->getResult(); |
|
89
|
|
|
|
|
90
|
3 |
|
foreach ($entities as $searchValue => $entity) { |
|
91
|
3 |
|
$expectedResultsOrder[md5($type.$searchValue)] = $entity; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
return array_values($expectedResultsOrder); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|