1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Command; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Introspection; |
15
|
|
|
use GraphQL\Utils\SchemaPrinter; |
16
|
|
|
use Overblog\GraphQLBundle\Request\Executor; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
22
|
|
|
|
23
|
|
|
class GraphQLDumpSchemaCommand extends ContainerAwareCommand |
24
|
|
|
{ |
25
|
4 |
|
protected function configure() |
26
|
|
|
{ |
27
|
4 |
|
$this |
28
|
4 |
|
->setName('graphql:dump-schema') |
29
|
4 |
|
->setAliases(['graph:dump-schema']) |
30
|
4 |
|
->setDescription('Dumps GraphQL schema') |
31
|
4 |
|
->addOption( |
32
|
4 |
|
'file', |
33
|
4 |
|
null, |
34
|
4 |
|
InputOption::VALUE_OPTIONAL, |
35
|
|
|
'Path to generate schema file.' |
36
|
4 |
|
) |
37
|
4 |
|
->addOption( |
38
|
4 |
|
'schema', |
39
|
4 |
|
null, |
40
|
4 |
|
InputOption::VALUE_OPTIONAL, |
41
|
|
|
'The schema name to generate.' |
42
|
4 |
|
) |
43
|
4 |
|
->addOption( |
44
|
4 |
|
'format', |
45
|
4 |
|
null, |
46
|
4 |
|
InputOption::VALUE_OPTIONAL, |
47
|
4 |
|
'The schema name to generate ("graphqls" or "json"). ', |
48
|
|
|
'json' |
49
|
4 |
|
); |
50
|
4 |
|
} |
51
|
|
|
|
52
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
4 |
|
$output = new SymfonyStyle($input, $output); |
55
|
|
|
|
56
|
4 |
|
$format = strtolower($input->getOption('format')); |
57
|
4 |
|
$schemaName = $input->getOption('schema'); |
58
|
4 |
|
$container = $this->getContainer(); |
59
|
4 |
|
$requestExecutor = $container->get('overblog_graphql.request_executor'); |
60
|
4 |
|
$file = $input->getOption('file') ?: $container->getParameter('kernel.root_dir').sprintf('/../var/schema%s.%s', $schemaName ? '.'.$schemaName : '', $format); |
61
|
|
|
|
62
|
4 |
|
$content = $this->createFileContent($requestExecutor, $format, $schemaName); |
63
|
3 |
|
file_put_contents($file, $content); |
64
|
|
|
|
65
|
3 |
|
$output->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file))); |
66
|
3 |
|
} |
67
|
|
|
|
68
|
4 |
|
private function createFileContent(Executor $requestExecutor, $format, $schemaName) |
69
|
|
|
{ |
70
|
|
|
switch ($format) { |
71
|
4 |
|
case 'json': |
72
|
|
|
$request = [ |
73
|
2 |
|
'query' => Introspection::getIntrospectionQuery(false), |
74
|
2 |
|
'variables' => [], |
75
|
2 |
|
'operationName' => null, |
76
|
2 |
|
]; |
77
|
|
|
|
78
|
|
|
$result = $requestExecutor |
79
|
2 |
|
->execute($request, [], $schemaName) |
80
|
2 |
|
->toArray(); |
81
|
|
|
|
82
|
2 |
|
$content = json_encode($result['data']); |
83
|
2 |
|
break; |
84
|
|
|
|
85
|
2 |
|
case 'graphqls': |
86
|
1 |
|
$content = SchemaPrinter::doPrint($requestExecutor->getSchema($schemaName)); |
87
|
1 |
|
break; |
88
|
|
|
|
89
|
1 |
|
default: |
90
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unknown format %s.', json_encode($format))); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
3 |
|
return $content; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|