-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
Run middleware on a per-field basis #395
Changes from 5 commits
0392663
2500bb7
55bbb06
9b2db47
a04e735
4cdc829
93cdeea
6cf4a15
585ee5a
7908482
61bb0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,43 @@ | |
|
||
namespace Nuwave\Lighthouse\Schema\Directives\Fields; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Router; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use Nuwave\Lighthouse\Support\Pipeline; | ||
use Illuminate\Routing\MiddlewareNameResolver; | ||
use Nuwave\Lighthouse\Schema\Values\FieldValue; | ||
use Nuwave\Lighthouse\Schema\MiddlewareRegistry; | ||
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; | ||
use Nuwave\Lighthouse\Support\Contracts\CreatesContext; | ||
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; | ||
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; | ||
|
||
class MiddlewareDirective extends BaseDirective implements FieldMiddleware | ||
{ | ||
/** @var string todo remove as soon as name() is static itself */ | ||
const NAME = 'middleware'; | ||
|
||
/** @var Pipeline */ | ||
protected $pipeline; | ||
/** @var CreatesContext */ | ||
protected $createsContext; | ||
|
||
/** | ||
* @param Pipeline $pipeline | ||
* @param CreatesContext $createsContext | ||
*/ | ||
public function __construct(Pipeline $pipeline, CreatesContext $createsContext) | ||
{ | ||
$this->pipeline = $pipeline; | ||
$this->createsContext = $createsContext; | ||
} | ||
|
||
/** | ||
* Name of the directive. | ||
* | ||
* @return string | ||
*/ | ||
public function name() | ||
public function name(): string | ||
{ | ||
return 'middleware'; | ||
} | ||
|
@@ -27,52 +51,49 @@ public function name() | |
* | ||
* @return FieldValue | ||
*/ | ||
public function handleField(FieldValue $value, \Closure $next) | ||
public function handleField(FieldValue $value, \Closure $next): FieldValue | ||
{ | ||
$checks = $this->getChecks($value); | ||
|
||
if ($checks) { | ||
$middlewareRegistry = resolve(MiddlewareRegistry::class); | ||
|
||
if ('Query' === $value->getNodeName()) { | ||
$middlewareRegistry->registerQuery( | ||
$value->getFieldName(), | ||
$checks | ||
); | ||
} elseif ('Mutation' === $value->getNodeName()) { | ||
$middlewareRegistry->registerMutation( | ||
$value->getFieldName(), | ||
$checks | ||
); | ||
} | ||
} | ||
|
||
return $next($value); | ||
} | ||
|
||
/** | ||
* Get middleware checks. | ||
* | ||
* @param FieldValue $value | ||
* | ||
* @return array|null | ||
*/ | ||
protected function getChecks(FieldValue $value) | ||
{ | ||
if (! in_array($value->getNodeName(), ['Mutation', 'Query'])) { | ||
return null; | ||
} | ||
|
||
$checks = $this->directiveArgValue('checks'); | ||
/** @var Router $router */ | ||
$router = resolve('router'); | ||
$middleware = $router->getMiddleware(); | ||
$middlewareGroups = $router->getMiddlewareGroups(); | ||
|
||
if (! $checks) { | ||
return null; | ||
} | ||
$middleware = collect($this->directiveArgValue('checks')) | ||
->map(function ($name) use ($middleware, $middlewareGroups){ | ||
return (array) MiddlewareNameResolver::resolve($name, $middleware, $middlewareGroups); | ||
}) | ||
->flatten(); | ||
|
||
if (is_string($checks)) { | ||
$checks = [$checks]; | ||
} | ||
$resolver = $value->getResolver(); | ||
|
||
return $checks; | ||
return $next( | ||
$value->setResolver( | ||
function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $middleware) { | ||
dump($resolveInfo->fieldName, $middleware); | ||
return $this->pipeline | ||
->send( | ||
Request::createFrom( | ||
$context->request() | ||
) | ||
// Duplicate the request so we have independent lifecycles for the fields | ||
// $context->request() | ||
// ->instance() | ||
// ->duplicate() | ||
) | ||
->through( | ||
$middleware | ||
) | ||
->then(function (Request $request) use ($resolver, $root, $args, $resolveInfo){ | ||
dump(data_get($request,'foo')); | ||
return $resolver( | ||
$root, | ||
$args, | ||
$this->createsContext->generate($request), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the context regenerated here and not passed through from line 75? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might not be necessary anymore. It might be there because middleware is often used for authentication, so we have to make sure the If the regeneration is not necessary, i would like to see that covered with a test, then we can remove it. Open to PRs. |
||
$resolveInfo | ||
); | ||
}); | ||
} | ||
) | ||
); | ||
} | ||
} |
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.
Note: These dumps break json output (workaround: use dump-server)
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.
Thanks for the review, this is still a WIP, i will remove the dumps once it is finished. Any opinions on the rest?
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.
I checked this out to benchmark and test it, unfortunately my relatively unperformant schema does not work with latest master or this branch, so I'm still figuring out whats causing the issues.
I will give some more feedback later