diff --git a/src/Console/Commands/GraphqliteExportSchema.php b/src/Console/Commands/GraphqliteExportSchema.php new file mode 100644 index 0000000..13ae9a5 --- /dev/null +++ b/src/Console/Commands/GraphqliteExportSchema.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Providers/GraphQLiteServiceProvider.php b/src/Providers/GraphQLiteServiceProvider.php index e7a665f..f92f5d9 100644 --- a/src/Providers/GraphQLiteServiceProvider.php +++ b/src/Providers/GraphQLiteServiceProvider.php @@ -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; @@ -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; @@ -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)) { @@ -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 ]; diff --git a/tests/Console/Commands/GraphqliteExportSchemaTest.php b/tests/Console/Commands/GraphqliteExportSchemaTest.php new file mode 100644 index 0000000..7e68a99 --- /dev/null +++ b/tests/Console/Commands/GraphqliteExportSchemaTest.php @@ -0,0 +1,28 @@ +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'); + } +}