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 Ynlo\GraphQLBundle\Annotation as GraphQL; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
17
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @GraphQL\Query(list=true) |
21
|
|
|
* @GraphQL\Argument(name="ids", type="[ID!]!") |
22
|
|
|
*/ |
23
|
|
|
class Nodes extends AbstractResolver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param ID[]|mixed[] $ids |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
2 |
|
public function __invoke($ids) |
31
|
|
|
{ |
32
|
2 |
|
if (empty($ids)) { |
33
|
|
|
return []; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
$types = []; |
37
|
2 |
|
$expectedResultsOrder = []; |
38
|
|
|
|
39
|
2 |
|
if (current($ids) instanceof ID) { |
40
|
1 |
|
$searchField = 'id'; |
41
|
1 |
|
foreach ($ids as $id) { |
42
|
1 |
|
$types[$id->getNodeType()][] = $id->getDatabaseId(); |
43
|
1 |
|
$expectedResultsOrder[md5($id->getNodeType().$id->getDatabaseId())] = null; |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
//when use a different field to fetch, |
47
|
|
|
//@see QueryGet::fetchBy |
48
|
1 |
|
$type = $this->getContext()->getDefinition()->getType(); |
49
|
1 |
|
$objectDefinition = $this->getContext()->getDefinitionManager()->getType($type); |
50
|
|
|
|
51
|
|
|
/** @var ArgumentDefinition $arg */ |
52
|
1 |
|
$arg = array_values($this->getContext()->getDefinition()->getArguments())[0]; |
53
|
|
|
|
54
|
1 |
|
$field = null; |
55
|
1 |
|
if ($objectDefinition->hasField($arg->getName())) { |
56
|
|
|
$field = $objectDefinition->getField($arg->getName()); |
57
|
1 |
|
} elseif ($objectDefinition->hasField(Inflector::singularize($arg->getName()))) { //by convention, singularize |
58
|
1 |
|
$field = $objectDefinition->getField(Inflector::singularize($arg->getName())); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (null === $field) { |
62
|
|
|
throw new \RuntimeException(sprintf('Can`t resolve the field `%s` inside type `%s`', $arg->getName(), $type)); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$searchField = $field->getOriginName(); |
66
|
1 |
|
$types[$type] = $ids; |
67
|
1 |
|
foreach ($ids as $identifier) { |
68
|
1 |
|
$expectedResultsOrder[md5($type.$identifier)] = null; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
foreach ($types as $type => $searchValues) { |
73
|
2 |
|
if ($this->getContext()->getDefinitionManager()->hasType($type)) { |
74
|
2 |
|
foreach ($searchValues as $searchValue) { |
75
|
|
|
//TODO: improve this to find all nodes in the same Repo with only one query, NOTE: the order and empty results are very IMPORTANT! |
76
|
|
|
//The list of given id should match with the list of results including non-found nodes |
77
|
2 |
|
$entity = $this->getContext()->getDefinitionManager()->getClassForType($type); |
78
|
2 |
|
$result = $this->getManager()->getRepository($entity)->findOneBy([$searchField => $searchValue]); |
79
|
2 |
|
$expectedResultsOrder[md5($type.$searchValue)] = $result; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return array_values($expectedResultsOrder); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|