1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\InputObjectFieldResolver\Middleware; |
6
|
|
|
|
7
|
|
|
use Andi\GraphQL\Attribute\InputObjectField; |
8
|
|
|
use Andi\GraphQL\Common\LazyParserType; |
9
|
|
|
use Andi\GraphQL\Common\LazyTypeByReflectionType; |
10
|
|
|
use Andi\GraphQL\Exception\CantResolveGraphQLTypeException; |
11
|
|
|
use Andi\GraphQL\InputObjectFieldResolver\InputObjectFieldResolverInterface; |
12
|
|
|
use Andi\GraphQL\TypeRegistryInterface; |
13
|
|
|
use GraphQL\Type\Definition as Webonyx; |
14
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; |
15
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Param; |
16
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
17
|
|
|
use ReflectionProperty; |
18
|
|
|
use Spiral\Attributes\ReaderInterface; |
19
|
|
|
|
20
|
|
|
final class ReflectionPropertyMiddleware implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
public const PRIORITY = 4096; |
23
|
|
|
|
24
|
9 |
|
public function __construct( |
25
|
|
|
private readonly ReaderInterface $reader, |
26
|
|
|
private readonly TypeRegistryInterface $typeRegistry, |
27
|
|
|
) { |
28
|
9 |
|
} |
29
|
|
|
|
30
|
8 |
|
public function process(mixed $field, InputObjectFieldResolverInterface $fieldResolver): Webonyx\InputObjectField |
31
|
|
|
{ |
32
|
8 |
|
if (! $field instanceof ReflectionProperty) { |
33
|
1 |
|
return $fieldResolver->resolve($field); |
34
|
|
|
} |
35
|
|
|
|
36
|
7 |
|
$attribute = $this->reader->firstPropertyMetadata($field, InputObjectField::class); |
37
|
|
|
|
38
|
7 |
|
$config = [ |
39
|
7 |
|
'name' => $this->getFieldName($field, $attribute), |
40
|
7 |
|
'description' => $this->getFieldDescription($field, $attribute), |
41
|
7 |
|
'type' => $this->getFieldType($field, $attribute), |
42
|
7 |
|
'deprecationReason' => $this->getFieldDeprecationReason($field, $attribute), |
43
|
7 |
|
]; |
44
|
|
|
|
45
|
6 |
|
if ($this->hasDefaultValue($field, $attribute)) { |
46
|
4 |
|
$config['defaultValue'] = $this->getDefaultValue($field, $attribute); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
return new Webonyx\InputObjectField($config); |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
private function getFieldName(ReflectionProperty $property, ?InputObjectField $attribute): string |
53
|
|
|
{ |
54
|
7 |
|
return $attribute?->name |
55
|
7 |
|
?? $property->getName(); |
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
private function getFieldDescription(ReflectionProperty $property, ?InputObjectField $attribute): ?string |
59
|
|
|
{ |
60
|
7 |
|
if ($attribute?->description) { |
61
|
2 |
|
return $attribute->description; |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
if ($property->isPromoted()) { |
65
|
1 |
|
if ($docComment = $property->getDeclaringClass()->getConstructor()->getDocComment()) { |
66
|
1 |
|
$docBlock = DocBlockFactory::createInstance(['psalm-param' => Param::class])->create($docComment); |
67
|
1 |
|
foreach ($docBlock->getTags() as $tag) { |
68
|
1 |
|
if ($tag instanceof Param && $tag->getVariableName() === $property->getName()) { |
69
|
1 |
|
return (string) $tag->getDescription() ?: null; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
4 |
|
} elseif ($docComment = $property->getDocComment()) { |
74
|
1 |
|
return DocBlockFactory::createInstance()->create($docComment)->getSummary() ?: null; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
private function getFieldType(ReflectionProperty $property, ?InputObjectField $attribute): callable |
81
|
|
|
{ |
82
|
7 |
|
if ($attribute?->type) { |
83
|
3 |
|
return new LazyParserType($attribute->type, $attribute->mode ?? 0, $this->typeRegistry); |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
if (! $property->hasType()) { |
87
|
1 |
|
throw new CantResolveGraphQLTypeException(\sprintf( |
88
|
1 |
|
'Can\'t resolve GraphQL type for field "%s"', |
89
|
1 |
|
$property->getName() |
90
|
1 |
|
)); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return new LazyTypeByReflectionType( |
94
|
3 |
|
$property->getType(), |
95
|
3 |
|
$this->typeRegistry, |
96
|
3 |
|
$property->getDeclaringClass()->getName(), |
97
|
3 |
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param ReflectionProperty $property |
102
|
|
|
* @param InputObjectField|null $attribute |
103
|
|
|
* |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
6 |
|
private function getFieldDeprecationReason(ReflectionProperty $property, ?InputObjectField $attribute): ?string |
107
|
|
|
{ |
108
|
6 |
|
if ($attribute?->deprecationReason) { |
109
|
1 |
|
return $attribute->deprecationReason; |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
if ($docComment = $property->getDocComment()) { |
113
|
2 |
|
$docBlock = DocBlockFactory::createInstance(['property-deprecated' => Deprecated::class]) |
114
|
2 |
|
->create($docComment); |
115
|
2 |
|
foreach ($docBlock->getTags() as $tag) { |
116
|
2 |
|
if ($tag instanceof Deprecated) { |
117
|
2 |
|
return (string) $tag->getDescription() ?: null; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
private function hasDefaultValue(ReflectionProperty $property, ?InputObjectField $attribute): bool |
126
|
|
|
{ |
127
|
6 |
|
return $attribute?->hasDefaultValue() || $property->hasDefaultValue(); |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
private function getDefaultValue(ReflectionProperty $property, ?InputObjectField $attribute): mixed |
131
|
|
|
{ |
132
|
4 |
|
if ($attribute?->hasDefaultValue()) { |
133
|
2 |
|
return $attribute->defaultValue; |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
return $property->getDefaultValue(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|