|
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\ObjectDefinitionInterface; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Model\AddNodePayload; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Model\UpdateNodePayload; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Mutation\AddNode; |
|
20
|
|
|
use Ynlo\GraphQLBundle\Mutation\UpdateNode; |
|
21
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
|
22
|
|
|
|
|
23
|
|
|
class MutationAddUpdateAnnotationParser extends MutationAnnotationParser |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
42 |
|
public function supports($annotation): bool |
|
29
|
|
|
{ |
|
30
|
42 |
|
return ($annotation instanceof Annotation\MutationAdd || $annotation instanceof Annotation\MutationUpdate); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
* |
|
36
|
|
|
* @param Annotation\MutationAdd $annotation |
|
37
|
|
|
*/ |
|
38
|
11 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
|
39
|
|
|
{ |
|
40
|
11 |
|
if (!$endpoint->hasTypeForClass($refClass->getName())) { |
|
41
|
1 |
|
throw new \RuntimeException(sprintf('Can\'t apply Add/Update operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName())); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
10 |
|
if (!$refClass->implementsInterface(NodeInterface::class)) { |
|
45
|
1 |
|
throw new \RuntimeException(sprintf('Can\'t apply Add/Update operation to "%s", CRUD operations can only be applied to nodes. You are implementing "%s" in this class?', $refClass->getName(), NodeInterface::class)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
9 |
|
$definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName())); |
|
49
|
9 |
|
$bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName()); |
|
50
|
|
|
|
|
51
|
9 |
|
$actionPrefix = $annotation instanceof Annotation\MutationAdd ? 'add' : 'update'; |
|
|
|
|
|
|
52
|
9 |
|
$annotation->name = $annotation->name ?? $actionPrefix.ucfirst($definition->getName()); |
|
53
|
9 |
|
$annotation->payload = $annotation->payload ?? null; |
|
54
|
9 |
|
if (!$annotation->payload) { |
|
55
|
|
|
//deep cloning |
|
56
|
|
|
/** @var ObjectDefinitionInterface $payload */ |
|
57
|
9 |
|
$payloadClass = $annotation instanceof Annotation\MutationAdd ? AddNodePayload::class : UpdateNodePayload::class; |
|
|
|
|
|
|
58
|
9 |
|
$payload = unserialize(serialize($endpoint->getType($payloadClass)), ['allowed_classes' => true]); |
|
59
|
9 |
|
$payload->setName(ucfirst($annotation->name.'Payload')); |
|
60
|
|
|
|
|
61
|
9 |
|
if (!$endpoint->hasType($payload->getName())) { |
|
62
|
9 |
|
$payload->getField('node')->setType($definition->getName()); |
|
63
|
9 |
|
$endpoint->add($payload); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
9 |
|
$annotation->payload = $payload->getName(); |
|
67
|
|
|
} |
|
68
|
9 |
|
$annotation->node = $annotation->node ?? $definition->getName(); |
|
69
|
|
|
|
|
70
|
9 |
|
if ($endpoint->hasTypeForClass($annotation->node)) { |
|
71
|
|
|
$annotation->node = $endpoint->getTypeForClass($annotation->node); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
$formType = null; |
|
75
|
9 |
|
$options = []; |
|
76
|
9 |
|
$generalForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->node, 'Input'); |
|
77
|
9 |
|
$specificForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->name, 'Input'); |
|
78
|
9 |
|
if (class_exists($specificForm)) { |
|
79
|
7 |
|
$formType = $specificForm; |
|
80
|
9 |
|
} elseif (class_exists($generalForm)) { |
|
81
|
9 |
|
$formType = $generalForm; |
|
82
|
9 |
|
$options['operation'] = $annotation->name; |
|
83
|
|
|
|
|
84
|
|
|
//in case the form has activated namespace operation alias |
|
85
|
|
|
//the operation name will be the alias |
|
86
|
9 |
|
foreach ($annotation->options as $name => $option) { |
|
87
|
|
|
if ($option instanceof Annotation\Plugin\Namespaces && $option->alias) { |
|
88
|
|
|
$options['operation'] = $option->alias; |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
//support for BC using array |
|
93
|
|
|
if (\is_array($option) && 'namespace' === $name && $option['alias'] ?? false) { |
|
94
|
|
|
$options['operation'] = $option->alias; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
|
$annotation->options = array_merge(['form' => ['type' => $formType, 'options' => $options]], $annotation->options); |
|
100
|
9 |
|
$resolver = $annotation instanceof Annotation\MutationAdd ? AddNode::class : UpdateNode::class; |
|
|
|
|
|
|
101
|
9 |
|
$resolverReflection = new \ReflectionClass($resolver); |
|
102
|
|
|
|
|
103
|
9 |
|
$resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name); |
|
104
|
9 |
|
if (class_exists($resolver)) { |
|
105
|
8 |
|
$annotation->resolver = $resolver; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
9 |
|
parent::parse($annotation, $resolverReflection, $endpoint); |
|
|
|
|
|
|
109
|
9 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|