|
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\DefinitionInterface; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Model\UpdateNodePayload; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Mutation\UpdateNode; |
|
20
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
|
21
|
|
|
|
|
22
|
|
|
class MutationUpdateAnnotationParser extends MutationAnnotationParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
21 |
|
public function supports($annotation): bool |
|
28
|
|
|
{ |
|
29
|
21 |
|
return $annotation instanceof Annotation\MutationUpdate; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
21 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
|
36
|
|
|
{ |
|
37
|
21 |
|
if (!$endpoint->hasTypeForClass($refClass->getName())) { |
|
38
|
|
|
throw new \RuntimeException(sprintf('Can\'t apply Update operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName())); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
21 |
|
if (!$refClass->implementsInterface(NodeInterface::class)) { |
|
42
|
|
|
throw new \RuntimeException(sprintf('Can\'t apply Update operation to "%s", CRUD operations can only be applied to nodes. You are implementing NodeInterface in this class?', $refClass->getName())); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
21 |
|
$definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName())); |
|
46
|
21 |
|
$bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName()); |
|
47
|
|
|
|
|
48
|
|
|
/** @var Annotation\MutationUpdate $annotation */ |
|
49
|
21 |
|
$annotation->name = $annotation->name ?? 'update'.ucfirst($definition->getName()); |
|
50
|
21 |
|
$annotation->payload = $annotation->payload ?? null; |
|
51
|
21 |
|
if (!$annotation->payload) { |
|
52
|
|
|
//deep cloning |
|
53
|
|
|
/** @var ObjectDefinitionInterface $payload */ |
|
54
|
21 |
|
$payload = unserialize(serialize($endpoint->getType(UpdateNodePayload::class)), [DefinitionInterface::class]); |
|
55
|
21 |
|
$payload->setName(ucfirst($annotation->name.'Payload')); |
|
56
|
|
|
|
|
57
|
21 |
|
if (!$endpoint->hasType($payload->getName())) { |
|
58
|
21 |
|
$payload->getField('node')->setType($definition->getName()); |
|
59
|
21 |
|
$endpoint->add($payload); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
21 |
|
$annotation->payload = $payload->getName(); |
|
63
|
|
|
} |
|
64
|
21 |
|
$annotation->node = $annotation->node ?? $definition->getName(); |
|
65
|
|
|
|
|
66
|
21 |
|
if ($endpoint->hasTypeForClass($annotation->node)) { |
|
67
|
|
|
$annotation->node = $endpoint->getTypeForClass($annotation->node); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
21 |
|
$formType = true; |
|
71
|
21 |
|
$options = []; |
|
72
|
21 |
|
$generalForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->node, 'Input'); |
|
73
|
21 |
|
$specificForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->name, 'Input'); |
|
74
|
21 |
|
if (class_exists($specificForm)) { |
|
75
|
21 |
|
$formType = $specificForm; |
|
76
|
|
|
} elseif (class_exists($generalForm)) { |
|
77
|
|
|
$formType = $generalForm; |
|
78
|
|
|
$options['operation'] = $annotation->name; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
21 |
|
$annotation->options = array_merge(['form' => ['type' => $formType, 'options' => $options]], $annotation->options); |
|
82
|
21 |
|
$resolverReflection = new \ReflectionClass(UpdateNode::class); |
|
83
|
|
|
|
|
84
|
21 |
|
$resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name); |
|
85
|
21 |
|
if (class_exists($resolver)) { |
|
86
|
|
|
$annotation->resolver = $resolver; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
21 |
|
parent::parse($annotation, $resolverReflection, $endpoint); |
|
90
|
21 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|