Skip to content

Commit

Permalink
Merge pull request #293 from mcg-web/commands_optimizations
Browse files Browse the repository at this point in the history
Commands fixes and optimizations
  • Loading branch information
mcg-web authored Feb 15, 2018
2 parents d005cb4 + a7a5a13 commit a79a7e2
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CompileCommand extends Command
final class CompileCommand extends Command
{
private $typeGenerator;

Expand Down
29 changes: 24 additions & 5 deletions Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ protected function configure()
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
sprintf('filter by a category (%s).', implode(', ', self::$categories))
)
->addOption(
'with-service-id',
null,
InputOption::VALUE_NONE,
'also display service id'
)
->setDescription('Display current GraphQL services (types, resolvers and mutations)');
}

Expand All @@ -66,28 +72,41 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$categories = empty($categoriesOption) ? self::$categories : $categoriesOption;
$withServiceId = $input->getOption('with-service-id');

$io = new SymfonyStyle($input, $output);
$tableHeaders = ['id', 'aliases'];
$tableHeaders = ['solution id', 'aliases'];
if ($withServiceId) {
$tableHeaders[] = 'service id';
}

foreach ($categories as $category) {
$io->title(sprintf('GraphQL %ss Services', ucfirst($category)));

/** @var FluentResolverInterface $resolver */
$resolver = $this->{$category.'Resolver'};
$this->renderTable($resolver, $tableHeaders, $io);
$this->renderTable($resolver, $tableHeaders, $io, $withServiceId);
}
}

private function renderTable(FluentResolverInterface $resolver, array $tableHeaders, SymfonyStyle $io)
/**
* @param FluentResolverInterface $resolver
* @param array $tableHeaders
* @param SymfonyStyle $io
* @param bool $withServiceId
*/
private function renderTable(FluentResolverInterface $resolver, array $tableHeaders, SymfonyStyle $io, $withServiceId)
{
$tableRows = [];
$solutionIDs = array_keys($resolver->getSolutions());
sort($solutionIDs);
foreach ($solutionIDs as $solutionID) {
$aliases = $resolver->getSolutionAliases($solutionID);
$aliases[] = $solutionID;
$options = $resolver->getSolutionOptions($solutionID);
$tableRows[$options['id']] = [$options['id'], self::serializeAliases($aliases, $options)];
$tableRows[$solutionID] = [$solutionID, self::serializeAliases($aliases, $options)];
if ($withServiceId) {
$tableRows[$solutionID][] = $options['id'];
}
}
ksort($tableRows);
$io->table($tableHeaders, $tableRows);
Expand Down
20 changes: 3 additions & 17 deletions Command/GraphQLDumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

use GraphQL\Type\Introspection;
use GraphQL\Utils\SchemaPrinter;
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class GraphQLDumpSchemaCommand extends Command
{
/** @var ContainerInterface */
private $container;
use RequestExecutorLazyLoaderTrait;

/** @var string */
private $baseExportPath;

public function __construct(
ContainerInterface $container,
$baseExportPath
) {
public function __construct($baseExportPath)
{
parent::__construct();
$this->container = $container;
$this->baseExportPath = $baseExportPath;
}

Expand Down Expand Up @@ -123,12 +117,4 @@ private function useModernJsonFormat(InputInterface $input)

return true === $modern;
}

/**
* @return RequestExecutor
*/
private function getRequestExecutor()
{
return $this->container->get('overblog_graphql.request_executor');
}
}
31 changes: 31 additions & 0 deletions Command/RequestExecutorLazyLoaderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Overblog\GraphQLBundle\Command;

use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;

trait RequestExecutorLazyLoaderTrait
{
/** @var RequestExecutor */
private $requestExecutor;

/** @var array */
private $requestExecutorFactory;

public function setRequestExecutorFactory(array $requestExecutorFactory)
{
$this->requestExecutorFactory = $requestExecutorFactory;
}

/**
* @return RequestExecutor
*/
protected function getRequestExecutor()
{
if (null === $this->requestExecutor && null !== $this->requestExecutorFactory) {
$this->requestExecutor = call_user_func_array(...$this->requestExecutorFactory);
}

return $this->requestExecutor;
}
}
2 changes: 1 addition & 1 deletion Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Executor
const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';

/** @var Schema[] */
private $schemas;
private $schemas = [];

/** @var EventDispatcherInterface|null */
private $dispatcher;
Expand Down
5 changes: 3 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ services:
class: Overblog\GraphQLBundle\Command\GraphQLDumpSchemaCommand
public: true
arguments:
- "%kernel.root_dir%"
calls:
# "overblog_graphql.request_executor" service must be load lazy since that command service
# is instanced before ClassLoaderEvent. This issues is fix in Symfony 3.4 introducing lazy commands
# see https://symfony.com/blog/new-in-symfony-3-4-lazy-commands
- '@service_container'
- "%kernel.root_dir%"
- ['setRequestExecutorFactory', [[['@service_container', 'get'], ['overblog_graphql.request_executor']]]]
tags:
- { name: console.command }

Expand Down
29 changes: 21 additions & 8 deletions Tests/Functional/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ public function setUp()
$this->command = static::$kernel->getContainer()->get('overblog_graphql.command.debug');
$this->commandTester = new CommandTester($this->command);

foreach (DebugCommand::getCategories() as $category) {
$this->logs[$category] = trim(
file_get_contents(
sprintf(
__DIR__.'/fixtures/%sdebug-%s.txt',
version_compare(Kernel::VERSION, '3.3.0') >= 0 ? 'case-sensitive/' : '',
$category
)
$categories = DebugCommand::getCategories();
$categories[] = 'all';

foreach ($categories as $category) {
$content = file_get_contents(
sprintf(
__DIR__.'/fixtures/debug/debug-%s.txt',
$category
)
);

$this->logs[$category] = 'all' === $category ? $content : trim($content);
}
}

Expand All @@ -60,6 +62,17 @@ public function testProcess(array $categories)
$this->assertEquals($expected, $this->commandTester->getDisplay());
}

public function testProcessWithServiceId()
{
if (version_compare(Kernel::VERSION, '3.3.0') < 0) {
$this->markTestSkipped('Test only for Symfony >= 3.3.0.');
}

$this->commandTester->execute(['--with-service-id' => null]);
$this->assertEquals(0, $this->commandTester->getStatusCode());
$this->assertEquals($this->logs['all'], $this->commandTester->getDisplay());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid category (fake)
Expand Down

This file was deleted.

This file was deleted.

37 changes: 0 additions & 37 deletions Tests/Functional/Command/fixtures/case-sensitive/debug-type.txt

This file was deleted.

15 changes: 0 additions & 15 deletions Tests/Functional/Command/fixtures/debug-mutation.txt

This file was deleted.

Loading

0 comments on commit a79a7e2

Please sign in to comment.