-
-
Notifications
You must be signed in to change notification settings - Fork 265
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
Add support for route parameters #1067
base: master
Are you sure you want to change the base?
Conversation
Correction find schema when prefix contains parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the source code change and the description given, I still don't understand the purpose.
Please provide more examples and include a test showcasing the use-case.
Ok, let's imagine I want a dynamic URL parameter at my Graphql entry point. I'll start by setting the prefix at the config level to "graphql/{my_parameter}" like this: return [
...
'route' => [
// The prefix for routes; do NOT use a leading slash!
'prefix' => 'graphql/{my_parameter}',
...
],
...
] The route generated will therefore make it possible to retrieve this parameter in this way in my queries: $myParameter = request()->route()->parameter('my_parameter') This works for the current version's default schema. But in case I have several schemas, a "public" and a "private". The controller tries to detect the schema using if (!Str::startsWith($path, $routePrefix)) {
return null;
} In case the URL is "/graphql/parameter_arbitrary_value/private" // When $path is retrieved using $request->getPathInfo(), the condition become
if (!Str::startsWith('/graphql/parameter_arbitrary_value/private', '/graphql/{my_parameter}')) {
return null; // Always return null and default schema is applied
}
//But, when $path is retrieved using $request->route()->uri(), the condition become
if (!Str::startsWith('/graphql/{my_parameter}/private', '/graphql/{my_parameter}')) {
return null;
}
//Everything works fine |
Thanks for the clarification, that helps a lot! Can you please add a test for the PR showing the change in action? It will also help me figure which approach is the best. Thanks |
Here, 2 tests have been added, one for the default schema and a second for the custom schema. |
I think a better approach would be to declare one and only one graphql route:
And in your controller use $schemaName = $request->route()->parameter('schemaName',$config->get('graphql.default_schema', 'default'));
//Instead of:
$routePrefix = $config->get('graphql.route.prefix', 'graphql');
$schemaName = $this->findSchemaNameInRequest($request, "/$routePrefix") ?: $config->get('graphql.default_schema', 'default'); But this is probably going to be a breaking change, |
💥 😄 I'm not opposed to bump a major version for a breaking change, if the improves are worth it. The routing code has undergone some major change in recent years (if you're invested in this PR, I encourage you to check out the I already had the feeling this won't be an easy PR but now I'm confident it's not. I'm on vacation for a good part of the summer months and I need focus time to wrap my head around this before proceeding. I'll revisit once I've time, but that will likely take months :/ |
src/GraphQLController.php
Outdated
@@ -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(); |
There was a problem hiding this comment.
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).
(I moved the PR in draft mode until we've consensus, I hope this is fine with you) |
Alright I am fine with it. |
To avoid breaking changes, I have reworked the I find this approach more flexible as it allows us to eventually inject other information that can be retrieved in any controller using I also took the opportunity to declare and move the route declaration into the All these changes have been tested, and some tests have been modified to verify the presence of the I have also removed the Looking forward to your feedback on this proposal. I also think it could be interesting in a future major version to define a In this case, for dynamic URLs, it will be possible to retrieve these parameters directly by using something like: public function resolve($root, $args, GraphQLContext $context, ResolveInfo $resolveInfo, Closure $getSelectFields): LengthAwarePaginator
{
$routeParameter = $context->getRouteParameters()['name_of_param'];
$routeParameter = $context->getRouteParameter('name_of_param');
} |
Correction find schema when prefix contains parameters
Summary
I add the ability to set custom parameters to route prefix, like:
This fix use $request->route()->uri() instead of $request->getPathInfo() to fix the schemaName detection to always return null due to Str::startsWith
Type of change:
Checklist:
composer fix-style