vendor/api-platform/core/src/GraphQl/Action/GraphQlPlaygroundAction.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\GraphQl\Action;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Twig\Environment as TwigEnvironment;
  17. /**
  18.  * GraphQL Playground entrypoint.
  19.  *
  20.  * @author Alan Poulain <contact@alanpoulain.eu>
  21.  */
  22. final class GraphQlPlaygroundAction
  23. {
  24.     public function __construct(private readonly TwigEnvironment $twig, private readonly RouterInterface $router, private readonly bool $graphQlPlaygroundEnabled false, private readonly string $title '', private $assetPackage null)
  25.     {
  26.     }
  27.     public function __invoke(Request $request): Response
  28.     {
  29.         if ($this->graphQlPlaygroundEnabled) {
  30.             return new Response($this->twig->render('@ApiPlatform/GraphQlPlayground/index.html.twig', [
  31.                 'title' => $this->title,
  32.                 'graphql_playground_data' => ['entrypoint' => $this->router->generate('api_graphql_entrypoint')],
  33.                 'assetPackage' => $this->assetPackage,
  34.             ]), 200, ['content-type' => 'text/html']);
  35.         }
  36.         throw new BadRequestHttpException('GraphQL Playground is not enabled.');
  37.     }
  38. }