nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 437 forks source link

Inject directive not working in scopes #1091

Open faiverson opened 4 years ago

faiverson commented 4 years ago

f I want to make a pagination (eg. user blog entries) and make sure that the user is the logged user in the system, I’d like to use @inject

entires(
    order: [OrderByClause!] @orderBy,
    filters: Filters @filters(columns: ["id", "name", "display_name", "description"])
  )[Role!]
    @can(ability: "view")
@inject(context: "user.id", name: "id")
@paginate(scopes: ["assignedToUser"])

Then I imagine to use scopes in order to filter the blog entries for that user The problem is that id never goes to scope as an argument (I think it should) because the arguments are set here:

$query = $resolveInfo
                    **->argumentSet**
                    ->enhanceBuilder(
                        $query,
                        $this->directiveArgValue('scopes', [])
                    );

argumentSet is not considering the @inject directive that’s why id is not appending to args, when you call:

`public function assignedToUser(array $args) {
dd($args["id"]); _// it is never goes to here!_
}

Lighthouse Version: latest Laravel Version: latest

faiverson commented 4 years ago

I add that it is not only a problem with @inject, you cannot set any directives to modify $args and then use them in the scope, because the scope is using the args from argumentSet and argumentSet never changes (that's why @inject is not working either)

spawnia commented 4 years ago

We might try to construct the ArgumentSet later, when the $args have been modified. That would require dealing with untyped and dynamic args, though.

kieuminhcanh commented 1 year ago

You should use Auth::user() in scope

public function scopeOwner(Builder $query, array $args)
{
    return $query->where('id', Auth::user()->id);
}