Skip to content

Add support for route parameters #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/GraphQLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function createBatchingNotSupportedResponse(array $input): array

protected function findSchemaNameInRequest(Request $request, string $routePrefix): ?string
{
$path = $request->getPathInfo();
$path = "/" . $request->route()->uri();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One side effect here I noticed that in \Rebing\GraphQL\Tests\Unit\EmptyRoutePrefixTest::testEmptyRoutePrefix the $path will turn into // (the test didn't break, I just noticed it and we should avoid this).

But this is without considering your other suggestion.

Feel free to go wild on the routing code and propose further commits with follow-up changes, as long as everything is covered with tests (they help me immensely figuring out where to go with this).


if (!Str::startsWith($path, $routePrefix)) {
return null;
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/EndpointParamsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Unit;

use Illuminate\Contracts\Config\Repository;
use Rebing\GraphQL\GraphQLController;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Tests\TestCase;

class EndpointParamsTest extends TestCase
{
public function testGetDefaultSchemaWithRouteParameter(): void
{
$response = $this->call('GET', '/graphql/arbitrary_param', [
'query' => $this->queries['examples'],
]);

self::assertEquals(200, $response->getStatusCode());

$content = $response->getData(true);
self::assertArrayHasKey('data', $content);
self::assertEquals($content['data'], [
'examples' => $this->data,
]);
}
public function testGetCustomSchemaWithRouteParameter(): void
{
$response = $this->call('GET', '/graphql/arbitrary_param/custom', [
'query' => $this->queries['examplesCustom'],
]);
self::assertEquals(200, $response->getStatusCode());
$content = $response->getData(true);
self::assertArrayHasKey('data', $content);
self::assertEquals($content['data'], [
'examplesCustom' => $this->data,
]);
}

protected function getEnvironmentSetUp($app): void
{
parent::getEnvironmentSetUp($app);
$app['config']->set('graphql.route.prefix', 'graphql/{parameter}');
}

}