Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2492 from alanpoulain/graphql-custom-types
[GraphQL] Custom types
- Loading branch information
Showing
15 changed files
with
317 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlTypePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Injects GraphQL types. | ||
* | ||
* @internal | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class GraphQlTypePass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$types = []; | ||
foreach ($container->findTaggedServiceIds('api_platform.graphql.type', true) as $serviceId => $tags) { | ||
foreach ($tags as $tag) { | ||
$types[$tag['id'] ?? $serviceId] = new Reference($serviceId); | ||
} | ||
} | ||
|
||
$container->getDefinition('api_platform.graphql.type_locator')->addArgument($types); | ||
$container->getDefinition('api_platform.graphql.types_factory')->addArgument(array_keys($types)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\GraphQl\Type\Definition; | ||
|
||
use GraphQL\Type\Definition\LeafType; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
interface TypeInterface extends LeafType | ||
{ | ||
public function getName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\GraphQl\Type; | ||
|
||
use ApiPlatform\Core\GraphQl\Type\Definition\TypeInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Get the registered services corresponding to GraphQL types. | ||
* | ||
* @experimental | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class TypesFactory implements TypesFactoryInterface | ||
{ | ||
private $typeLocator; | ||
private $typeIds; | ||
|
||
/** | ||
* @param string[] $typeIds | ||
*/ | ||
public function __construct(ContainerInterface $typeLocator, array $typeIds) | ||
{ | ||
$this->typeLocator = $typeLocator; | ||
$this->typeIds = $typeIds; | ||
} | ||
|
||
public function getTypes(): array | ||
{ | ||
$types = []; | ||
|
||
foreach ($this->typeIds as $typeId) { | ||
/** @var TypeInterface $type */ | ||
$type = $this->typeLocator->get($typeId); | ||
$types[$type->getName()] = $type; | ||
} | ||
|
||
return $types; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\GraphQl\Type; | ||
|
||
/** | ||
* Get the GraphQL types. | ||
* | ||
* @experimental | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
interface TypesFactoryInterface | ||
{ | ||
public function getTypes(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlTypePassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
class GraphQlTypePassTest extends TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$filterPass = new GraphQlTypePass(); | ||
|
||
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass); | ||
|
||
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class); | ||
$typeLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) { | ||
return !isset($arg['foo']) && isset($arg['my_id']) && $arg['my_id'] instanceof Reference; | ||
}))->shouldBeCalled(); | ||
|
||
$typesFactoryDefinitionProphecy = $this->prophesize(Definition::class); | ||
$typesFactoryDefinitionProphecy->addArgument(['my_id'])->shouldBeCalled(); | ||
|
||
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); | ||
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.type', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldBeCalled(); | ||
$containerBuilderProphecy->getDefinition('api_platform.graphql.type_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled(); | ||
$containerBuilderProphecy->getDefinition('api_platform.graphql.types_factory')->willReturn($typesFactoryDefinitionProphecy->reveal())->shouldBeCalled(); | ||
|
||
$filterPass->process($containerBuilderProphecy->reveal()); | ||
} | ||
|
||
public function testIdNotExist() | ||
{ | ||
$filterPass = new GraphQlTypePass(); | ||
|
||
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass); | ||
|
||
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class); | ||
$typeLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) { | ||
return !isset($arg['foo']) && isset($arg['bar']) && $arg['bar'] instanceof Reference; | ||
}))->shouldBeCalled(); | ||
|
||
$typesFactoryDefinitionProphecy = $this->prophesize(Definition::class); | ||
$typesFactoryDefinitionProphecy->addArgument(['bar'])->shouldBeCalled(); | ||
|
||
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); | ||
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.type', true)->willReturn(['foo' => [], 'bar' => [['hi' => 'hello']]])->shouldBeCalled(); | ||
$containerBuilderProphecy->getDefinition('api_platform.graphql.type_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled(); | ||
$containerBuilderProphecy->getDefinition('api_platform.graphql.types_factory')->willReturn($typesFactoryDefinitionProphecy->reveal())->shouldBeCalled(); | ||
|
||
$filterPass->process($containerBuilderProphecy->reveal()); | ||
} | ||
} |
Oops, something went wrong.