Skip to content

Commit 2057763

Browse files
committed
Addig a graphqlite:export-schema command
This new Laravel command can be run using ``` ./artisan graphqlite:export-schema --output file.schema ``` It will output the content of the Schema to an SDL file. This can be useful for instance for tools like graphql-codegen.
1 parent 4694583 commit 2057763

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Laravel\Console\Commands;
4+
5+
use GraphQL\Utils\SchemaPrinter;
6+
use Illuminate\Console\Command;
7+
use TheCodingMachine\GraphQLite\Schema;
8+
9+
/**
10+
* A command to export the GraphQL schema in "Schema Definition Language" (SDL) format.
11+
*/
12+
class GraphqliteExportSchema extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'graphqlite:export-schema {--O|output= : Output file name. If not specified, prints on stdout}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Exports the GraphQL schema in "Schema Definition Language" (SDL) format.';
27+
28+
public function __construct(private Schema $schema)
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return int
37+
*/
38+
public function handle()
39+
{
40+
$output = $this->option('output');
41+
42+
$sdl = SchemaPrinter::doPrint($this->schema, [
43+
"sortArguments" => true,
44+
"sortEnumValues" => true,
45+
"sortFields" => true,
46+
"sortInputFields" => true,
47+
"sortTypes" => true,
48+
]);
49+
50+
if ($output === null) {
51+
$this->line($sdl);
52+
} else {
53+
file_put_contents($output, $sdl);
54+
}
55+
56+
return Command::SUCCESS;
57+
}
58+
}

src/Providers/GraphQLiteServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TheCodingMachine\GraphQLite\Laravel\Providers;
44

5+
use GraphQL\Type\Definition\ObjectType;
6+
use GraphQL\Type\SchemaConfig;
57
use Illuminate\Contracts\Auth\Access\Gate;
68
use Illuminate\Contracts\Auth\Factory as AuthFactory;
79
use Illuminate\Contracts\Events\Dispatcher;
@@ -21,6 +23,7 @@
2123
use TheCodingMachine\GraphQLite\Exceptions\WebonyxErrorHandler;
2224
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
2325
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
26+
use TheCodingMachine\GraphQLite\Laravel\Console\Commands\GraphqliteExportSchema;
2427
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
2528
use TheCodingMachine\GraphQLite\Laravel\Mappers\Parameters\ValidateFieldMiddleware;
2629
use TheCodingMachine\GraphQLite\Laravel\Mappers\PaginatorTypeMapper;
@@ -71,6 +74,10 @@ public function boot(Dispatcher $events)
7174
*/
7275
public function register()
7376
{
77+
$this->commands([
78+
GraphqliteExportSchema::class,
79+
]);
80+
7481
$this->app->bind(WebonyxSchema::class, Schema::class);
7582

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

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

156+
// We need to configure an empty Subscription type to avoid an exception in the generate-schema command.
157+
$config = SchemaConfig::create();
158+
$config->setSubscription(new ObjectType([
159+
'name' => 'Subscription',
160+
'fields' => [],
161+
]));
162+
$service->setSchemaConfig($config);
163+
149164
$controllers = config('graphqlite.controllers', 'App\\Http\\Controllers');
150165
if (!is_iterable($controllers)) {
151166
$controllers = [ $controllers ];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Laravel\Console\Commands;
4+
5+
6+
use Orchestra\Testbench\TestCase;
7+
use TheCodingMachine\GraphQLite\Laravel\Providers\GraphQLiteServiceProvider;
8+
use TheCodingMachine\TDBM\TDBMService;
9+
10+
11+
class GraphqliteExportSchemaTest extends TestCase
12+
{
13+
protected function getPackageProviders($app)
14+
{
15+
return [GraphQLiteServiceProvider::class];
16+
}
17+
18+
public function testCommand(): void
19+
{
20+
$this->artisan('graphqlite:export-schema -O test.graphql')
21+
->assertExitCode(0);
22+
23+
$this->assertFileExists('test.graphql');
24+
$content = file_get_contents('test.graphql');
25+
$this->assertStringContainsString('type Query {', $content);
26+
unlink('test.graphql');
27+
}
28+
}

0 commit comments

Comments
 (0)