Skip to content

Adding a graphqlite:export-schema command #44

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 1 commit into from
Apr 28, 2023
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
58 changes: 58 additions & 0 deletions src/Console/Commands/GraphqliteExportSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace TheCodingMachine\GraphQLite\Laravel\Console\Commands;

use GraphQL\Utils\SchemaPrinter;
use Illuminate\Console\Command;
use TheCodingMachine\GraphQLite\Schema;

/**
* A command to export the GraphQL schema in "Schema Definition Language" (SDL) format.
*/
class GraphqliteExportSchema extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'graphqlite:export-schema {--O|output= : Output file name. If not specified, prints on stdout}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Exports the GraphQL schema in "Schema Definition Language" (SDL) format.';

public function __construct(private Schema $schema)
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$output = $this->option('output');

$sdl = SchemaPrinter::doPrint($this->schema, [
"sortArguments" => true,
"sortEnumValues" => true,
"sortFields" => true,
"sortInputFields" => true,
"sortTypes" => true,
]);

if ($output === null) {
$this->line($sdl);
} else {
file_put_contents($output, $sdl);
}

return Command::SUCCESS;
}
}
15 changes: 15 additions & 0 deletions src/Providers/GraphQLiteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TheCodingMachine\GraphQLite\Laravel\Providers;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\SchemaConfig;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -21,6 +23,7 @@
use TheCodingMachine\GraphQLite\Exceptions\WebonyxErrorHandler;
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
use TheCodingMachine\GraphQLite\Laravel\Console\Commands\GraphqliteExportSchema;
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
use TheCodingMachine\GraphQLite\Laravel\Mappers\Parameters\ValidateFieldMiddleware;
use TheCodingMachine\GraphQLite\Laravel\Mappers\PaginatorTypeMapper;
Expand Down Expand Up @@ -71,6 +74,10 @@ public function boot(Dispatcher $events)
*/
public function register()
{
$this->commands([
GraphqliteExportSchema::class,
]);

$this->app->bind(WebonyxSchema::class, Schema::class);

if (!$this->app->has(ServerRequestFactoryInterface::class)) {
Expand Down Expand Up @@ -146,6 +153,14 @@ public function register()

$service->addTypeMapperFactory($app[PaginatorTypeMapperFactory::class]);

// We need to configure an empty Subscription type to avoid an exception in the generate-schema command.
$config = SchemaConfig::create();
$config->setSubscription(new ObjectType([
'name' => 'Subscription',
'fields' => [],
]));
$service->setSchemaConfig($config);

$controllers = config('graphqlite.controllers', 'App\\Http\\Controllers');
if (!is_iterable($controllers)) {
$controllers = [ $controllers ];
Expand Down
28 changes: 28 additions & 0 deletions tests/Console/Commands/GraphqliteExportSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TheCodingMachine\GraphQLite\Laravel\Console\Commands;


use Orchestra\Testbench\TestCase;
use TheCodingMachine\GraphQLite\Laravel\Providers\GraphQLiteServiceProvider;
use TheCodingMachine\TDBM\TDBMService;


class GraphqliteExportSchemaTest extends TestCase
{
protected function getPackageProviders($app)
{
return [GraphQLiteServiceProvider::class];
}

public function testCommand(): void
{
$this->artisan('graphqlite:export-schema -O test.graphql')
->assertExitCode(0);

$this->assertFileExists('test.graphql');
$content = file_get_contents('test.graphql');
$this->assertStringContainsString('type Query {', $content);
unlink('test.graphql');
}
}