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