Skip to content

Commit 149adf7

Browse files
authored
fix(laravel): register graphql routes before catch-all entrypoint (#8248)
Fixes #8128
1 parent ed3d7ae commit 149adf7

2 files changed

Lines changed: 83 additions & 19 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Laravel\Tests;
15+
16+
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17+
use Illuminate\Config\Repository;
18+
use Illuminate\Foundation\Application;
19+
use Illuminate\Foundation\Testing\RefreshDatabase;
20+
use Orchestra\Testbench\Concerns\WithWorkbench;
21+
use Orchestra\Testbench\TestCase;
22+
23+
class GraphQlRoutePrefixTest extends TestCase
24+
{
25+
use ApiTestAssertionsTrait;
26+
use RefreshDatabase;
27+
use WithWorkbench;
28+
29+
/**
30+
* @param Application $app
31+
*/
32+
protected function defineEnvironment($app): void
33+
{
34+
tap($app['config'], static function (Repository $config): void {
35+
$config->set('app.debug', true);
36+
$config->set('api-platform.graphql.enabled', true);
37+
$config->set('api-platform.defaults.route_prefix', '');
38+
});
39+
}
40+
41+
public function testPostGraphQlWithEmptyRoutePrefix(): void
42+
{
43+
$response = $this->postJson('/graphql', ['query' => '{books { edges { node { id }}}}'], ['accept' => ['application/json']]);
44+
$response->assertStatus(200);
45+
$data = $response->json();
46+
$this->assertArrayHasKey('data', $data);
47+
$this->assertArrayNotHasKey('errors', $data);
48+
}
49+
50+
public function testGetGraphQlWithEmptyRoutePrefix(): void
51+
{
52+
$response = $this->get('/graphql?query='.urlencode('{books { edges { node { id }}}}'), ['accept' => ['application/json']]);
53+
$response->assertStatus(200);
54+
$data = $response->json();
55+
$this->assertArrayHasKey('data', $data);
56+
$this->assertArrayNotHasKey('errors', $data);
57+
}
58+
59+
public function testGetGraphiQlWithEmptyRoutePrefix(): void
60+
{
61+
$response = $this->get('/graphiql');
62+
$response->assertStatus(200);
63+
}
64+
}

src/Laravel/routes/api.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@
7979
$prefix = config()->get('api-platform.defaults.route_prefix', '');
8080

8181
Route::group(['prefix' => $prefix], static function (): void {
82+
if (config()->get('api-platform.graphql.enabled')) {
83+
Route::group([
84+
'middleware' => config()->get('api-platform.graphql.middleware', []),
85+
], static function (): void {
86+
Route::addRoute(['POST', 'GET'], '/graphql', GraphQlEntrypointController::class)
87+
->name('api_graphql');
88+
});
89+
90+
if (config()->get('api-platform.graphiql.enabled', true)) {
91+
Route::group([
92+
'middleware' => config()->get('api-platform.graphiql.middleware', []),
93+
'domain' => config()->get('api-platform.graphiql.domain', ''),
94+
], static function (): void {
95+
Route::get('/graphiql', GraphiQlController::class)
96+
->name('api_graphiql');
97+
});
98+
}
99+
}
100+
82101
Route::group(['middleware' => ApiPlatformMiddleware::class], static function (): void {
83102
Route::get('/contexts/{shortName?}{_format?}', static function (Request $request, ContextAction $contextAction, string $shortName = 'Entrypoint') {
84103
return $contextAction($shortName, $request);
@@ -98,25 +117,6 @@
98117
->where('index', 'index')
99118
->name('api_entrypoint');
100119
});
101-
102-
if (config()->get('api-platform.graphql.enabled')) {
103-
Route::group([
104-
'middleware' => config()->get('api-platform.graphql.middleware', []),
105-
], static function (): void {
106-
Route::addRoute(['POST', 'GET'], '/graphql', GraphQlEntrypointController::class)
107-
->name('api_graphql');
108-
});
109-
110-
if (config()->get('api-platform.graphiql.enabled', true)) {
111-
Route::group([
112-
'middleware' => config()->get('api-platform.graphiql.middleware', []),
113-
'domain' => config()->get('api-platform.graphiql.domain', ''),
114-
], static function (): void {
115-
Route::get('/graphiql', GraphiQlController::class)
116-
->name('api_graphiql');
117-
});
118-
}
119-
}
120120
});
121121
});
122122

0 commit comments

Comments
 (0)