Skip to content

Commit

Permalink
fix(graphql): add a clearer error message when TwigBundle is disable …
Browse files Browse the repository at this point in the history
…but graphQL clients are enabled
  • Loading branch information
ArnoudThibaut committed Oct 17, 2022
1 parent defe717 commit 0d46cd6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/GraphQl/Action/EntrypointAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class EntrypointAction
{
private int $debug;

public function __construct(private readonly SchemaBuilderInterface $schemaBuilder, private readonly ExecutorInterface $executor, private readonly GraphiQlAction $graphiQlAction, private readonly GraphQlPlaygroundAction $graphQlPlaygroundAction, private readonly NormalizerInterface $normalizer, private readonly ErrorHandlerInterface $errorHandler, bool $debug = false, private readonly bool $graphiqlEnabled = false, private readonly bool $graphQlPlaygroundEnabled = false, private readonly ?string $defaultIde = null)
public function __construct(private readonly SchemaBuilderInterface $schemaBuilder, private readonly ExecutorInterface $executor, private readonly ?GraphiQlAction $graphiQlAction, private readonly ?GraphQlPlaygroundAction $graphQlPlaygroundAction, private readonly NormalizerInterface $normalizer, private readonly ErrorHandlerInterface $errorHandler, bool $debug = false, private readonly bool $graphiqlEnabled = false, private readonly bool $graphQlPlaygroundEnabled = false, private readonly ?string $defaultIde = null)
{
$this->debug = $debug ? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE : DebugFlag::NONE;
}
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;

/**
* The extension of this bundle.
Expand Down Expand Up @@ -462,9 +463,12 @@ private function registerGraphQlConfiguration(ContainerBuilder $container, array
{
$enabled = $this->isConfigEnabled($container, $config['graphql']);

$graphiqlEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']);
$graphqlPlayGroundEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphql_playground']);

$container->setParameter('api_platform.graphql.enabled', $enabled);
$container->setParameter('api_platform.graphql.graphiql.enabled', $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']));
$container->setParameter('api_platform.graphql.graphql_playground.enabled', $enabled && $this->isConfigEnabled($container, $config['graphql']['graphql_playground']));
$container->setParameter('api_platform.graphql.graphiql.enabled', $graphiqlEnabled);
$container->setParameter('api_platform.graphql.graphql_playground.enabled', $graphqlPlayGroundEnabled);
$container->setParameter('api_platform.graphql.collection.pagination', $config['graphql']['collection']['pagination']);

if (!$enabled) {
Expand All @@ -476,6 +480,15 @@ private function registerGraphQlConfiguration(ContainerBuilder $container, array

$loader->load('graphql.xml');

// @phpstan-ignore-next-line
if (!class_exists(Environment::class) || !isset($container->getParameter('kernel.bundles')['TwigBundle'])) {
if ($graphiqlEnabled || $graphqlPlayGroundEnabled) {
throw new RuntimeException(sprintf('GraphiQL and GraphQL Playground interfaces depend on Twig. Please activate TwigBundle for the %s environnement or disable GraphiQL and GraphQL Playground.', $container->getParameter('kernel.environment')));
}
$container->removeDefinition('api_platform.graphql.action.graphiql');
$container->removeDefinition('api_platform.graphql.action.graphql_playground');
}

$container->registerForAutoconfiguration(QueryItemResolverInterface::class)
->addTag('api_platform.graphql.query_resolver');
$container->registerForAutoconfiguration(QueryCollectionResolverInterface::class)
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/Resources/config/graphql.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<service id="api_platform.graphql.action.entrypoint" class="ApiPlatform\GraphQl\Action\EntrypointAction" public="true">
<argument type="service" id="api_platform.graphql.schema_builder" />
<argument type="service" id="api_platform.graphql.executor" />
<argument type="service" id="api_platform.graphql.action.graphiql" />
<argument type="service" id="api_platform.graphql.action.graphql_playground" />
<argument type="service" id="api_platform.graphql.action.graphiql" on-invalid="null"/>
<argument type="service" id="api_platform.graphql.action.graphql_playground" on-invalid="null"/>
<argument type="service" id="serializer" />
<argument type="service" id="api_platform.graphql.error_handler" />
<argument>%kernel.debug%</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Uid\AbstractUid;
Expand Down Expand Up @@ -163,6 +165,7 @@ protected function setUp(): void
'kernel.bundles' => [
'DoctrineBundle' => DoctrineBundle::class,
'SecurityBundle' => SecurityBundle::class,
'TwigBundle' => TwigBundle::class,
],
'kernel.bundles_metadata' => [
'TestBundle' => [
Expand All @@ -173,6 +176,7 @@ protected function setUp(): void
],
'kernel.project_dir' => __DIR__.'/../../../Fixtures/app',
'kernel.debug' => false,
'kernel.environment' => 'test',
]);

$this->container = new ContainerBuilder($containerParameterBag);
Expand Down Expand Up @@ -693,6 +697,37 @@ public function testGraphQlConfiguration(): void
$this->assertServiceHasTags('api_platform.graphql.normalizer.runtime_exception', ['serializer.normalizer']);
}

public function testRuntimeExceptionIsThrownIfTwigIsNotEnabledButGraphqlClientsAre(): void
{
$config = self::DEFAULT_CONFIG;
$config['api_platform']['graphql']['enabled'] = true;
$this->container->getParameterBag()->set('kernel.bundles', [
'DoctrineBundle' => DoctrineBundle::class,
'SecurityBundle' => SecurityBundle::class,
]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('GraphiQL and GraphQL Playground interfaces depend on Twig. Please activate TwigBundle for the test environnement or disable GraphiQL and GraphQL Playground.');

(new ApiPlatformExtension())->load($config, $this->container);
}

public function testGraphqlClientsDefinitionsAreRemovedIfDisabled(): void
{
$config = self::DEFAULT_CONFIG;
$config['api_platform']['graphql']['enabled'] = true;
$config['api_platform']['graphql']['graphiql']['enabled'] = false;
$config['api_platform']['graphql']['graphql_playground']['enabled'] = false;
$this->container->getParameterBag()->set('kernel.bundles', [
'DoctrineBundle' => DoctrineBundle::class,
'SecurityBundle' => SecurityBundle::class,
]);

(new ApiPlatformExtension())->load($config, $this->container);

$this->assertNotContainerHasService('api_platform.graphql.action.graphiql');
$this->assertNotContainerHasService('api_platform.graphql.action.graphql_playground');
}

public function testDoctrineOrmConfiguration(): void
{
$config = self::DEFAULT_CONFIG;
Expand Down

0 comments on commit 0d46cd6

Please sign in to comment.