-
Notifications
You must be signed in to change notification settings - Fork 12
/
GraphiQLController.php
executable file
·38 lines (31 loc) · 1.28 KB
/
GraphiQLController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php declare(strict_types=1);
namespace MLL\GraphiQL;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class GraphiQLController
{
public function __construct(
protected ViewFactory $viewFactory,
) {}
public function __invoke(ConfigRepository $config, Request $request): View
{
// Handle /, /graphiql or graphiql
$path = '/' . trim($request->path(), '/');
$routeConfig = $config->get("graphiql.routes.{$path}")
?? throw new NotFoundHttpException("No graphiql route config found for '{$path}'.");
assert(is_array($routeConfig));
return $this->viewFactory->make('graphiql::index', [
'url' => $this->maybeURL($routeConfig['endpoint'] ?? null),
'subscriptionUrl' => $this->maybeURL($routeConfig['subscription-endpoint'] ?? null),
]);
}
protected function maybeURL(?string $endpoint): ?string
{
return is_string($endpoint) && filter_var($endpoint, FILTER_VALIDATE_URL)
? url($endpoint) // Avoid injecting UrlGenerator for Lumen compatibility
: $endpoint;
}
}