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

Execution middleware #762

Merged
merged 53 commits into from
May 11, 2021
Merged

Execution middleware #762

merged 53 commits into from
May 11, 2021

Conversation

crissi
Copy link
Contributor

@crissi crissi commented May 3, 2021

Summary

Execution hook/middleware based on #753

TODO

  • convert current middleware like functions to use this instead
  • docs
  • test for batched requests
  • document breaking changes in more detail
  • test per-schema execution middleware

Type of change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Misc. change (internal, infrastructure, maintenance, etc.)

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

@crissi
Copy link
Contributor Author

crissi commented May 3, 2021

@mfn can you take a look when you have some time!

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 like 👍

Not sure were we end up with this, but I see this as a promising start! Also: so little code 😱

I think we should try hard with the actual data we pass and I fully accept further breaking changes for the upcoming 8 release cycle; it's the best time now.

Feel free to react to the feedback already, but I definitely need more time to play around with this. Now that we've something working, it's really best probably to try / see implement:

  • APC
  • detect_unused_variables

to better understand what is / what is not possible and what's missing etc.

Thanks for the initiative 🙏

src/GraphQL.php Outdated
Comment on lines 128 to 130
if (1 === count($middlewareResponse)) {
return $middlewareResponse;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this doing exactly? Is it for the case of already returning a response?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this catches, if you returning directly in the middleware.

After thinking about it this might not be enough to know for sure that it was a response that has been returned, will investigate further

src/GraphQL.php Outdated
@@ -118,6 +119,17 @@ public function schema($schema = null): Schema
*/
public function query($query, ?array $variables = [], array $opts = []): array
{
$middlewareResponse = app()->make(Pipeline::class)
->send([$query, $variables])
Copy link
Collaborator

@mfn mfn May 3, 2021

Choose a reason for hiding this comment

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

I think we should add $opts here too.

Or, well at least the most important bit would be the schemaName.

TBH I despise the whole signature of this query method, it just does not make any sense to me.

I think the one from \Rebing\GraphQL\GraphQLController::executeQuery makes much more sense:

executeQuery(string $schemaName, OperationParams $params):

with the addition of being able to carry over the parsed query (either freshly parsed or retrieved e.g. from APQ).

src/GraphQL.php Outdated
*/
public function executionMiddleware(): array
{
return config('graphql.execution_middleware', []);
Copy link
Collaborator

@mfn mfn May 3, 2021

Choose a reason for hiding this comment

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

I think it's important we consider the schema name / per schema execution middleware too (*), something like:

return
    config("graphql.$schemaName.execution_middleware") ??
    config('graphql.execution_middleware') ??
    [];

(*) which isn't in this context yet, I know

src/Support/ExecutionMiddleware.php Outdated Show resolved Hide resolved
@mfn
Copy link
Collaborator

mfn commented May 3, 2021

Btw, why are tests not running? I know there were some recent GHA changes, but you're not a first time contributor and I also don't see a button for approval of the requests:
image

Or are tests on public repos in draft mode not executed anymore? 🤔

@mfn
Copy link
Collaborator

mfn commented May 3, 2021

Just thinking out loud, no need to do anything right now:

No matter how we name this middleware, we probably should rename \Rebing\GraphQL\Support\Middleware into ResolverMiddleware. I wonder what the guide of "where to place things" should be. It would not be unlikely a project will use a mix of resolver and execution middlewares; and then where should we but them? Should we stick to \GraphQL\Middleware

return $rootNamespace . '\GraphQL\Middleware';

for both "kinds" of middlewares?

Maybe moving both to \Rebing\GraphQL\Middleware\ would be a good idea (ResolverMiddleware and ExecutionMiddleware)

@crissi
Copy link
Contributor Author

crissi commented May 3, 2021

Feel free to react to the feedback already, but I definitely need more time to play around with this. Now that we've something working, it's really best probably to try / see implement:

  • APC
  • detect_unused_variables

to better understand what is / what is not possible and what's missing etc.

I will try to move them to the middleware

@mfn

This comment has been minimized.

@mfn

This comment has been minimized.

mfn added 3 commits May 4, 2021 20:48
The $opts[schema] supports ad-hoc schemas, i.e. not just a name
referencing the config, thus the boilerplate.

I'm wonder if this is truly needed and playing with the idea to
actually remove support for this.
I don't see it useful being publicly available, it's more of an
internal helper which can be overriden if necessary.
@crissi
Copy link
Contributor Author

crissi commented May 4, 2021

I tried to move the apc part but got into problems with the queryContext missing a query string, to fix this the query context needs to be added to the Graphql class instead of the controller. But maybe we can let the apc be at is!?

@crissi
Copy link
Contributor Author

crissi commented May 8, 2021

All good man, the feature is the most important thing for me😊

Like it alot that everything is a brick that you can choose to add or remove.
Will try to get some time to play with it next week!

@mfn mfn marked this pull request as ready for review May 9, 2021 19:54
mfn added 2 commits May 10, 2021 08:10
Some of the previous breaking changes for the next major release have
become obsolete due to the further refactoring.
CHANGELOG.md Outdated
@@ -94,6 +94,7 @@ CHANGELOG
new: `public function queryAndReturnResult($query, ?array $variables = [], array $opts = []): ExecutionResult`

### Added
- The primary execution of the GraphQL requested is now modeled via middlewares [\#762 / crissi and mfn](https://github.com/rebing/graphql-laravel/pull/762)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@crissi I don't want it to go unmentioned I also mentioned me as secondary author, is this fine with you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all good 🙂

@crissi
Copy link
Contributor Author

crissi commented May 10, 2021

Getting "Type default not found". Checked the config, but looks similar.

image

I am firing a query from graphiql UI. Are you getting the same?

@mfn
Copy link
Collaborator

mfn commented May 10, 2021

Getting "Type default not found". Checked the config, but looks similar.

I've recently changed the behaviour that invalid schemas are not silently ignored anymore when building the routes => #766

  • Is your config graphql.default_schema set to default?
  • Do you have a graphql.schemas.default?

image

That would be my first idea…

@mfn
Copy link
Collaborator

mfn commented May 10, 2021

I ran it on a private project with ~5k GraphQL tests, multiple schemas and custom context with no issues (except adapting the config to inject the context via middleware now)

@crissi
Copy link
Contributor Author

crissi commented May 11, 2021

Getting "Type default not found". Checked the config, but looks similar.

I've recently changed the behaviour that invalid schemas are not silently ignored anymore when building the routes => #766

  • Is your config graphql.default_schema set to default?
  • Do you have a graphql.schemas.default?

image

That would be my first idea…

thanks , error 40:)

@crissi
Copy link
Contributor Author

crissi commented May 11, 2021

do we need a getter and setter on OperationParams for the variables? now there is only the public variables

@mfn
Copy link
Collaborator

mfn commented May 11, 2021

do we need a getter and setter on OperationParams for the variables? now there is only the public variables

I wanted to stick to and follow \GraphQL\Server\OperationParams which works that way.

I'm not really happy how \Rebing\GraphQL\Support\OperationParams turns out regarding the decoration and might revisit this, but otherwise no real plans here.

thanks , error 40:)

Do you mean it works for you?

@crissi
Copy link
Contributor Author

crissi commented May 11, 2021

Yes works fine😊

@mfn
Copy link
Collaborator

mfn commented May 11, 2021

Ok, I think I'll go ahead and merge this then later today.

I still want to work on a better handling of the three middlewares now supported (http laravel, execution, resolve) but I'll do this in a follow-up PRs

@mfn mfn merged commit 85a9f71 into rebing:master May 11, 2021
@mfn mfn added this to the 8.0.0 milestone May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants