Skip to content

Commit

Permalink
Introduce multi-route support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mfn committed Apr 13, 2023
1 parent 0c20cd7 commit 50a38c2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/GraphiQLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
74 changes: 38 additions & 36 deletions src/graphiql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
],

/*
|--------------------------------------------------------------------------
Expand Down
15 changes: 7 additions & 8 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{name?: string, middleware?: string, prefix?: string, domain?: string}> $routesConfig */
$routesConfig = $config->get('graphiql.routes', []);

foreach ($routesConfig as $routeUri => $routeConfig) {
$actions = [
'as' => $routeConfig['name'] ?? 'graphiql',
'uses' => GraphiQLController::class,
Expand All @@ -31,8 +33,5 @@
$actions['domain'] = $routeConfig['domain'];
}

$router->get(
$routeConfig['uri'] ?? '/graphiql',
$actions
);
$router->get($routeUri, $actions);
}
4 changes: 2 additions & 2 deletions views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<script src="{{ \MLL\GraphiQL\DownloadAssetsCommand::explorerPluginPath() }}"></script>
<script>
const fetcher = GraphiQL.createFetcher({
url: '{{ filter_var($endpoint = config('graphiql.endpoint'), FILTER_VALIDATE_URL) ? url($endpoint) : $endpoint }}',
subscriptionUrl: '{{ config('graphiql.subscription-endpoint') }}',
url: '{{ filter_var($endpoint = $routeConfig['endpoint'] ?? null, FILTER_VALIDATE_URL) ? url($endpoint) : $endpoint }}',
subscriptionUrl: '{{ $routeConfig['subscription-endpoint'] ?? null }}',
});
function GraphiQLWithExplorer() {
Expand Down

0 comments on commit 50a38c2

Please sign in to comment.