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; |
12
|
|
|
|
13
|
|
|
use GraphQL\Type\Definition\ObjectType; |
14
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
19
|
|
|
use Ynlo\GraphQLBundle\Resolver\ObjectFieldResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AbstractObjectType |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractObjectType extends ObjectType implements |
25
|
|
|
ContainerAwareInterface, |
26
|
|
|
DefinitionManagerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use ContainerAwareTrait; |
29
|
|
|
use DefinitionManagerAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ObjectDefinition |
33
|
|
|
*/ |
34
|
|
|
protected $definition; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ObjectDefinition $definition |
38
|
|
|
*/ |
39
|
14 |
|
public function __construct(ObjectDefinition $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
|
1 |
|
'interfaces' => function () { |
51
|
1 |
|
return $this->resolveInterfaces(); |
52
|
1 |
|
}, |
53
|
14 |
View Code Duplication |
'resolveField' => function ($root, array $args, $context, ResolveInfo $resolveInfo) { |
|
|
|
|
54
|
14 |
|
$resolver = new ObjectFieldResolver($this->container, $this->manager, $this->definition); |
55
|
|
|
|
56
|
14 |
|
return $resolver($root, $args, $context, $resolveInfo); |
57
|
1 |
|
}, |
58
|
14 |
|
'isTypeOf' => function ($value, $context, ResolveInfo $info) { |
|
|
|
|
59
|
|
|
//TODO: implement this |
60
|
14 |
|
}, |
61
|
|
|
] |
62
|
|
|
); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
1 |
|
private function resolveFields() |
69
|
|
|
{ |
70
|
1 |
|
$fields = []; |
71
|
1 |
|
foreach ($this->definition->getFields() as $fieldDefinition) { |
72
|
1 |
|
$type = Types::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 |
|
$args = []; |
86
|
1 |
View Code Duplication |
foreach ($fieldDefinition->getArguments() as $argument) { |
|
|
|
|
87
|
1 |
|
$argumentType = Types::get($argument->getType()); |
88
|
|
|
|
89
|
1 |
|
if ($argument->isList()) { |
90
|
1 |
|
if ($argument->isNonNullList()) { |
91
|
|
|
$argumentType = Type::nonNull($type); |
92
|
|
|
} |
93
|
1 |
|
$argumentType = Type::listOf($argumentType); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
if ($argument->isNonNull()) { |
97
|
|
|
$argumentType = Type::nonNull($argumentType); |
98
|
|
|
} |
99
|
1 |
|
$arg['name'] = $argument->getName(); |
100
|
1 |
|
$arg['type'] = $argumentType; |
101
|
1 |
|
$arg['description'] = $argument->getDescription(); |
102
|
|
|
|
103
|
1 |
|
if ($argument->getDefaultValue() !== null) { |
104
|
|
|
$arg['defaultValue'] = $argument->getDefaultValue(); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$args[] = $arg; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$fields[$fieldDefinition->getName()] = [ |
111
|
1 |
|
'type' => $type, |
112
|
1 |
|
'description' => $fieldDefinition->getDescription(), |
113
|
1 |
|
'deprecationReason' => $fieldDefinition->getDeprecationReason(), |
114
|
1 |
|
'args' => $args, |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
return $fields; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
1 |
|
private function resolveInterfaces() |
125
|
|
|
{ |
126
|
1 |
|
$interfaces = []; |
127
|
1 |
|
foreach ($this->definition->getInterfaces() as $interface) { |
128
|
1 |
|
$interfaces[] = Types::get($interface); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $interfaces; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.