From 5cc4ee18640bdfcdbdd4fa9cac5c6332f0f2f2cd Mon Sep 17 00:00:00 2001 From: "michael.koltan@oxid-esales.com" Date: Tue, 5 Feb 2019 11:11:47 +0100 Subject: [PATCH] Add compiler pass to collect GraphQL query and mutation types --- .../Internal/Application/ContainerBuilder.php | 2 + .../Application/Utility/GraphQlTypePass.php | 89 +++++++++++++++++++ .../Application/ContainerBuilderTest.php | 11 +++ .../Internal/Application/DummyGraphQLType.php | 12 +++ .../Application/DummyGraphQLTypeFactory.php | 28 ++++++ .../CE/Internal/Application/services.yaml | 14 +++ 6 files changed, 156 insertions(+) create mode 100644 source/Internal/Application/Utility/GraphQlTypePass.php create mode 100644 tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/DummyGraphQLType.php create mode 100644 tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/DummyGraphQLTypeFactory.php diff --git a/source/Internal/Application/ContainerBuilder.php b/source/Internal/Application/ContainerBuilder.php index 74cacd9541..42db792c55 100644 --- a/source/Internal/Application/ContainerBuilder.php +++ b/source/Internal/Application/ContainerBuilder.php @@ -10,6 +10,7 @@ use OxidEsales\EshopCommunity\Internal\Application\Utility\BasicContextInterface; use OxidEsales\EshopCommunity\Internal\Application\Dao\ProjectYamlDao; use OxidEsales\EshopCommunity\Internal\Application\Service\ProjectYamlImportService; +use OxidEsales\EshopCommunity\Internal\Application\Utility\GraphQlTypePass; use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; use Symfony\Component\Config\FileLocator; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; @@ -48,6 +49,7 @@ public function getContainer(): SymfonyContainerBuilder { $symfonyContainer = new SymfonyContainerBuilder(); $symfonyContainer->addCompilerPass(new RegisterListenersPass()); + $symfonyContainer->addCompilerPass(new GraphQlTypePass()); $symfonyContainer->addCompilerPass(new AddConsoleCommandPass()); $this->loadServiceFiles($symfonyContainer); $this->loadEditionServices($symfonyContainer); diff --git a/source/Internal/Application/Utility/GraphQlTypePass.php b/source/Internal/Application/Utility/GraphQlTypePass.php new file mode 100644 index 0000000000..8f46001a27 --- /dev/null +++ b/source/Internal/Application/Utility/GraphQlTypePass.php @@ -0,0 +1,89 @@ +queryTypeFactoryId = $queryTypeFactoryId; + $this->queryTypeTag = $queryTypeTag; + $this->mutationTypeFactoryId = $mutationTypeFactoryId; + $this->mutationTypeTag = $mutationTypeTag; + } + + /** + * @param ContainerBuilder $container + * @return null + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->queryTypeFactoryId) && !$container->hasAlias($this->queryTypeFactoryId)) { + return; + } + + if (!$container->hasDefinition($this->mutationTypeFactoryId) && !$container->hasAlias($this->mutationTypeFactoryId)) { + return; + } + + $queryTypeFactoryDefinition = $container->findDefinition($this->queryTypeFactoryId); + $mutationTypeFactoryDefinition = $container->findDefinition($this->mutationTypeFactoryId); + + foreach ($container->findTaggedServiceIds($this->queryTypeTag, true) as $id => $type) { + $queryTypeFactoryDefinition->addMethodCall('addSubType', array(new Reference($id))); + } + foreach ($container->findTaggedServiceIds($this->mutationTypeTag, true) as $id => $type) { + $mutationTypeFactoryDefinition->addMethodCall('addSubType', array(new Reference($id))); + } + } +} diff --git a/tests/Integration/Internal/Application/ContainerBuilderTest.php b/tests/Integration/Internal/Application/ContainerBuilderTest.php index 137b283e4a..a9ca152adf 100644 --- a/tests/Integration/Internal/Application/ContainerBuilderTest.php +++ b/tests/Integration/Internal/Application/ContainerBuilderTest.php @@ -63,6 +63,17 @@ public function testWhenProjectOverwritesEditions() $this->assertSame('Service overwriting for Project!', $container->get('oxid_esales.tests.internal.dummy_executor')->execute()); } + public function testGraphQLTypeIntegration() + { + $context = $this->makeContextStub(); + $context->method('getEdition')->willReturn(EditionSelector::COMMUNITY); + $context->method('getGeneratedProjectFilePath')->willReturn('not_existing.yaml'); + $container = $this->makeContainer($context); + + $this->assertTrue($container->get('query_type_factory')->verifySubType()); + $this->assertTrue($container->get('mutation_type_factory')->verifySubType()); + } + /** * @param BasicContext $context * @return \Symfony\Component\DependencyInjection\Container diff --git a/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/DummyGraphQLType.php b/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/DummyGraphQLType.php new file mode 100644 index 0000000000..26354013ee --- /dev/null +++ b/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/DummyGraphQLType.php @@ -0,0 +1,12 @@ +type = $type; + } + + public function verifySubType() + { + return $this->type !== null && $this->type->getInfo() == 'Type is installed'; + } + +} diff --git a/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/services.yaml b/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/services.yaml index 318bde10a2..357ee52c08 100644 --- a/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/services.yaml +++ b/tests/Integration/Internal/Application/Fixtures/CE/Internal/Application/services.yaml @@ -2,3 +2,17 @@ services: oxid_esales.tests.internal.dummy_executor: class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\DummyExecutor public: true + + query_type_factory: + class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application\DummyGraphQLTypeFactory + + query_type_key: + class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application\DummyGraphQLType + tags: ['graphql_query_type'] + + mutation_type_factory: + class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application\DummyGraphQLTypeFactory + + mutation_type_key: + class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application\DummyGraphQLType + tags: ['graphql_mutation_type'] \ No newline at end of file