|
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\Definition\Loader\Annotation; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Util\Inflector; |
|
14
|
|
|
use Ynlo\GraphQLBundle\Annotation; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Query\Node\Node; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Query\Node\Nodes; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Resolve queries |
|
23
|
|
|
*/ |
|
24
|
|
|
class QueryGetNodeAnnotationParser implements AnnotationParserInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use AnnotationReaderAwareTrait; |
|
27
|
|
|
use AnnotationParserHelper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function supports($annotation): bool |
|
33
|
|
|
{ |
|
34
|
1 |
|
return $annotation instanceof Annotation\QueryGet; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function parse($annotation, \ReflectionClass $refClass, DefinitionManager $definitionManager) |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var Annotation\QueryGet $annotation */ |
|
43
|
1 |
View Code Duplication |
if ($annotation->name) { |
|
|
|
|
|
|
44
|
|
|
$name = $annotation->name; |
|
45
|
|
|
} else { |
|
46
|
1 |
|
$name = lcfirst($this->getDefaultName($refClass, $definitionManager)); |
|
47
|
|
|
} |
|
48
|
1 |
|
$this->createGetNoneQuery($name, $annotation, $refClass, $definitionManager); |
|
49
|
|
|
|
|
50
|
1 |
|
if ($annotation->pluralQuery) { |
|
51
|
1 |
|
if ($annotation->pluralQueryName) { |
|
52
|
|
|
$name = $annotation->pluralQueryName; |
|
53
|
|
|
} else { |
|
54
|
1 |
|
$name = Inflector::pluralize(lcfirst($this->getDefaultName($refClass, $definitionManager))); |
|
55
|
|
|
} |
|
56
|
1 |
|
$this->createGetNoneQuery($name, $annotation, $refClass, $definitionManager, true); |
|
57
|
|
|
} |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @param mixed $annotation |
|
63
|
|
|
* @param \ReflectionClass $refClass |
|
64
|
|
|
* @param DefinitionManager $definitionManager |
|
65
|
|
|
* @param bool $plural |
|
66
|
|
|
*/ |
|
67
|
1 |
|
protected function createGetNoneQuery( |
|
68
|
|
|
$name, |
|
69
|
|
|
$annotation, |
|
70
|
|
|
\ReflectionClass $refClass, |
|
71
|
|
|
DefinitionManager $definitionManager, |
|
72
|
|
|
$plural = false |
|
73
|
|
|
) { |
|
74
|
|
|
/** @var Annotation\QueryGet $annotation */ |
|
75
|
1 |
|
$query = new QueryDefinition(); |
|
76
|
1 |
|
$query->setName($name); |
|
77
|
|
|
|
|
78
|
1 |
|
$objectDefinition = null; |
|
79
|
|
|
/** @var Annotation\ObjectType $objectType */ |
|
80
|
1 |
|
if ($objectType = $this->reader->getClassAnnotation($refClass, Annotation\ObjectType::class)) { |
|
|
|
|
|
|
81
|
1 |
|
$typeName = $definitionManager->getTypeForClass($refClass->getName()); |
|
82
|
1 |
|
$objectDefinition = $definitionManager->getType($typeName); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$fetchBy = $annotation->fetchBy ?? 'id'; |
|
86
|
|
|
|
|
87
|
1 |
|
if (!$objectDefinition->hasField($fetchBy)) { |
|
88
|
|
|
$error = sprintf('The object type "%s" does not have a field "%s" to fetch record using this field.', $objectDefinition->getName(), $fetchBy); |
|
89
|
|
|
throw new \InvalidArgumentException($error); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$fieldDefinition = $objectDefinition->getField($fetchBy); |
|
93
|
1 |
|
$argument = new ArgumentDefinition(); |
|
94
|
1 |
|
$argument->setName($plural ? Inflector::pluralize($fieldDefinition->getName()) : $fieldDefinition->getName()); |
|
95
|
1 |
|
$argument->setType($fieldDefinition->getType()); |
|
96
|
1 |
|
$argument->setList($plural); |
|
97
|
1 |
|
$argument->setDescription($fieldDefinition->getDescription()); |
|
98
|
1 |
|
$argument->setInternalName($plural ? 'ids' : 'id'); |
|
99
|
1 |
|
$query->addArgument($argument); |
|
100
|
|
|
|
|
101
|
1 |
|
$query->setType($objectDefinition->getName()); |
|
102
|
1 |
|
$query->setList($plural); |
|
103
|
1 |
|
$query->setResolver($plural ? Nodes::class : Node::class); |
|
104
|
1 |
|
$query->setDeprecationReason($annotation->deprecationReason); |
|
105
|
1 |
|
$query->setDescription($annotation->description); |
|
106
|
|
|
|
|
107
|
1 |
|
$definitionManager->addQuery($query); |
|
108
|
1 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.