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