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