Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphQL] Add a clearer error message when TwigBundle is disable but graphql clients aren't #5064

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.3

* Graphql: add a clearer error message when TwigBundle is disabled but graphQL clients are enabled (#5064)

## 3.0.2

* Metadata: generate skolem IRI by default, use `genId: false` to disable **BC**
Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -43,11 +43,11 @@ public function __invoke(Request $request): Response
{
try {
if ($request->isMethod('GET') && 'html' === $request->getRequestFormat()) {
if ('graphiql' === $this->defaultIde && $this->graphiqlEnabled) {
if ('graphiql' === $this->defaultIde && $this->graphiqlEnabled && $this->graphiQlAction) {
return ($this->graphiQlAction)($request);
}

if ('graphql-playground' === $this->defaultIde && $this->graphQlPlaygroundEnabled) {
if ('graphql-playground' === $this->defaultIde && $this->graphQlPlaygroundEnabled && $this->graphQlPlaygroundAction) {
return ($this->graphQlPlaygroundAction)($request);
}
}
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 because PHPStan uses the container of the test env cache and in test the parameter kernel.bundles always contains the key TwigBundle
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