|
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\FieldDecorator; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Types\Type as DoctrineType; |
|
14
|
|
|
use Doctrine\ORM\Mapping\Column; |
|
15
|
|
|
use Doctrine\ORM\Mapping\Embedded; |
|
16
|
|
|
use Doctrine\ORM\Mapping\Id; |
|
17
|
|
|
use Doctrine\ORM\Mapping\ManyToMany; |
|
18
|
|
|
use Doctrine\ORM\Mapping\ManyToOne; |
|
19
|
|
|
use Doctrine\ORM\Mapping\OneToMany; |
|
20
|
|
|
use Doctrine\ORM\Mapping\OneToOne; |
|
21
|
|
|
use GraphQL\Type\Definition\Type; |
|
22
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldDefinition; |
|
23
|
|
|
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationReaderAwareTrait; |
|
24
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Decorate a field definition using doctrine annotations |
|
28
|
|
|
*/ |
|
29
|
|
|
class DoctrineFieldDefinitionDecorator implements FieldDefinitionDecoratorInterface |
|
30
|
|
|
{ |
|
31
|
|
|
use AnnotationReaderAwareTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition) |
|
37
|
|
|
{ |
|
38
|
1 |
|
if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) { |
|
39
|
|
|
throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
if ($field instanceof \ReflectionMethod) { |
|
43
|
1 |
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if (!$definition->getType()) { |
|
47
|
1 |
|
$parentField = null; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Column $column */ |
|
50
|
1 |
|
if ($column = $this->reader->getPropertyAnnotation($field, Column::class)) { |
|
51
|
1 |
|
$definition->setType($this->getNormalizedType($column->type)); |
|
52
|
1 |
|
$definition->setNonNull(!$column->nullable); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @var Id $id */ |
|
56
|
1 |
|
if ($column = $this->reader->getPropertyAnnotation($field, Id::class)) { |
|
|
|
|
|
|
57
|
1 |
|
$definition->setType(Type::ID); |
|
58
|
1 |
|
$definition->setNonNull(true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var OneToOne $oneToOne */ |
|
62
|
1 |
|
if ($oneToOne = $this->reader->getPropertyAnnotation($field, OneToOne::class)) { |
|
63
|
1 |
|
$definition->setType($oneToOne->targetEntity); |
|
64
|
1 |
|
$definition->setNonNull(true); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var OneToMany $oneToMany */ |
|
68
|
1 |
|
if ($oneToMany = $this->reader->getPropertyAnnotation($field, OneToMany::class)) { |
|
69
|
1 |
|
$definition->setType($oneToMany->targetEntity); |
|
70
|
1 |
|
$definition->setList(true); |
|
71
|
1 |
|
$parentField = $oneToMany->mappedBy; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @var ManyToOne $manyToOne */ |
|
75
|
1 |
|
if ($manyToOne = $this->reader->getPropertyAnnotation($field, ManyToOne::class)) { |
|
76
|
1 |
|
$definition->setType($manyToOne->targetEntity); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @var ManyToMany $manyToMany */ |
|
80
|
1 |
|
if ($manyToMany = $this->reader->getPropertyAnnotation($field, ManyToMany::class)) { |
|
81
|
|
|
$definition->setType($manyToMany->targetEntity); |
|
82
|
|
|
$definition->setList(true); |
|
83
|
|
|
$parentField = $oneToMany->mappedBy; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @var Embedded $embedded */ |
|
87
|
1 |
|
if ($embedded = $this->reader->getPropertyAnnotation($field, Embedded::class)) { |
|
88
|
1 |
|
$definition->setType($embedded->class); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
if ($definition->isList() && $parentField) { |
|
92
|
1 |
|
$definition->setMeta('connection_parent_field', $parentField); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $type |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
1 |
|
protected function getNormalizedType(?string $type):?string |
|
103
|
|
|
{ |
|
104
|
|
|
switch ($type) { |
|
105
|
1 |
|
case DoctrineType::BOOLEAN: |
|
106
|
1 |
|
$type = Type::BOOLEAN; |
|
107
|
1 |
|
break; |
|
108
|
1 |
|
case DoctrineType::DECIMAL: |
|
109
|
1 |
|
case DoctrineType::FLOAT: |
|
110
|
|
|
$type = Type::FLOAT; |
|
111
|
|
|
break; |
|
112
|
1 |
|
case DoctrineType::INTEGER: |
|
113
|
1 |
|
case DoctrineType::BIGINT: |
|
114
|
1 |
|
case DoctrineType::SMALLINT: |
|
115
|
1 |
|
$type = Type::INT; |
|
116
|
1 |
|
break; |
|
117
|
1 |
|
case DoctrineType::STRING: |
|
118
|
1 |
|
case DoctrineType::TEXT: |
|
119
|
1 |
|
$type = Type::STRING; |
|
120
|
1 |
|
break; |
|
121
|
1 |
|
case DoctrineType::DATE: |
|
122
|
1 |
|
case DoctrineType::DATETIME: |
|
123
|
1 |
|
$type = 'DateTime'; |
|
124
|
1 |
|
break; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return $type; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|