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\Registry\Endpoint; |
16
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolve field of types queries using naming conventions |
21
|
|
|
*/ |
22
|
|
|
class StandaloneFieldParser extends QueryAnnotationParser |
23
|
|
|
{ |
24
|
|
|
use AnnotationReaderAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
22 |
|
public function supports($annotation): bool |
30
|
|
|
{ |
31
|
22 |
|
return $annotation instanceof Annotation\Field; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
22 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
38
|
|
|
{ |
39
|
|
|
/** @var Annotation\Field $annotation */ |
40
|
|
|
|
41
|
22 |
|
$field = new FieldDefinition(); |
42
|
|
|
|
43
|
22 |
|
if ($annotation->name) { |
44
|
|
|
$field->setName($annotation->name); |
45
|
|
|
} else { |
46
|
22 |
|
$field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); |
47
|
|
|
} |
48
|
|
|
|
49
|
22 |
|
$objectType = null; |
|
|
|
|
50
|
22 |
|
preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches); |
51
|
22 |
|
if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) { |
52
|
|
|
$error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName()); |
53
|
|
|
throw new \RuntimeException($error); |
54
|
|
|
} |
55
|
22 |
|
$objectDefinition = $endpoint->getType($matches[1]); |
56
|
22 |
|
$objectDefinition->addField($field); |
|
|
|
|
57
|
|
|
|
58
|
22 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
59
|
22 |
|
foreach ($argAnnotations as $argAnnotation) { |
60
|
22 |
|
if ($argAnnotation instanceof Annotation\Argument) { |
61
|
22 |
|
$this->resolveArgument($field, $argAnnotation); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
22 |
|
$field->setType(TypeUtil::normalize($annotation->type)); |
66
|
22 |
|
$field->setList(TypeUtil::isTypeList($annotation->type)); |
67
|
22 |
|
$field->setResolver($annotation->resolver ?? $refClass->getName()); |
|
|
|
|
68
|
22 |
|
$field->setDeprecationReason($annotation->deprecationReason); |
69
|
22 |
|
$field->setDescription($annotation->description); |
70
|
22 |
|
$field->setComplexity($annotation->complexity); |
71
|
22 |
|
$field->setMaxConcurrentUsage($annotation->maxConcurrentUsage); |
72
|
|
|
|
73
|
22 |
|
foreach ($annotation->options as $option => $value) { |
74
|
22 |
|
$field->setMeta($option, $value); |
75
|
|
|
} |
76
|
22 |
|
} |
77
|
|
|
} |
78
|
|
|
|