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\Type; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
18
|
|
|
use Ynlo\GraphQLBundle\Resolver\ResolverExecutor; |
19
|
|
|
use Ynlo\GraphQLBundle\Util\GraphQLBuilder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class QueryType |
23
|
|
|
*/ |
24
|
|
|
class QueryType extends ObjectType implements |
25
|
|
|
ContainerAwareInterface, |
26
|
|
|
EndpointAwareInterface |
27
|
|
|
{ |
28
|
|
|
use ContainerAwareTrait; |
29
|
|
|
use EndpointAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* QueryType constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $config |
35
|
|
|
*/ |
36
|
1 |
View Code Duplication |
public function __construct(array $config = []) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$defaults = [ |
39
|
1 |
|
'name' => 'Query', |
40
|
1 |
|
'fields' => function () { |
41
|
1 |
|
$queries = []; |
42
|
1 |
|
foreach ($this->endpoint->allQueries() as $query) { |
43
|
1 |
|
$queries[$query->getName()] = $this->getQueryConfig($query); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $queries; |
47
|
1 |
|
}, |
48
|
|
|
]; |
49
|
1 |
|
parent::__construct(array_merge($defaults, $config)); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param QueryDefinition $query |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
1 |
|
protected function getQueryConfig(QueryDefinition $query): array |
58
|
|
|
{ |
59
|
1 |
|
$config['type'] = Types::get($query->getType()); |
|
|
|
|
60
|
1 |
|
if ($query->isList()) { |
61
|
1 |
|
$config['type'] = Type::listOf($config['type']); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$config['args'] = GraphQLBuilder::buildArguments($query); |
65
|
|
|
|
66
|
1 |
|
$config['resolve'] = new ResolverExecutor($this->container, $this->endpoint, $query); |
67
|
1 |
|
$config['description'] = $query->getDescription(); |
68
|
1 |
|
$config['deprecationReason'] = $query->getDeprecationReason(); |
69
|
|
|
|
70
|
1 |
|
return $config; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.