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 Ynlo\GraphQLBundle\Annotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentAwareInterface; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
18
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
19
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Parse Query annotation to fetch queries |
23
|
|
|
*/ |
24
|
|
|
class QueryAnnotationParser implements AnnotationParserInterface |
25
|
|
|
{ |
26
|
|
|
use AnnotationReaderAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
1 |
|
public function supports($annotation): bool |
32
|
|
|
{ |
33
|
1 |
|
return $annotation instanceof Annotation\Query; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
40
|
|
|
{ |
41
|
|
|
/** @var Annotation\Query $annotation */ |
42
|
1 |
|
$query = new QueryDefinition(); |
43
|
|
|
|
44
|
1 |
View Code Duplication |
if ($annotation->name) { |
|
|
|
|
45
|
1 |
|
$query->setName($annotation->name); |
46
|
|
|
} else { |
47
|
1 |
|
$query->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$endpoint->addQuery($query); |
51
|
|
|
|
52
|
1 |
|
if ($annotation->node) { |
53
|
1 |
|
$query->setNode($annotation->node); |
54
|
1 |
|
$query->setType($annotation->node); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$query->setList($annotation->list); |
58
|
|
|
|
59
|
1 |
|
if (!$annotation->node) { |
60
|
1 |
|
$nodeType = ClassUtils::getNodeFromClass($refClass->getName()); |
61
|
1 |
|
$objectDefinition = null; |
62
|
1 |
|
if ($nodeType && $endpoint->hasType($nodeType)) { |
63
|
1 |
|
$objectDefinition = $endpoint->getType($nodeType); |
64
|
|
|
} |
65
|
1 |
|
if ($objectDefinition) { |
66
|
1 |
|
$query->setType($objectDefinition->getName()); |
67
|
1 |
|
$query->setNode($objectDefinition->getName()); |
68
|
|
|
} else { |
69
|
|
|
//avoid throw Exception when the schema is empty |
70
|
|
|
//when the schema is empty the NodeInterface has not been loaded |
71
|
|
|
//then the node(id) and nodes(ids) queries fails |
72
|
|
|
if ('Node' === $nodeType) { |
73
|
|
|
$endpoint->removeQuery($query->getName()); |
74
|
|
|
|
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName()); |
79
|
|
|
throw new \RuntimeException($error); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
if ($annotation->arguments) { |
|
|
|
|
84
|
|
|
foreach ($annotation->arguments as $argAnnotation) { |
85
|
|
|
if ($argAnnotation instanceof Annotation\Argument) { |
86
|
|
|
$this->resolveArgument($query, $argAnnotation); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
1 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
91
|
1 |
|
foreach ($argAnnotations as $argAnnotation) { |
92
|
1 |
|
if ($argAnnotation instanceof Annotation\Argument) { |
93
|
1 |
|
$this->resolveArgument($query, $argAnnotation); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$query->setResolver($annotation->resolver ?? $refClass->getName()); |
99
|
1 |
|
$query->setDeprecationReason($annotation->deprecationReason); |
100
|
1 |
|
$query->setDescription($annotation->description); |
101
|
|
|
|
102
|
1 |
|
foreach ($annotation->options as $option => $value) { |
103
|
1 |
|
$query->setMeta($option, $value); |
104
|
|
|
} |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param ArgumentAwareInterface $argumentAware |
109
|
|
|
* @param object $argAnnotation |
110
|
|
|
*/ |
111
|
1 |
|
public function resolveArgument(ArgumentAwareInterface $argumentAware, $argAnnotation) |
112
|
|
|
{ |
113
|
1 |
|
$arg = new ArgumentDefinition(); |
114
|
1 |
|
$arg->setName($argAnnotation->name); |
115
|
1 |
|
$arg->setDescription($argAnnotation->description); |
116
|
1 |
|
$arg->setInternalName($argAnnotation->internalName); |
117
|
1 |
|
$arg->setDefaultValue($argAnnotation->defaultValue); |
118
|
1 |
|
$arg->setType(TypeUtil::normalize($argAnnotation->type)); |
119
|
1 |
|
$arg->setList(TypeUtil::isTypeList($argAnnotation->type)); |
120
|
1 |
|
$arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type)); |
121
|
1 |
|
$arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type)); |
122
|
1 |
|
$argumentAware->addArgument($arg); |
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|
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.