|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Resolver\MutationResolver; |
|
6
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverInterface; |
|
7
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverResolver; |
|
8
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
|
|
15
|
|
|
class DebugCommand extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
private static $categories = ['type', 'mutation', 'resolver']; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var TypeResolver |
|
21
|
|
|
*/ |
|
22
|
|
|
private $typeResolver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var MutationResolver |
|
26
|
|
|
*/ |
|
27
|
|
|
private $mutationResolver; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ResolverResolver |
|
31
|
|
|
*/ |
|
32
|
|
|
private $resolverResolver; |
|
33
|
|
|
|
|
34
|
1 |
|
public function __construct( |
|
35
|
|
|
TypeResolver $typeResolver, |
|
36
|
|
|
MutationResolver $mutationResolver, |
|
37
|
|
|
ResolverResolver $resolverResolver |
|
38
|
|
|
) { |
|
39
|
1 |
|
parent::__construct(); |
|
40
|
1 |
|
$this->typeResolver = $typeResolver; |
|
41
|
1 |
|
$this->mutationResolver = $mutationResolver; |
|
42
|
1 |
|
$this->resolverResolver = $resolverResolver; |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
protected function configure() |
|
46
|
|
|
{ |
|
47
|
|
|
$this |
|
48
|
1 |
|
->setName('graphql:debug') |
|
49
|
1 |
|
->setAliases(['debug:graphql']) |
|
50
|
1 |
|
->addOption( |
|
51
|
1 |
|
'category', |
|
52
|
1 |
|
null, |
|
53
|
1 |
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
|
54
|
1 |
|
sprintf('filter by a category (%s).', implode(', ', self::$categories)) |
|
55
|
|
|
) |
|
56
|
1 |
|
->setDescription('Display current GraphQL services (types, resolvers and mutations)'); |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
60
|
|
|
{ |
|
61
|
6 |
|
$categoriesOption = $input->getOption('category'); |
|
62
|
6 |
|
$categoriesOption = is_array($categoriesOption) ? $categoriesOption : [$categoriesOption]; |
|
63
|
6 |
|
$notAllowed = array_diff($categoriesOption, self::$categories); |
|
64
|
6 |
|
if (!empty($notAllowed)) { |
|
65
|
1 |
|
throw new \InvalidArgumentException(sprintf('Invalid category (%s)', implode(',', $notAllowed))); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
$categories = empty($categoriesOption) ? self::$categories : $categoriesOption; |
|
69
|
|
|
|
|
70
|
5 |
|
$io = new SymfonyStyle($input, $output); |
|
71
|
5 |
|
$tableHeaders = ['id', 'aliases']; |
|
72
|
5 |
|
foreach ($categories as $category) { |
|
73
|
5 |
|
$io->title(sprintf('GraphQL %ss Services', ucfirst($category))); |
|
74
|
|
|
|
|
75
|
|
|
/** @var ResolverInterface $resolver */ |
|
76
|
5 |
|
$resolver = $this->{$category.'Resolver'}; |
|
77
|
|
|
|
|
78
|
5 |
|
$solutions = $this->retrieveSolutions($resolver); |
|
79
|
5 |
|
$this->renderTable($tableHeaders, $solutions, $io); |
|
80
|
|
|
} |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
private function renderTable(array $tableHeaders, array $solutions, SymfonyStyle $io) |
|
84
|
|
|
{ |
|
85
|
5 |
|
$tableRows = []; |
|
86
|
5 |
|
foreach ($solutions as $id => &$options) { |
|
87
|
4 |
|
ksort($options['aliases']); |
|
88
|
4 |
|
$tableRows[] = [$id, implode("\n", $options['aliases'])]; |
|
89
|
|
|
} |
|
90
|
5 |
|
$io->table($tableHeaders, $tableRows); |
|
91
|
5 |
|
$io->write("\n\n"); |
|
92
|
5 |
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
private function retrieveSolutions(ResolverInterface $resolver) |
|
95
|
|
|
{ |
|
96
|
5 |
|
$data = []; |
|
97
|
5 |
|
foreach ($resolver->getSolutions() as $alias => $solution) { |
|
98
|
4 |
|
$options = $resolver->getSolutionOptions($alias); |
|
99
|
|
|
|
|
100
|
4 |
|
$id = $options['id']; |
|
101
|
4 |
|
if (!isset($data[$id]['aliases'])) { |
|
102
|
4 |
|
$data[$id]['aliases'] = []; |
|
103
|
|
|
} |
|
104
|
4 |
|
$data[$id]['aliases'][] = $options['alias'].(isset($options['method']) ? ' (method: '.$options['method'].')' : ''); |
|
105
|
|
|
} |
|
106
|
5 |
|
ksort($data); |
|
107
|
|
|
|
|
108
|
5 |
|
return $data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
6 |
|
public static function getCategories() |
|
112
|
|
|
{ |
|
113
|
6 |
|
return self::$categories; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|