Lighthouse: GraphQLController@query trigger a user error when not auth

Created on 5 Sep 2018  路  7Comments  路  Source: nuwave/lighthouse

Describe the bug

GraphQLController@query trigger a user error when not auth

Output/Logs

Argument 2 passed to Nuwave\\Lighthouse\\Schema\\Context::__construct() must be an instance of Illuminate\\Foundation\\Auth\\User, null given, called in /workspace/Php/lighthouse/src/Support/Http/Controllers/GraphQLController.php on line 45"

Environment

Lighthouse Version: master
Laravel Version: 5.6
PHP Version: 7.1.20

The GraphQLController@query function:

    public function query(Request $request)
    {
        $query = $request->input('query');
        $variables = $request->input('variables');

        if (is_string($variables)) {
            $variables = json_decode($variables, true);
        }
        return response(
            graphql()->execute(
                $query,
                #look here!!!!!
                new Context($request, app('auth')->user()),
                $variables
            )
        );
    }

282

All 7 comments

Have a fix for that lined up, did not consider unauthenticated routes. Hang tight, will push the fix in a few hours

Environment

Lighthouse Version: master
Lumen Version: 5.6
PHP Version: 7.1

Hey guys - noticed this issue too yesterday so thanks for fixing so quickly! However I'm now getting a similar error with a User being passed to Context -

Argument 2 passed to Nuwave\Lighthouse\Schema\Context::__construct() must be an instance of Illuminate\Foundation\Auth\User or null, instance of App\Http\Users\Models\User given

It looks like my actual User model is somehow being passed instead of Auth::user() - my AuthServiceProvider.php validates a JWT token and returns a User:

return User::where('id', $credentials->user->id)->first();

Can you see if I'm doing something weird/wrong here?!

your User Model must extend of Illuminate\Foundation\Auth\User

the code look like:
```php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use Notifiable,HasApiTokens;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

Thanks for the help! Am I right in thinking that would work just for Laravel - I'm using Lumen which doesn't have Illuminate\Foundation\Auth\User so will have a look to see what I can do instead.

It does not? What does the Auth::user() facade return then?

In Lumen it returns the Authenticatable contract - same as Laravel does, but the Lumen framework doesn't provide an Illuminate\Foundation\Auth\User model to extend.
This doesn't look like it should be a problem, as my User model implements Illuminate\Contracts\Auth\Authenticatable in the same way.

Would it be feasible to typehint Illuminate\Contracts\Auth\Authenticatable instead of Illuminate\Foundation\Auth\User in Context.php? eg:

```

namespace Nuwave\Lighthouse\Schema;

use Illuminate\Http\Request;
use Illuminate\Contracts\AuthAuthenticatable as User;

class Context
{
...
```

Just tested this in my Lumen app and it solved it for me - can submit a pull request if alright with you!

I would be fine with that, go ahead.

Was this page helpful?
0 / 5 - 0 ratings