Skip to content

Commit 3afc2ee

Browse files
authored
Merge pull request #93 from lingoda/dump-schema-command
Command to dump schema
2 parents 5481ecb + d7cc7e5 commit 3afc2ee

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

Command/DumpSchemaCommand.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\Graphqlite\Bundle\Command;
6+
7+
use GraphQL\Type\Definition\ObjectType;
8+
use GraphQL\Utils\SchemaPrinter;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Style\SymfonyStyle;
14+
use TheCodingMachine\GraphQLite\Schema;
15+
16+
/**
17+
* Shamelessly stolen from Api Platform
18+
*/
19+
class DumpSchemaCommand extends Command
20+
{
21+
protected static $defaultName = 'graphqlite:dump-schema';
22+
23+
/**
24+
* @var Schema
25+
*/
26+
private $schema;
27+
28+
public function __construct(Schema $schema)
29+
{
30+
$this->schema = $schema;
31+
32+
parent::__construct();
33+
}
34+
35+
protected function configure(): void
36+
{
37+
$this
38+
->setDescription('Export the GraphQL schema in Schema Definition Language (SDL)')
39+
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Write output to file');
40+
}
41+
42+
protected function execute(InputInterface $input, OutputInterface $output): int
43+
{
44+
$io = new SymfonyStyle($input, $output);
45+
46+
// Trying to guarantee deterministic order
47+
$this->sortSchema();
48+
49+
$schemaExport = SchemaPrinter::doPrint($this->schema);
50+
51+
$filename = $input->getOption('output');
52+
if (\is_string($filename)) {
53+
file_put_contents($filename, $schemaExport);
54+
$io->success(sprintf('Data written to %s.', $filename));
55+
} else {
56+
$output->writeln($schemaExport);
57+
}
58+
59+
return 0;
60+
}
61+
62+
private function sortSchema(): void
63+
{
64+
$config = $this->schema->getConfig();
65+
66+
$refl = new \ReflectionProperty(ObjectType::class, 'fields');
67+
$refl->setAccessible(true);
68+
69+
$fields = $config->query->getFields();
70+
ksort($fields);
71+
$refl->setValue($config->query, $fields);
72+
73+
$fields = $config->mutation->getFields();
74+
ksort($fields);
75+
$refl->setValue($config->mutation, $fields);
76+
}
77+
}

Resources/config/container/graphqlite.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,7 @@
129129
</argument>
130130
<tag name="kernel.cache_clearer"/>
131131
</service>
132+
133+
<service id="TheCodingMachine\Graphqlite\Bundle\Command\DumpSchemaCommand" />
132134
</services>
133135
</container>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Graphqlite\Bundle\Tests\Command;
5+
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Tester\CommandTester;
9+
use TheCodingMachine\Graphqlite\Bundle\Tests\GraphqliteTestingKernel;
10+
use Symfony\Bundle\FrameworkBundle\Console\Application;
11+
12+
class DumpSchemaCommandTest extends TestCase
13+
{
14+
public function testExecute()
15+
{
16+
$kernel = new GraphqliteTestingKernel();
17+
$application = new Application($kernel);
18+
19+
$command = $application->find('graphqlite:dump-schema');
20+
$commandTester = new CommandTester($command);
21+
$commandTester->execute([]);
22+
23+
$this->assertRegExp(
24+
'/type Product {[\s"]*seller: Contact\s*name: String!\s*price: Float!\s*}/',
25+
$commandTester->getDisplay()
26+
);
27+
}
28+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"nyholm/psr7": "^1.1",
3030
"laminas/laminas-diactoros": "^2.2.2",
3131
"overblog/graphiql-bundle": "^0.1.2 | ^0.2",
32-
"thecodingmachine/cache-utils": "^1"
32+
"thecodingmachine/cache-utils": "^1",
33+
"symfony/console": "^4.1.9 | ^5"
3334
},
3435
"require-dev": {
3536
"symfony/security-bundle": "^4.2 || ^5",

0 commit comments

Comments
 (0)