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\FieldDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
19
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
20
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Resolve field of types queries using naming conventions |
24
|
|
|
*/ |
25
|
|
|
class StandaloneFieldParser extends QueryAnnotationParser |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
39 |
|
public function supports($annotation): bool |
31
|
|
|
{ |
32
|
39 |
|
return $annotation instanceof Annotation\Field; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
* |
38
|
|
|
* @param Annotation\Field $annotation |
39
|
|
|
*/ |
40
|
1 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
41
|
|
|
{ |
42
|
1 |
|
$field = new FieldDefinition(); |
43
|
|
|
|
44
|
1 |
|
if ($annotation->name) { |
45
|
|
|
$field->setName($annotation->name); |
46
|
|
|
} else { |
47
|
1 |
|
$field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$objectType = null; |
|
|
|
|
51
|
1 |
|
preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches); |
52
|
1 |
|
if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) { |
53
|
|
|
$error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName()); |
54
|
|
|
throw new \RuntimeException($error); |
55
|
|
|
} |
56
|
|
|
/** @var ObjectDefinitionInterface $objectDefinition */ |
57
|
1 |
|
$objectDefinition = $endpoint->getType($matches[1]); |
58
|
|
|
|
59
|
1 |
|
if ($annotation->in || $annotation->notIn) { |
|
|
|
|
60
|
|
|
if ($annotation->in && \in_array($objectDefinition->getName(), $annotation->in, true)) { |
|
|
|
|
61
|
|
|
$objectDefinition->addField($field); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($annotation->notIn && !\in_array($objectDefinition->getName(), $annotation->notIn, true)) { |
|
|
|
|
65
|
|
|
$objectDefinition->addField($field); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
1 |
|
$objectDefinition->addField($field); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
72
|
1 |
|
foreach ($argAnnotations as $argAnnotation) { |
73
|
1 |
|
if ($argAnnotation instanceof Annotation\Argument) { |
74
|
1 |
|
$this->resolveArgument($field, $argAnnotation); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$field->setType(TypeUtil::normalize($annotation->type)); |
79
|
1 |
|
$field->setList(TypeUtil::isTypeList($annotation->type)); |
80
|
1 |
|
$field->setNonNull(TypeUtil::isTypeNonNull($annotation->type)); |
81
|
1 |
|
$field->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type)); |
82
|
1 |
|
$field->setResolver($annotation->resolver ?? $refClass->getName()); |
83
|
1 |
|
$field->setDeprecationReason($annotation->deprecationReason); |
84
|
1 |
|
$field->setDescription($annotation->description); |
85
|
1 |
|
$field->setComplexity($annotation->complexity); |
86
|
1 |
|
$field->setMaxConcurrentUsage($annotation->maxConcurrentUsage); |
87
|
|
|
|
88
|
1 |
|
foreach ($annotation->options as $option => $value) { |
89
|
1 |
|
$field->setMeta($option, $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
if ($objectDefinition instanceof InterfaceDefinition) { |
93
|
1 |
|
$implementors = $objectDefinition->getImplementors(); |
94
|
1 |
|
foreach ($implementors as $implementor) { |
95
|
1 |
|
$childType = $endpoint->getType($implementor); |
96
|
1 |
|
if ($childType instanceof FieldsAwareDefinitionInterface) { |
97
|
1 |
|
if ($annotation->in || $annotation->notIn) { |
|
|
|
|
98
|
|
|
if ($annotation->in && \in_array($childType->getName(), $annotation->in, true)) { |
|
|
|
|
99
|
|
|
$childType->addField($field); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($annotation->notIn && !\in_array($childType->getName(), $annotation->notIn, true)) { |
|
|
|
|
103
|
|
|
$childType->addField($field); |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
1 |
|
$childType->addField($field); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
} |
113
|
|
|
|