Permalink
Browse files

Add compiler pass to collect GraphQL query and mutation types

  • Loading branch information...
mtkoltan committed Feb 5, 2019
1 parent 30f36e6 commit 5cc4ee18640bdfcdbdd4fa9cac5c6332f0f2f2cd
@@ -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);
@@ -0,0 +1,89 @@
<?php declare(strict_types=1);
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Internal\Application\Utility;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Class GraphQlTypePass
*
* This class configures the query/mutation type factories used
* to build the GraphQL schema. It collects all appropriately
* tagged queries / mutations and places them into the factories.
*
* @package OxidEsales\EshopCommunity\Internal\Application\Utility
*/
class GraphQlTypePass implements CompilerPassInterface
{
/**
* @var string $queryTypeFactoryId The container key for the query
* type factory
*/
protected $queryTypeFactoryId;
/**
* @var string $queryTypeTag The tag string for query types
*/
protected $queryTypeTag;
/**
* @var string $mutationTypeFactoryId The container key for the
* mutation type factory
*/
protected $mutationTypeFactoryId;
/**
* @var string $mutationTypeTag The tag string for the mutation types
*/
protected $mutationTypeTag;
/**
* GraphQlTypePass constructor.
*
* @param string $queryTypeFactoryId
* @param string $queryTypeTag
* @param string $mutationTypeFactoryId
* @param string $mutationTypeTag
*/
public function __construct(
$queryTypeFactoryId = 'query_type_factory',
$queryTypeTag = 'graphql_query_type',
$mutationTypeFactoryId = 'mutation_type_factory',
$mutationTypeTag = 'graphql_mutation_type'
) {
$this->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)));
}
}
}
@@ -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
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application;
class DummyGraphQLType
{
public function getInfo()
{
return 'Type is installed';
}
}
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\Internal\Application;
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
class DummyGraphQLTypeFactory
{
/**
* @var DummyGraphQLType
*/
private $type = null;
public function addSubType($type)
{
$this->type = $type;
}
public function verifySubType()
{
return $this->type !== null && $this->type->getInfo() == 'Type is installed';
}
}
@@ -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']

0 comments on commit 5cc4ee1

Please sign in to comment.