Skip to content

Commit ea2ea45

Browse files
authored
Merge pull request #2691 from soyuka/fix-graphql
GraphQl: Use compiler pass only when enabled
2 parents be932e9 + 8dc2421 commit ea2ea45

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlMutationResolverPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ final class GraphQlMutationResolverPass implements CompilerPassInterface
3131
*/
3232
public function process(ContainerBuilder $container)
3333
{
34+
if (!$container->getParameter('api_platform.graphql.enabled')) {
35+
return;
36+
}
37+
3438
$mutations = [];
3539
foreach ($container->findTaggedServiceIds('api_platform.graphql.mutation_resolver', true) as $serviceId => $tags) {
3640
foreach ($tags as $tag) {

src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlQueryResolverPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ final class GraphQlQueryResolverPass implements CompilerPassInterface
3131
*/
3232
public function process(ContainerBuilder $container)
3333
{
34+
if (!$container->getParameter('api_platform.graphql.enabled')) {
35+
return;
36+
}
37+
3438
$resolvers = [];
3539
foreach ($container->findTaggedServiceIds('api_platform.graphql.query_resolver', true) as $serviceId => $tags) {
3640
foreach ($tags as $tag) {

src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlTypePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ final class GraphQlTypePass implements CompilerPassInterface
3131
*/
3232
public function process(ContainerBuilder $container)
3333
{
34+
if (!$container->getParameter('api_platform.graphql.enabled')) {
35+
return;
36+
}
37+
3438
$types = [];
3539
foreach ($container->findTaggedServiceIds('api_platform.graphql.type', true) as $serviceId => $tags) {
3640
foreach ($tags as $tag) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlMutationResolverPass;
17+
use PHPUnit\Framework\TestCase;
18+
use Prophecy\Argument;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
/**
25+
* @author Alan Poulain <[email protected]>
26+
*/
27+
class GraphQlMutationResolverPassTest extends TestCase
28+
{
29+
public function testProcess()
30+
{
31+
$filterPass = new GraphQlMutationResolverPass();
32+
33+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
34+
35+
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class);
36+
$typeLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) {
37+
return !isset($arg['foo']) && isset($arg['bar']) && $arg['bar'] instanceof Reference;
38+
}))->shouldBeCalled();
39+
40+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
41+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
42+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.mutation_resolver', true)->willReturn(['foo' => [], 'bar' => [['id' => 'bar']]])->shouldBeCalled();
43+
$containerBuilderProphecy->getDefinition('api_platform.graphql.mutation_resolver_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled();
44+
45+
$filterPass->process($containerBuilderProphecy->reveal());
46+
}
47+
48+
public function testDisabled()
49+
{
50+
$filterPass = new GraphQlMutationResolverPass();
51+
52+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
53+
54+
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class);
55+
$typeLocatorDefinitionProphecy->addArgument(Argument::any())->shouldNotBeCalled();
56+
57+
$typesFactoryDefinitionProphecy = $this->prophesize(Definition::class);
58+
$typesFactoryDefinitionProphecy->addArgument(['my_id'])->shouldNotBeCalled();
59+
60+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
61+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(false)->shouldBeCalled();
62+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.mutation_resolver', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldNotBeCalled();
63+
64+
$filterPass->process($containerBuilderProphecy->reveal());
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlQueryResolverPass;
17+
use PHPUnit\Framework\TestCase;
18+
use Prophecy\Argument;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
/**
25+
* @author Alan Poulain <[email protected]>
26+
*/
27+
class GraphQlQueryResolverPassTest extends TestCase
28+
{
29+
public function testProcess()
30+
{
31+
$filterPass = new GraphQlQueryResolverPass();
32+
33+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
34+
35+
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class);
36+
$typeLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) {
37+
return !isset($arg['foo']) && isset($arg['bar']) && $arg['bar'] instanceof Reference;
38+
}))->shouldBeCalled();
39+
40+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
41+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
42+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.query_resolver', true)->willReturn(['foo' => [], 'bar' => [['id' => 'bar']]])->shouldBeCalled();
43+
$containerBuilderProphecy->getDefinition('api_platform.graphql.query_resolver_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled();
44+
45+
$filterPass->process($containerBuilderProphecy->reveal());
46+
}
47+
48+
public function testDisabled()
49+
{
50+
$filterPass = new GraphQlQueryResolverPass();
51+
52+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
53+
54+
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class);
55+
$typeLocatorDefinitionProphecy->addArgument(Argument::any())->shouldNotBeCalled();
56+
57+
$typesFactoryDefinitionProphecy = $this->prophesize(Definition::class);
58+
$typesFactoryDefinitionProphecy->addArgument(['my_id'])->shouldNotBeCalled();
59+
60+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
61+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(false)->shouldBeCalled();
62+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.query_resolver', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldNotBeCalled();
63+
64+
$filterPass->process($containerBuilderProphecy->reveal());
65+
}
66+
}

tests/Bridge/Symfony/Bundle/DependencyInjection/Compiler/GraphQlTypePassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function testProcess()
4141
$typesFactoryDefinitionProphecy->addArgument(['my_id'])->shouldBeCalled();
4242

4343
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
44+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
4445
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.type', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldBeCalled();
4546
$containerBuilderProphecy->getDefinition('api_platform.graphql.type_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled();
4647
$containerBuilderProphecy->getDefinition('api_platform.graphql.types_factory')->willReturn($typesFactoryDefinitionProphecy->reveal())->shouldBeCalled();
@@ -63,10 +64,32 @@ public function testIdNotExist()
6364
$typesFactoryDefinitionProphecy->addArgument(['bar'])->shouldBeCalled();
6465

6566
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
67+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
6668
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.type', true)->willReturn(['foo' => [], 'bar' => [['hi' => 'hello']]])->shouldBeCalled();
6769
$containerBuilderProphecy->getDefinition('api_platform.graphql.type_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldBeCalled();
6870
$containerBuilderProphecy->getDefinition('api_platform.graphql.types_factory')->willReturn($typesFactoryDefinitionProphecy->reveal())->shouldBeCalled();
6971

7072
$filterPass->process($containerBuilderProphecy->reveal());
7173
}
74+
75+
public function testDisabled()
76+
{
77+
$filterPass = new GraphQlTypePass();
78+
79+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
80+
81+
$typeLocatorDefinitionProphecy = $this->prophesize(Definition::class);
82+
$typeLocatorDefinitionProphecy->addArgument(Argument::any())->shouldNotBeCalled();
83+
84+
$typesFactoryDefinitionProphecy = $this->prophesize(Definition::class);
85+
$typesFactoryDefinitionProphecy->addArgument(['my_id'])->shouldNotBeCalled();
86+
87+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
88+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(false)->shouldBeCalled();
89+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.type', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldNotBeCalled();
90+
$containerBuilderProphecy->getDefinition('api_platform.graphql.type_locator')->willReturn($typeLocatorDefinitionProphecy->reveal())->shouldNotBeCalled();
91+
$containerBuilderProphecy->getDefinition('api_platform.graphql.types_factory')->willReturn($typesFactoryDefinitionProphecy->reveal())->shouldNotBeCalled();
92+
93+
$filterPass->process($containerBuilderProphecy->reveal());
94+
}
7295
}

0 commit comments

Comments
 (0)