lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Inject directive not working in scopes

Open faiverson opened this issue 6 years ago • 3 comments

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 avatar Dec 20 '19 04:12 faiverson

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)

faiverson avatar Jan 08 '20 20:01 faiverson

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

spawnia avatar Jan 08 '20 21:01 spawnia

You should use Auth::user() in scope

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

kieuminhcanh avatar Jan 03 '23 01:01 kieuminhcanh