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