|
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
20
|
|
|
|
|
21
|
|
|
class GraphQLDumpSchemaCommand extends ContainerAwareCommand |
|
22
|
|
|
{ |
|
23
|
1 |
|
protected function configure() |
|
24
|
|
|
{ |
|
25
|
1 |
|
$this |
|
26
|
1 |
|
->setName('graphql:dump-schema') |
|
27
|
1 |
|
->setAliases(['graph:dump-schema']) |
|
28
|
1 |
|
->setDescription('Dumps GraphQL schema') |
|
29
|
1 |
|
->addOption( |
|
30
|
1 |
|
'file', |
|
31
|
1 |
|
null, |
|
32
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
33
|
|
|
'Path to generate schema file.' |
|
34
|
1 |
|
) |
|
35
|
1 |
|
->addOption( |
|
36
|
1 |
|
'schema', |
|
37
|
1 |
|
null, |
|
38
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
39
|
|
|
'The schema name to generate.' |
|
40
|
1 |
|
); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$output = new SymfonyStyle($input, $output); |
|
46
|
|
|
|
|
47
|
|
|
$request = [ |
|
48
|
1 |
|
'query' => Introspection::getIntrospectionQuery(false), |
|
49
|
1 |
|
'variables' => [], |
|
50
|
1 |
|
'operationName' => null, |
|
51
|
1 |
|
]; |
|
52
|
1 |
|
$schemaName = $input->getOption('schema'); |
|
53
|
|
|
|
|
54
|
1 |
|
$container = $this->getContainer(); |
|
55
|
|
|
$result = $container |
|
56
|
1 |
|
->get('overblog_graphql.request_executor') |
|
57
|
1 |
|
->execute($request, [], $schemaName) |
|
58
|
1 |
|
->toArray(); |
|
59
|
|
|
|
|
60
|
1 |
|
$file = $input->getOption('file') ?: $container->getParameter('kernel.root_dir').sprintf('/../var/schema%s.json', $schemaName? '.'.$schemaName:''); |
|
61
|
|
|
|
|
62
|
1 |
|
$schema = json_encode($result['data']); |
|
63
|
|
|
|
|
64
|
1 |
|
file_put_contents($file, $schema); |
|
65
|
|
|
|
|
66
|
1 |
|
$output->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file))); |
|
67
|
1 |
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|