Skip to content

Command to dump schema #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Command/DumpSchemaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\Graphqlite\Bundle\Command;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Utils\SchemaPrinter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TheCodingMachine\GraphQLite\Schema;

/**
* Shamelessly stolen from Api Platform
*/
class DumpSchemaCommand extends Command
{
protected static $defaultName = 'graphqlite:dump-schema';

/**
* @var Schema
*/
private $schema;

public function __construct(Schema $schema)
{
$this->schema = $schema;

parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Export the GraphQL schema in Schema Definition Language (SDL)')
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Write output to file');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

// Trying to guarantee deterministic order
$this->sortSchema();

$schemaExport = SchemaPrinter::doPrint($this->schema);

$filename = $input->getOption('output');
if (\is_string($filename)) {
file_put_contents($filename, $schemaExport);
$io->success(sprintf('Data written to %s.', $filename));
} else {
$output->writeln($schemaExport);
}

return 0;
}

private function sortSchema(): void
{
$config = $this->schema->getConfig();

$refl = new \ReflectionProperty(ObjectType::class, 'fields');
$refl->setAccessible(true);

$fields = $config->query->getFields();
ksort($fields);
$refl->setValue($config->query, $fields);

$fields = $config->mutation->getFields();
ksort($fields);
$refl->setValue($config->mutation, $fields);
}
}
2 changes: 2 additions & 0 deletions Resources/config/container/graphqlite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,7 @@
</argument>
<tag name="kernel.cache_clearer"/>
</service>

<service id="TheCodingMachine\Graphqlite\Bundle\Command\DumpSchemaCommand" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace TheCodingMachine\Graphqlite\Bundle\Tests\Command;


use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use TheCodingMachine\Graphqlite\Bundle\Tests\GraphqliteTestingKernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

class DumpSchemaCommandTest extends TestCase
{
public function testExecute()
{
$kernel = new GraphqliteTestingKernel();
$application = new Application($kernel);

$command = $application->find('graphqlite:dump-schema');
$commandTester = new CommandTester($command);
$commandTester->execute([]);

$this->assertRegExp(
'/type Product {[\s"]*seller: Contact\s*name: String!\s*price: Float!\s*}/',
$commandTester->getDisplay()
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"nyholm/psr7": "^1.1",
"laminas/laminas-diactoros": "^2.2.2",
"overblog/graphiql-bundle": "^0.1.2 | ^0.2",
"thecodingmachine/cache-utils": "^1"
"thecodingmachine/cache-utils": "^1",
"symfony/console": "^4.1.9 | ^5"
},
"require-dev": {
"symfony/security-bundle": "^4.2 || ^5",
Expand Down