Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add compiler pass to collect GraphQL query and mutation types
  • Loading branch information
mtkoltan committed Feb 5, 2019
1 parent 30f36e6 commit 5cc4ee1
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/Internal/Application/ContainerBuilder.php
Expand Up @@ -10,6 +10,7 @@
use OxidEsales\EshopCommunity\Internal\Application\Utility\BasicContextInterface; use OxidEsales\EshopCommunity\Internal\Application\Utility\BasicContextInterface;
use OxidEsales\EshopCommunity\Internal\Application\Dao\ProjectYamlDao; use OxidEsales\EshopCommunity\Internal\Application\Dao\ProjectYamlDao;
use OxidEsales\EshopCommunity\Internal\Application\Service\ProjectYamlImportService; use OxidEsales\EshopCommunity\Internal\Application\Service\ProjectYamlImportService;
use OxidEsales\EshopCommunity\Internal\Application\Utility\GraphQlTypePass;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function getContainer(): SymfonyContainerBuilder
{ {
$symfonyContainer = new SymfonyContainerBuilder(); $symfonyContainer = new SymfonyContainerBuilder();
$symfonyContainer->addCompilerPass(new RegisterListenersPass()); $symfonyContainer->addCompilerPass(new RegisterListenersPass());
$symfonyContainer->addCompilerPass(new GraphQlTypePass());
$symfonyContainer->addCompilerPass(new AddConsoleCommandPass()); $symfonyContainer->addCompilerPass(new AddConsoleCommandPass());
$this->loadServiceFiles($symfonyContainer); $this->loadServiceFiles($symfonyContainer);
$this->loadEditionServices($symfonyContainer); $this->loadEditionServices($symfonyContainer);
Expand Down
89 changes: 89 additions & 0 deletions source/Internal/Application/Utility/GraphQlTypePass.php
@@ -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)));
}
}
}
11 changes: 11 additions & 0 deletions tests/Integration/Internal/Application/ContainerBuilderTest.php
Expand Up @@ -63,6 +63,17 @@ public function testWhenProjectOverwritesEditions()
$this->assertSame('Service overwriting for Project!', $container->get('oxid_esales.tests.internal.dummy_executor')->execute()); $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 * @param BasicContext $context
* @return \Symfony\Component\DependencyInjection\Container * @return \Symfony\Component\DependencyInjection\Container
Expand Down
@@ -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';
}

}
Expand Up @@ -2,3 +2,17 @@ services:
oxid_esales.tests.internal.dummy_executor: oxid_esales.tests.internal.dummy_executor:
class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\DummyExecutor class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Application\Fixtures\CE\DummyExecutor
public: true 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.