|
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\Type\Definition; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
14
|
|
|
use GraphQL\Type\Definition\InterfaceType; |
|
15
|
|
|
use GraphQL\Type\Definition\Type; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class InterfaceDefinitionType |
|
23
|
|
|
*/ |
|
24
|
|
|
class InterfaceDefinitionType extends InterfaceType implements EndpointAwareInterface, ContainerAwareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ContainerAwareTrait; |
|
27
|
|
|
use EndpointAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var InterfaceDefinition |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $definition; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* InterfaceDefinitionType constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param InterfaceDefinition $definition |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function __construct(InterfaceDefinition $definition) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->definition = $definition; |
|
42
|
|
|
|
|
43
|
1 |
|
parent::__construct( |
|
44
|
|
|
[ |
|
45
|
1 |
|
'name' => $definition->getName(), |
|
46
|
1 |
|
'description' => $definition->getDescription(), |
|
47
|
1 |
|
'fields' => function () { |
|
48
|
1 |
|
return $this->resolveFields(); |
|
49
|
1 |
|
}, |
|
50
|
6 |
|
'resolveType' => function ($value) { |
|
51
|
6 |
|
foreach ($this->definition->getImplementors() as $implementor) { |
|
52
|
6 |
|
$implementorDef = $this->endpoint->getType($implementor); |
|
53
|
|
|
//ClassUtils::getClass is required to avoid proxies |
|
54
|
6 |
|
if ($implementorDef->getClass() === ClassUtils::getClass($value)) { |
|
|
|
|
|
|
55
|
6 |
|
return TypeRegistry::get($implementorDef->getName()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
1 |
|
}, |
|
61
|
|
|
] |
|
62
|
|
|
); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
1 |
View Code Duplication |
private function resolveFields() |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
1 |
|
$fields = []; |
|
71
|
1 |
|
foreach ($this->definition->getFields() as $fieldDefinition) { |
|
72
|
1 |
|
$type = TypeRegistry::get($fieldDefinition->getType()); |
|
73
|
|
|
|
|
74
|
1 |
|
if ($fieldDefinition->isList()) { |
|
75
|
1 |
|
if ($fieldDefinition->isNonNullList()) { |
|
76
|
|
|
$type = Type::nonNull($type); |
|
77
|
|
|
} |
|
78
|
1 |
|
$type = Type::listOf($type); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
if ($fieldDefinition->isNonNull()) { |
|
82
|
1 |
|
$type = Type::nonNull($type); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$fields[$fieldDefinition->getName()] = [ |
|
86
|
1 |
|
'type' => $type, |
|
87
|
1 |
|
'description' => $fieldDefinition->getDescription(), |
|
88
|
1 |
|
'deprecationReason' => $fieldDefinition->getDeprecationReason(), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return $fields; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|