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

Add input-object as an option, to the type make command. #298

Closed
Closed
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
41 changes: 31 additions & 10 deletions src/Folklore/GraphQL/Console/TypeMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Folklore\GraphQL\Console;

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

class TypeMakeCommand extends GeneratorCommand
{
Expand All @@ -12,7 +11,9 @@ class TypeMakeCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'make:graphql:type {name}';
protected $signature = 'make:graphql:type
{name : The name of the type class}
{--O|object : Create a new input object type}';

/**
* The console command description.
Expand Down Expand Up @@ -41,7 +42,8 @@ protected function getStub()
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
Expand All @@ -52,7 +54,8 @@ protected function getDefaultNamespace($rootNamespace)
/**
* Build the class with the given name.
*
* @param string $name
* @param string $name
*
* @return string
*/
protected function buildClass($name)
Expand All @@ -65,19 +68,37 @@ protected function buildClass($name)
/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
* @param string $stub
* @param string $name
*
* @return $this
*/
protected function replaceType($stub, $name)
{
preg_match('/([^\\\]+)$/', $name, $matches);
$stub = str_replace(
'DummyType',
$matches[1],

$replace = [$matches[1]];

$this->addInputObjectAttribute($replace);

return str_replace(
['DummyType', 'DummyInputObject'],
$replace,
$stub
);
}

return $stub;
/**
* Add input object attribute to replace type.
*
* @param array $replace
*/
protected function addInputObjectAttribute(array &$replace): void
{
if ($this->option('object')) {
array_push($replace, 'protected $inputObject = true;');
} else {
array_push($replace, '');
}
}
}
6 changes: 5 additions & 1 deletion src/Folklore/GraphQL/Console/stubs/type.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use GraphQL;

class DummyClass extends BaseType
{
DummyInputObject
protected $attributes = [
'name' => 'DummyType',
'description' => 'A type'
Expand All @@ -16,7 +17,10 @@ class DummyClass extends BaseType
public function fields()
{
return [

'sampleField' => [
'type' => Type::string(),
'description' => 'Sample field description',
],
];
}
}