Lighthouse GraphQL - 如何在突变解析器中 return current_user?

Lighthouse GraphQL - How to return current_user in mutation resolver?

我有一种情况,我正在编写一个自定义突变解析器,目前必须从我的前端传递 current_user 的 ID,然后才能在 ::find 上执行用户模型。然而,理想的情况是能够使用 current_user 的实例,这样我就不必依赖将 ID 传递到我的 GraphQL 服务器。

总的来说,我对 Laravel 和 GraphQL 的世界还很陌生,但是我一直在阅读 Lighthouse 文档,其中提到了 @auth directive, and other Whosebug answers that mention using auth('api')->user(), however that returns NULL for me. I should also mention that I'm using the lighthouse-graphql-passport-auth 用于处理用户身份验证的库,如果这有什么不同的话。有人知道如何访问 current_user 吗?

public function __invoke($_, array $args)
    {
        // $user = \App\Models\User::find($args['id']); <--- not ideal
        $user = auth('api')->user(); <--- returns NULL
        var_dump($user);

        foreach ($user->notifications as $notification) {
            $notification->viewed = true;
            $notification->save();
        }

        $notifications = $user->notifications->toArray();

        return [
            'status' => 'Success',
            'notifications' => $notifications
        ];
    }

我在 lighthouse-graphql-passport-auth 文档中发现了一个有趣的部分,它讨论了设置 [global middleware][3] 以将登录用户插入到 $context 中。这正是我所需要的,并且在将行添加到文档中提到的 lighthouse.php 配置的中间件部分之后,我能够使用 $context->user() 到 return 当前登录的用户。