From 9444749b4819ae67e809160490f4329c67025f5d Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Thu, 20 Oct 2016 11:34:34 +0200 Subject: [PATCH 1/2] Removed ResolverConfigurator to win some runtime --- ...MutationTaggedServiceMappingTaggedPass.php | 5 ++ .../ResolverTaggedServiceMappingPass.php | 5 ++ .../Compiler/TaggedServiceMappingPass.php | 25 ++++++-- .../Compiler/TypeTaggedServiceMappingPass.php | 5 ++ .../Configurator/ResolverConfigurator.php | 59 ------------------- Resources/config/services.yml | 11 ---- 6 files changed, 34 insertions(+), 76 deletions(-) delete mode 100644 DependencyInjection/Configurator/ResolverConfigurator.php diff --git a/DependencyInjection/Compiler/MutationTaggedServiceMappingTaggedPass.php b/DependencyInjection/Compiler/MutationTaggedServiceMappingTaggedPass.php index 2ebf57d3e..123099fc4 100644 --- a/DependencyInjection/Compiler/MutationTaggedServiceMappingTaggedPass.php +++ b/DependencyInjection/Compiler/MutationTaggedServiceMappingTaggedPass.php @@ -22,4 +22,9 @@ protected function getParameterName() { return 'overblog_graphql.mutations_mapping'; } + + protected function getResolverServiceID() + { + return 'overblog_graphql.mutation_resolver'; + } } diff --git a/DependencyInjection/Compiler/ResolverTaggedServiceMappingPass.php b/DependencyInjection/Compiler/ResolverTaggedServiceMappingPass.php index 7c2c0b478..d48f5ce6e 100644 --- a/DependencyInjection/Compiler/ResolverTaggedServiceMappingPass.php +++ b/DependencyInjection/Compiler/ResolverTaggedServiceMappingPass.php @@ -33,4 +33,9 @@ protected function checkRequirements($id, array $tag) ); } } + + protected function getResolverServiceID() + { + return 'overblog_graphql.resolver_resolver'; + } } diff --git a/DependencyInjection/Compiler/TaggedServiceMappingPass.php b/DependencyInjection/Compiler/TaggedServiceMappingPass.php index 3c4a7f246..7fd31ecf3 100644 --- a/DependencyInjection/Compiler/TaggedServiceMappingPass.php +++ b/DependencyInjection/Compiler/TaggedServiceMappingPass.php @@ -12,7 +12,9 @@ namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; abstract class TaggedServiceMappingPass implements CompilerPassInterface { @@ -32,14 +34,23 @@ private function getTaggedServiceMapping(ContainerBuilder $container, $tagName) return $serviceMapping; } - private function taggedServiceToParameterMapping(ContainerBuilder $container, $tagName, $parameterName) - { - $container->setParameter($parameterName, $this->getTaggedServiceMapping($container, $tagName)); - } - public function process(ContainerBuilder $container) { - $this->taggedServiceToParameterMapping($container, $this->getTagName(), $this->getParameterName()); + $mapping = $this->getTaggedServiceMapping($container, $this->getTagName()); + $container->setParameter($this->getParameterName(), $mapping); + $resolverDefinition = $container->findDefinition($this->getResolverServiceID()); + + foreach ($mapping as $name => $options) { + $cleanOptions = $options; + $solutionID = $options['id']; + $solution = $container->get($solutionID); + + if ($solution instanceof ContainerAwareInterface) { + $solutionDefinition = $container->findDefinition($options['id']); + $solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]); + } + $resolverDefinition->addMethodCall('addSolution', [$name, new Reference($solutionID), $cleanOptions]); + } } protected function checkRequirements($id, array $tag) @@ -53,5 +64,7 @@ protected function checkRequirements($id, array $tag) abstract protected function getTagName(); + abstract protected function getResolverServiceID(); + abstract protected function getParameterName(); } diff --git a/DependencyInjection/Compiler/TypeTaggedServiceMappingPass.php b/DependencyInjection/Compiler/TypeTaggedServiceMappingPass.php index 82c73e5e9..03f587613 100644 --- a/DependencyInjection/Compiler/TypeTaggedServiceMappingPass.php +++ b/DependencyInjection/Compiler/TypeTaggedServiceMappingPass.php @@ -22,4 +22,9 @@ protected function getParameterName() { return 'overblog_graphql.types_mapping'; } + + protected function getResolverServiceID() + { + return 'overblog_graphql.type_resolver'; + } } diff --git a/DependencyInjection/Configurator/ResolverConfigurator.php b/DependencyInjection/Configurator/ResolverConfigurator.php deleted file mode 100644 index f8f80a8fe..000000000 --- a/DependencyInjection/Configurator/ResolverConfigurator.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Overblog\GraphQLBundle\DependencyInjection\Configurator; - -use Overblog\GraphQLBundle\Resolver\AbstractResolver; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; - -class ResolverConfigurator implements ContainerAwareInterface -{ - use ContainerAwareTrait; - - private $mappings; - - public function addMapping($name, array $mapping = []) - { - $this->mappings[$name] = $mapping; - } - - private function configure(AbstractResolver $resolver, array $mapping) - { - foreach ($mapping as $name => $options) { - $cleanOptions = $options; - $solution = $this->container->get($options['id']); - - if ($solution instanceof ContainerAwareInterface) { - $solution->setContainer($this->container); - } - - $resolver->addSolution($name, $solution, $cleanOptions); - } - } - - public function __call($name, $arguments) - { - if (!preg_match('/^configure(.*)/i', $name, $matches) || !isset($this->mappings[strtolower($matches[1])])) { - throw new \BadMethodCallException(sprintf('Call to unknown method %s', $name)); - } - - $mapping = $this->mappings[strtolower($matches[1])]; - - if (!isset($arguments[0]) || !$arguments[0] instanceof AbstractResolver) { - throw new \InvalidArgumentException( - sprintf('Resolver must implement "%s"', 'Overblog\\GraphQLBundle\\Resolver\\AbstractResolver') - ); - } - - $this->configure($arguments[0], $mapping); - } -} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 75732d3c0..cf2cae6af 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -37,29 +37,18 @@ services: overblog_graphql.type_resolver: class: Overblog\GraphQLBundle\Resolver\TypeResolver - configurator: ["@overblog_graphql.resolver_configurator", "configureType"] arguments: - overblog_graphql.resolver_resolver: class: Overblog\GraphQLBundle\Resolver\ResolverResolver - configurator: ["@overblog_graphql.resolver_configurator", "configureResolver"] overblog_graphql.mutation_resolver: class: Overblog\GraphQLBundle\Resolver\MutationResolver - configurator: ["@overblog_graphql.resolver_configurator", "configureMutation"] overblog_graphql.access_resolver: class: Overblog\GraphQLBundle\Resolver\AccessResolver - overblog_graphql.resolver_configurator: - class: Overblog\GraphQLBundle\DependencyInjection\Configurator\ResolverConfigurator - calls: - - ["setContainer", ["@service_container"]] - - ["addMapping", ["type", "%overblog_graphql.types_mapping%"]] - - ["addMapping", ["resolver", "%overblog_graphql.resolvers_mapping%"]] - - ["addMapping", ["mutation", "%overblog_graphql.mutations_mapping%"]] - overblog_graphql.cache_expression_language_parser.default: class: Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache public: false From 73a8122251e257b1885f0256389048a0d8318ac8 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Thu, 20 Oct 2016 14:48:03 +0200 Subject: [PATCH 2/2] GraphQL command schema dump accepts schema name. --- ...aCommand.php => GraphQLDumpSchemaCommand.php} | 16 ++++++++++++---- .../Command/GraphDumpSchemaCommandTest.php | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) rename Command/{GraphDumpSchemaCommand.php => GraphQLDumpSchemaCommand.php} (75%) diff --git a/Command/GraphDumpSchemaCommand.php b/Command/GraphQLDumpSchemaCommand.php similarity index 75% rename from Command/GraphDumpSchemaCommand.php rename to Command/GraphQLDumpSchemaCommand.php index ca7dc6d72..6fd4b9180 100644 --- a/Command/GraphDumpSchemaCommand.php +++ b/Command/GraphQLDumpSchemaCommand.php @@ -18,18 +18,25 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -class GraphDumpSchemaCommand extends ContainerAwareCommand +class GraphQLDumpSchemaCommand extends ContainerAwareCommand { protected function configure() { $this - ->setName('graph:dump-schema') + ->setName('graphql:dump-schema') + ->setAliases(['graph:dump-schema']) ->setDescription('Dumps GraphQL schema') ->addOption( 'file', null, InputOption::VALUE_OPTIONAL, 'Path to generate schema file.' + ) + ->addOption( + 'schema', + null, + InputOption::VALUE_OPTIONAL, + 'The schema name to generate.' ); } @@ -42,14 +49,15 @@ protected function execute(InputInterface $input, OutputInterface $output) 'variables' => [], 'operationName' => null, ]; + $schemaName = $input->getOption('schema'); $container = $this->getContainer(); $result = $container ->get('overblog_graphql.request_executor') - ->execute($request) + ->execute($request, [], $schemaName) ->toArray(); - $file = $input->getOption('file') ?: $container->getParameter('kernel.root_dir').'/../var/schema.json'; + $file = $input->getOption('file') ?: $container->getParameter('kernel.root_dir').sprintf('/../var/schema%s.json', $schemaName? '.'.$schemaName:''); $schema = json_encode($result['data']); diff --git a/Tests/Functional/Command/GraphDumpSchemaCommandTest.php b/Tests/Functional/Command/GraphDumpSchemaCommandTest.php index f7fb239ba..c55e5e3c9 100644 --- a/Tests/Functional/Command/GraphDumpSchemaCommandTest.php +++ b/Tests/Functional/Command/GraphDumpSchemaCommandTest.php @@ -11,7 +11,7 @@ namespace Overblog\GraphQLBundle\Tests\Functional\Command; -use Overblog\GraphQLBundle\Command\GraphDumpSchemaCommand; +use Overblog\GraphQLBundle\Command\GraphQLDumpSchemaCommand; use Overblog\GraphQLBundle\Tests\Functional\TestCase; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -24,7 +24,7 @@ public function testExecute() $kernel = $client->getKernel(); $application = new Application($kernel); - $application->add(new GraphDumpSchemaCommand()); + $application->add(new GraphQLDumpSchemaCommand()); $command = $application->find('graph:dump-schema'); $file = $kernel->getCacheDir().'/schema.json';