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\MutationDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
16
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parse mutation annotation to fetch definitions |
21
|
|
|
*/ |
22
|
|
|
class MutationAnnotationParser extends QueryAnnotationParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
41 |
|
public function supports($annotation): bool |
28
|
|
|
{ |
29
|
41 |
|
return $annotation instanceof Annotation\Mutation; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
* |
35
|
|
|
* @param Annotation\Mutation $annotation |
36
|
|
|
*/ |
37
|
11 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
38
|
|
|
{ |
39
|
|
|
/** @var Annotation\Mutation $annotation */ |
40
|
|
|
|
41
|
11 |
|
if (!preg_match('/\\Mutation\\\\/', $refClass->getName())) { |
42
|
|
|
$error = sprintf( |
43
|
|
|
'Annotation "@Mutation" in the class "%s" is not valid, |
44
|
|
|
mutations can only be applied to classes inside "...Bundle\Mutation\..."', |
45
|
|
|
$refClass->getName() |
46
|
|
|
); |
47
|
|
|
throw new \RuntimeException($error); |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
if (!$annotation->resolver && !$refClass->hasMethod('__invoke')) { |
51
|
|
|
$error = sprintf( |
52
|
|
|
'The class "%s" should have a method "__invoke" to process the mutation.', |
53
|
|
|
$refClass->getName() |
54
|
|
|
); |
55
|
|
|
throw new \RuntimeException($error); |
56
|
|
|
} |
57
|
|
|
|
58
|
11 |
|
$mutation = new MutationDefinition(); |
59
|
|
|
|
60
|
11 |
|
if ($annotation->name) { |
61
|
11 |
|
$mutation->setName($annotation->name); |
62
|
|
|
} else { |
63
|
|
|
$mutation->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); |
64
|
|
|
} |
65
|
|
|
|
66
|
11 |
|
$endpoint->addMutation($mutation); |
67
|
|
|
|
68
|
11 |
|
if (!$annotation->payload) { |
69
|
|
|
if (class_exists($refClass->getName().'Payload')) { |
70
|
|
|
$annotation->payload = $refClass->getName().'Payload'; |
71
|
|
|
if (!$endpoint->hasTypeForClass($annotation->payload)) { |
72
|
|
|
$error = sprintf( |
73
|
|
|
'The payload "%s" exist but does not exist a valid GraphQL type, is missing ObjectType annotation?', |
74
|
|
|
$annotation->payload |
75
|
|
|
); |
76
|
|
|
throw new \RuntimeException($error); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
$mutation->setType(TypeUtil::normalize($annotation->payload)); |
82
|
11 |
|
$mutation->setList(TypeUtil::isTypeList($annotation->payload)); |
83
|
11 |
|
$mutation->setNonNullList(TypeUtil::isTypeNonNullList($annotation->payload)); |
84
|
11 |
|
$mutation->setNonNull(TypeUtil::isTypeNonNull($annotation->payload)); |
85
|
|
|
|
86
|
11 |
|
if (!$mutation->getType()) { |
87
|
|
|
$error = sprintf( |
88
|
|
|
'The mutation "%s" does not have a valid payload, |
89
|
|
|
create a file called %sPayload or specify a payload.', |
90
|
|
|
$mutation->getName(), |
91
|
|
|
$refClass->getName() |
92
|
|
|
); |
93
|
|
|
throw new \RuntimeException($error); |
94
|
|
|
} |
95
|
|
|
|
96
|
11 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
97
|
11 |
|
foreach ($argAnnotations as $argAnnotation) { |
98
|
|
|
if ($argAnnotation instanceof Annotation\Argument) { |
99
|
|
|
$this->resolveArgument($mutation, $argAnnotation); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
if ($annotation->node) { |
104
|
11 |
|
$mutation->setNode($annotation->node); |
105
|
|
|
} elseif (($node = ClassUtils::getNodeFromClass($refClass->getName())) && $endpoint->hasType($node)) { |
106
|
|
|
$mutation->setNode($node); |
107
|
|
|
} |
108
|
|
|
|
109
|
11 |
|
$mutation->setResolver($annotation->resolver ?? $refClass->getName()); |
110
|
11 |
|
$mutation->setDeprecationReason($annotation->deprecationReason); |
111
|
11 |
|
$mutation->setDescription($annotation->description); |
112
|
|
|
|
113
|
|
|
//enable form auto-loaded by default |
114
|
11 |
|
$form = null; |
115
|
11 |
|
foreach ($annotation->options as $option) { |
116
|
11 |
|
if ($option instanceof Annotation\Plugin\Form) { |
117
|
|
|
$form = $option; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//form plugin is automatically enable for mutations |
122
|
11 |
|
if (!$form && !isset($annotation->options['form'])) { |
123
|
|
|
$annotation->options['form'] = ['enabled' => true]; |
124
|
|
|
} |
125
|
|
|
|
126
|
11 |
|
foreach ($annotation->options as $option => $value) { |
127
|
11 |
|
$mutation->setMeta($option, $value); |
128
|
|
|
} |
129
|
11 |
|
} |
130
|
|
|
} |
131
|
|
|
|