Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Add command for making input type #310

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions src/Folklore/GraphQL/Console/InputMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Folklore\GraphQL\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class InputMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:graphql:input {name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new GraphQL input class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Input';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/stubs/input.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\GraphQL\Inputs';
}

/**
* Build the class with the given name.
*
* @param string $name
*
* @return string
*/
protected function buildClass($name)
{
$stub = parent::buildClass($name);

return $this->replaceType($stub, $name);
}

/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
*
* @return $this
*/
protected function replaceType($stub, $name)
{
preg_match('/([^\\\]+)$/', $name, $matches);
$stub = str_replace(
'DummyType',
$matches[1],
$stub
);

return $stub;
}
}
22 changes: 22 additions & 0 deletions src/Folklore/GraphQL/Console/stubs/input.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace DummyNamespace;

use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\InputType;
use GraphQL;

class DummyClass extends InputType
{
protected $attributes = [
'name' => 'DummyType',
'description' => 'An input'
];

public function fields()
{
return [

];
}
}
1 change: 1 addition & 0 deletions src/Folklore/GraphQL/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ protected function registerConsole()
$this->commands(Console\FieldMakeCommand::class);
$this->commands(Console\InterfaceMakeCommand::class);
$this->commands(Console\ScalarMakeCommand::class);
$this->commands(Console\InputMakeCommand::class);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Console/ExampleInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\GraphQL\Inputs;

use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\InputType;
use GraphQL;

class ExampleInput extends InputType
{
protected $attributes = [
'name' => 'ExampleInput',
'description' => 'An input'
];

public function fields()
{
return [

];
}
}
25 changes: 25 additions & 0 deletions tests/Console/InputMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Folklore\GraphQL\Console\InputMakeCommand;
use Orchestra\Testbench\TestCase as BaseTestCase;

class InputMakeCommandTest extends BaseTestCase
{
protected function getPackageProviders($app)
{
return [
\Folklore\GraphQL\ServiceProvider::class,
];
}

/** @test */
public function it_makes_an_input_file()
{
$this->artisan('make:graphql:input', [
'name' => 'ExampleInput',
]);

$this->assertFileExists(app_path('GraphQL/Inputs') . '/ExampleInput.php');
$this->assertFileEquals(app_path('GraphQL/Inputs') . '/ExampleInput.php', __DIR__ . '/ExampleInput.php');
}
}