nuwave / nuwave/lighthouse

@inject context user returns null with multi-guards

Open
#1,763 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs reproduction
Dominant language
PHP
Stars
3.5k
Forks
468
Avg merge
3h 9m
Merged PRs (30d)
2

Description

My Lighthouse's (v5.0.2) default guard is 'web', but I use an 'admin' guard also.
Default guard context works properly in any case, but when I need to use admin's context – it returns null.
(Whereas admin guard works correctly like a guard: it protects field and mutations as expected).

There is a mutation example:
addNote(text: String!): Note! @create @inject(context: "user.id", name: "admin_id") @guard(with: "admin")

I've tried to create a custom mutator and it possible to get an access to admin's context via
$context->request()->user();
(Whereas for default guard $context->user works also)

But if I have a lot of simple mutations like @create, @update, etc., do I really need to create multiple custom mutator methods in this case instead of just using @inject directive?

And another problem: if I need to give an access to the same mutation to multiple guards, I do need to write it twice (or more) and name differently.

Do I misunderstand something?

Many thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the mutation using @inject(context: "user.id") with the admin guard, then compare its behavior with @guard(with: "admin") and the request user available to a custom mutator. Done means the expected behavior for multiple guards and @inject is established, with the relevant mutation cases covered by tests or clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, laravel, php
Domain
api, authentication
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.