-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dependencies updated Controller has been updated for the Symfony v3 Additional checks were implemented GraphQL Schema class was moved to the config file as parameter and is now being used inside the controller
- Loading branch information
Showing
6 changed files
with
168 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace AppBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
class GenerateSchemaCommand extends ContainerAwareCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('graphql:generate-schema') | ||
->setDescription('Generates GraphQL Schema class') | ||
->addArgument('bundle', InputArgument::REQUIRED, 'Bundle to generate class to'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$bundleName = $input->getArgument('bundle'); | ||
if (strpos($bundleName, -6) != 'Bundle') $bundleName .= 'Bundle'; | ||
|
||
$srcPath = realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../src'); | ||
$activeBundles = $this->getContainer()->getParameter('kernel.bundles'); | ||
if (!array_key_exists($bundleName, $activeBundles)) { | ||
$output->writeln('There is no active bundleName: ' . $bundleName); | ||
} | ||
|
||
$graphqlPath = $srcPath . '/' . $bundleName . '/GraphQL'; | ||
$className = 'Schema'; | ||
$classPath = $graphqlPath . '/' . $className . '.php'; | ||
|
||
$inputHelper = $this->getHelper('question'); | ||
$question = new ConfirmationQuestion(sprintf('Confirm creating class at %s ?', $classPath), false); | ||
if (!$inputHelper->ask($input, $output, $question)) { | ||
return; | ||
} | ||
|
||
if (!is_dir($graphqlPath)) { | ||
mkdir($graphqlPath, 0777, true); | ||
} | ||
file_put_contents($classPath, $this->getSchemaClassTemplate($bundleName, $className)); | ||
|
||
$output->writeln('Schema file has been created at'); | ||
$output->writeln($classPath . "\n"); | ||
$output->writeln('Update your app/config/config.yml with the parameter:'); | ||
$output->writeln('graph_ql:'); | ||
$output->writeln(sprintf(' schema_class: %s\GraphQL\%s', $bundleName, $className)); | ||
} | ||
|
||
protected function getSchemaClassTemplate($bundleName, $className = 'Schema') | ||
{ | ||
$tpl = <<<TEXT | ||
<?php | ||
/** | ||
* This class was automatically generated by GraphQL Schema generator | ||
*/ | ||
namespace $bundleName\GraphQL; | ||
use Youshido\GraphQL\AbstractSchema; | ||
use Youshido\GraphQL\Type\Config\Schema\SchemaConfig; | ||
use Youshido\GraphQL\Type\Scalar\StringType; | ||
class $className extends AbstractSchema | ||
{ | ||
public function build(SchemaConfig \$config) | ||
{ | ||
\$config->getQuery()->addFields([ | ||
'hello' => [ | ||
'type' => new StringType(), | ||
'resolve' => function () { | ||
return 'world!'; | ||
} | ||
] | ||
]); | ||
} | ||
} | ||
TEXT; | ||
|
||
return $tpl; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters