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