1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Command; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Introspection; |
6
|
|
|
use GraphQL\Utils\SchemaPrinter; |
7
|
|
|
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
final class GraphQLDumpSchemaCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** @var ContainerInterface */ |
18
|
|
|
private $container; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $baseExportPath; |
22
|
|
|
|
23
|
7 |
|
public function __construct( |
24
|
|
|
ContainerInterface $container, |
25
|
|
|
$baseExportPath |
26
|
|
|
) { |
27
|
7 |
|
parent::__construct(); |
28
|
7 |
|
$this->container = $container; |
29
|
7 |
|
$this->baseExportPath = $baseExportPath; |
30
|
7 |
|
} |
31
|
|
|
|
32
|
7 |
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
7 |
|
->setName('graphql:dump-schema') |
36
|
7 |
|
->setAliases(['graph:dump-schema']) |
37
|
7 |
|
->setDescription('Dumps GraphQL schema') |
38
|
7 |
|
->addOption( |
39
|
7 |
|
'file', |
40
|
7 |
|
null, |
41
|
7 |
|
InputOption::VALUE_OPTIONAL, |
42
|
7 |
|
'Path to generate schema file.' |
43
|
|
|
) |
44
|
7 |
|
->addOption( |
45
|
7 |
|
'schema', |
46
|
7 |
|
null, |
47
|
7 |
|
InputOption::VALUE_OPTIONAL, |
48
|
7 |
|
'The schema name to generate.' |
49
|
|
|
) |
50
|
7 |
|
->addOption( |
51
|
7 |
|
'format', |
52
|
7 |
|
null, |
53
|
7 |
|
InputOption::VALUE_OPTIONAL, |
54
|
7 |
|
'The schema format to generate ("graphql" or "json").', |
55
|
7 |
|
'json' |
56
|
|
|
) |
57
|
7 |
|
->addOption( |
58
|
7 |
|
'modern', |
59
|
7 |
|
null, |
60
|
7 |
|
InputOption::VALUE_NONE, |
61
|
7 |
|
'Enabled modern json format: { "data": { "__schema": {...} } }.' |
62
|
|
|
) |
63
|
7 |
|
->addOption( |
64
|
7 |
|
'classic', |
65
|
7 |
|
null, |
66
|
7 |
|
InputOption::VALUE_NONE, |
67
|
7 |
|
'Enabled classic json format: { "__schema": {...} }.' |
68
|
|
|
) |
69
|
|
|
; |
70
|
7 |
|
} |
71
|
|
|
|
72
|
7 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
7 |
|
$io = new SymfonyStyle($input, $output); |
75
|
7 |
|
$file = $this->createFile($input); |
76
|
5 |
|
$io->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file))); |
77
|
5 |
|
} |
78
|
|
|
|
79
|
7 |
|
private function createFile(InputInterface $input) |
80
|
|
|
{ |
81
|
7 |
|
$format = strtolower($input->getOption('format')); |
82
|
7 |
|
$schemaName = $input->getOption('schema'); |
83
|
|
|
|
84
|
7 |
|
$file = $input->getOption('file') ?: $this->baseExportPath.sprintf('/../var/schema%s.%s', $schemaName ? '.'.$schemaName : '', $format); |
85
|
|
|
|
86
|
|
|
switch ($format) { |
87
|
7 |
|
case 'json': |
88
|
|
|
$request = [ |
89
|
5 |
|
'query' => Introspection::getIntrospectionQuery(false), |
90
|
|
|
'variables' => [], |
91
|
|
|
'operationName' => null, |
92
|
|
|
]; |
93
|
|
|
|
94
|
5 |
|
$modern = $this->useModernJsonFormat($input); |
95
|
|
|
|
96
|
4 |
|
$result = $this->getRequestExecutor() |
97
|
4 |
|
->execute($request, [], $schemaName) |
98
|
4 |
|
->toArray(); |
99
|
|
|
|
100
|
4 |
|
$content = json_encode($modern ? $result : $result['data'], \JSON_PRETTY_PRINT); |
101
|
4 |
|
break; |
102
|
|
|
|
103
|
2 |
|
case 'graphql': |
104
|
1 |
|
$content = SchemaPrinter::doPrint($this->getRequestExecutor()->getSchema($schemaName)); |
105
|
1 |
|
break; |
106
|
|
|
|
107
|
|
|
default: |
108
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unknown format %s.', json_encode($format))); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
file_put_contents($file, $content); |
112
|
|
|
|
113
|
5 |
|
return $file; |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
private function useModernJsonFormat(InputInterface $input) |
117
|
|
|
{ |
118
|
5 |
|
$modern = $input->getOption('modern'); |
119
|
5 |
|
$classic = $input->getOption('classic'); |
120
|
5 |
|
if ($modern && $classic) { |
121
|
1 |
|
throw new \InvalidArgumentException('"modern" and "classic" options should not be used together.'); |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
return $modern === true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return RequestExecutor |
129
|
|
|
*/ |
130
|
5 |
|
private function getRequestExecutor() |
131
|
|
|
{ |
132
|
5 |
|
return $this->container->get('overblog_graphql.request_executor'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|