Files
kupshop/bundles/KupShop/GraphQLBundle/Command/SchemaExportCommand.php
2025-08-02 16:30:27 +02:00

56 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GraphQLBundle\Command;
use GraphQL\Utils\SchemaPrinter;
use KupShop\GraphQLBundle\Util\SchemaFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SchemaExportCommand extends Command
{
public function __construct(
private readonly SchemaFactory $schemaFactory,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$schema = match ($input->getArgument('api')) {
'public' => $this->schemaFactory->createPublicSchema(),
'pos' => $this->schemaFactory->createPosSchema(),
'admin' => $this->schemaFactory->createAdminSchema(),
default => throw new \InvalidArgumentException('Invalid argument'),
};
if ($file = $input->getOption('file')) {
$output->writeln('=== Dumping schema to file '.realpath($file).'...');
file_put_contents($file, SchemaPrinter::doPrint($schema));
} else {
$output->write(SchemaPrinter::doPrint($schema));
}
return Command::SUCCESS;
}
protected function configure(): void
{
$this->setName('kupshop:graphql-dump-schema')
->setDescription('Prints the GraphQL schema in Schema Definition Language (SDL)')
->addArgument('api', InputArgument::OPTIONAL, 'Which api schema to export [public, pos, admin]', 'public')
->addOption('file', 'f', InputArgument::OPTIONAL, 'File to export to');
}
public function isEnabled(): bool
{
return isLocalDevelopment();
}
}