Skip to content
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

[LUMEN] Fix multiple schemas on query/mutation #599

Closed
wants to merge 4 commits into from

Conversation

Vespira
Copy link

@Vespira Vespira commented Mar 4, 2020

Summary

$schema arg received in query function is always null and if we have only one element in $routeParameters, $schema remain empty, then the query always hit default schema.

Type of change

switching condition to fill $schema, from if (count($routeParameters) > 1) to if (count($routeParameters) > 0)

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • Existing tests have been adapted and/or new tests have been added
  • Add a CHANGELOG.md entry
  • Update the README.md
  • Code style has been fixed via composer fix-style

Links

$schema arg received in query function is always null and if we have only one element in $routeParameters, $schema remain empty, then the query always hit default schema.
@mfn mfn changed the title attend issue https://github.com/rebing/graphql-laravel/issues/568 [LUMEN] Fix multiple schemas on query/mutation Mar 4, 2020
@Vespira
Copy link
Author

Vespira commented Mar 5, 2020

it appears to fail at dependency installation with composer, in this travis Job :

PHP: 7.3
LARAVEL='^6.0' INSTALL_LARAVEL=1 INSTALL_LUMEN=1

But the fact is, I didn't change anything in composer.json or composer-lock file ... is it something that happens frequently ?

@mfn
Copy link
Collaborator

mfn commented Mar 6, 2020

is it something that happens frequently ?

Hmm, not frequently but it can…
… still busy and some other PRs have more priority so pleas bear with me here; thx.

@mfn mfn added the Lumen label Mar 7, 2020
@mfn
Copy link
Collaborator

mfn commented Mar 7, 2020

But the fact is, I didn't change anything in composer.json or composer-lock file ... is it something that happens frequently ?

The culprit was that the integration tests use laravel/laravel (no version constraint) and Laravel 7 was just released and the test tried to make the library with only supporting Laravel 6 to work => this has just been fixed in master via #597

Copy link
Collaborator

@mfn mfn left a comment

Choose a reason for hiding this comment

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

I don't think the fix is correct, see my feedback…

@@ -29,7 +29,7 @@ public function query(Request $request, string $schema = null): JsonResponse
// If there are multiple route params we can expect that there
// will be a schema name that has to be built
$routeParameters = $this->getRouteParameters($request);
if (count($routeParameters) > 1) {
if (count($routeParameters) > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think the fix is correct here and should rather be within \Rebing\GraphQL\GraphQLController::getRouteParameters, can you investigate?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think the fix is correct here and should rather be within \Rebing\GraphQL\GraphQLController::getRouteParameters, can you investigate?

Thanks mfn for having a look at my PR :) I don't see what can be fixed in the getRouteParameters as it already handle the Lumen case, and return the only one element in the route param available ..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, so my impression is "if it's broken with Lumen, it need to be fixed in the Lumen code path".

This change here affects all environments == also Laravel, so I'm not confident this is correct (as no one reported problems with Laravel).

Could I explain my position / concerns?

thanks

Copy link
Author

Choose a reason for hiding this comment

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

Ok I get your point.

The situation here is kind of tricky because with Lumen and only one element in the route URL, the schema is never assigned, and the default schema will always be picked.

Could I write something like this instead :

$routeParameters = $this->getRouteParameters($request);
if (count($routeParameters) > Helpers::isLumen() ? 0 : 1) {
        $schema = implode('/', $routeParameters);
}

This fix wouldn't be necessary if we could pass a schema name directly into function arg :
public function query(Request $request, string $schema = null): JsonResponse {}

Copy link
Collaborator

Choose a reason for hiding this comment

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

if (count($routeParameters) > Helpers::isLumen() ? 0 : 1) {

But you can to the same fix within getRouteParameters in a lumen specific code path, no?

This fix wouldn't be necessary if we could pass a schema name directly into function arg :

That would probably require adapting the routes.php => I'm open to that, but would see some concrete code.

Also we would definitely need a test for this.

Copy link
Author

@Vespira Vespira Mar 19, 2020

Choose a reason for hiding this comment

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

Sorry I'm on two parallel tasks, and I'm fairly new to PHP so I may be annoying. But my feeling here is that the count($routeParameters) > 1 is not right, why would we want to take default schema, if there is one path element anyway ? it still seems valid.

EDIT : I took the time to test a Laravel boilerplate project with graphQL, and saw that we receive the exact same thing from the getRouteParameters method. for instance, if I access a ping query on "test" schema, I will in both case (Laravel and Lumen) receive an array of one element : array("test').

So I decided to fix the root problem and send schema name in query. After a bit of code exploring I didnt found yet the place it's specified 😫

I'll focus on this today, and I'll update the PR to reflect that and update tests

Copy link
Author

Choose a reason for hiding this comment

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

Ok I took some time again to look at the code, and I finally found something amazing 😲

Into graphql.php config file, if you replace the academic {graphql_schema?} with {schema} in Lumen, it works so hard that I can't even think about how many hours I lost trying to wrap my head around it. I spotted the good place to log into RoutesRequests.php file from Lumen Framework, and I saw that the pattern given as a config key 'routes' in graphql.php was used to insert a into the parameters the schema with this form (e.g private schema ) :
["private": "private"]

Now if we change the config key to {schema} it will then automatically add into parameters :
["schema": "private"]

Hence, the GraphQLController gets hit with correct schema value.

🗡 🗡 I could easily do Sepukku, if we were 600 years back in the past

Jokes aside, I will close this non-sense PR as it works with Lumen, BUT I will open another PR to update documentation about that.

@Vespira Vespira closed this Mar 30, 2020
@Vespira Vespira mentioned this pull request Apr 3, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multiple schema on lumen not find query or mutation
2 participants