From 50a38c2189d3447ce2788cc41b17f4eb1898a14a Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Wed, 12 Apr 2023 20:55:22 +0200 Subject: [PATCH] Introduce multi-route support Instead of a single route definition, multiple routes can be defined. They "key" is the URI name and necessary for the controller to look up the config for e.g. the endpoint. --- CHANGELOG.md | 4 +++ README.md | 9 ++--- src/GraphiQLController.php | 15 ++++++-- src/graphiql.php | 74 +++++++++++++++++++------------------- src/routes.php | 15 ++++---- views/index.blade.php | 4 +-- 6 files changed, 69 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 893dc41..794fda4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ See [GitHub releases](/~https://github.com/mll-lab/laravel-graphiql/releases). ## Unreleased +### Added + +- Introduce multi-route support + ## v1.2.2 ### Fixed diff --git a/README.md b/README.md index 57629b7..582434f 100644 --- a/README.md +++ b/README.md @@ -97,10 +97,11 @@ Make sure your route includes the `web` middleware group in `config/graphiql.php ```diff 'route' => [ - 'uri' => '/graphiql', - 'name' => 'graphiql', -+ 'middleware' => ['web'] - ] + '/graphiql' => [ + 'name' => 'graphiql', + + 'middleware' => ['web'] + ], + ], ``` ## Local assets diff --git a/src/GraphiQLController.php b/src/GraphiQLController.php index e8028d2..2b897a7 100755 --- a/src/GraphiQLController.php +++ b/src/GraphiQLController.php @@ -2,12 +2,23 @@ namespace MLL\GraphiQL; +use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Contracts\View\View; +use Illuminate\Http\Request; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class GraphiQLController { - public function __invoke(): View + public function __invoke(ConfigRepository $config, Request $request): View { - return view('graphiql::index'); + $path = '/' . $request->path(); + + $routeConfig = $config->get("graphiql.routes.{$path}"); + + if (null === $routeConfig) { + throw new NotFoundHttpException("No graphiql route config found for '{$path}'"); + } + + return view('graphiql::index', ['routeConfig' => $routeConfig]); } } diff --git a/src/graphiql.php b/src/graphiql.php index bc9d4c9..a91c152 100644 --- a/src/graphiql.php +++ b/src/graphiql.php @@ -3,48 +3,50 @@ return [ /* |-------------------------------------------------------------------------- - | Route configuration + | Routes configuration |-------------------------------------------------------------------------- | - | Set the URI at which the GraphiQL UI can be viewed, + | Set the key as URI at which the GraphiQL UI can be viewed, | and add any additional configuration for the route. | - */ - - 'route' => [ - 'uri' => '/graphiql', - 'name' => 'graphiql', - // 'middleware' => ['web'] - // 'prefix' => '', - // 'domain' => 'graphql.' . env('APP_DOMAIN', 'localhost'), - ], - - /* - |-------------------------------------------------------------------------- - | Default GraphQL endpoint - |-------------------------------------------------------------------------- - | - | The default endpoint that the GraphiQL UI is set to. - | It assumes you are running GraphQL on the same domain - | as GraphiQL, but can be set to any URL. + | You can add multiple routes pointing to different GraphQL endpoints. | */ - - 'endpoint' => '/graphql', - - /* - |-------------------------------------------------------------------------- - | Subscription endpoint - |-------------------------------------------------------------------------- - | - | The default subscription endpoint the GraphiQL UI uses to connect to. - | Tries to connect to the `endpoint` value if `null` as ws://{{endpoint}} - | - | Example: `ws://your-endpoint` or `wss://your-endpoint` - | - */ - - 'subscription-endpoint' => env('GRAPHIQL_SUBSCRIPTION_ENDPOINT', null), + 'routes' => [ + '/graphiql' => [ + 'name' => 'graphiql', + // 'middleware' => ['web'] + // 'prefix' => '', + // 'domain' => 'graphql.' . env('APP_DOMAIN', 'localhost'), + + /* + |-------------------------------------------------------------------------- + | Default GraphQL endpoint + |-------------------------------------------------------------------------- + | + | The default endpoint that the GraphiQL UI is set to. + | It assumes you are running GraphQL on the same domain + | as GraphiQL, but can be set to any URL. + | + */ + + 'endpoint' => '/graphql', + + /* + |-------------------------------------------------------------------------- + | Subscription endpoint + |-------------------------------------------------------------------------- + | + | The default subscription endpoint the GraphiQL UI uses to connect to. + | Tries to connect to the `endpoint` value if `null` as ws://{{endpoint}} + | + | Example: `ws://your-endpoint` or `wss://your-endpoint` + | + */ + + 'subscription-endpoint' => env('GRAPHIQL_SUBSCRIPTION_ENDPOINT', null), + ], + ], /* |-------------------------------------------------------------------------- diff --git a/src/routes.php b/src/routes.php index 4993221..3095bbb 100755 --- a/src/routes.php +++ b/src/routes.php @@ -9,11 +9,13 @@ $config = $app->make(ConfigRepository::class); assert($config instanceof ConfigRepository); -$routeConfig = $config->get('graphiql.route'); -if (is_array($routeConfig)) { - /** @var \Illuminate\Contracts\Routing\Registrar|\Laravel\Lumen\Routing\Router $router */ - $router = $app->make('router'); +/** @var \Illuminate\Contracts\Routing\Registrar|\Laravel\Lumen\Routing\Router $router */ +$router = $app->make('router'); +/** @var array $routesConfig */ +$routesConfig = $config->get('graphiql.routes', []); + +foreach ($routesConfig as $routeUri => $routeConfig) { $actions = [ 'as' => $routeConfig['name'] ?? 'graphiql', 'uses' => GraphiQLController::class, @@ -31,8 +33,5 @@ $actions['domain'] = $routeConfig['domain']; } - $router->get( - $routeConfig['uri'] ?? '/graphiql', - $actions - ); + $router->get($routeUri, $actions); } diff --git a/views/index.blade.php b/views/index.blade.php index d16370f..460d79b 100644 --- a/views/index.blade.php +++ b/views/index.blade.php @@ -31,8 +31,8 @@