1

即使在模式中定义了突变,它也不会广播,我还在 Subscription 字段类中添加了一些日志消息,但是如果我通过突变或从工匠修补程序触发订阅,如下所示:

>>> Nuwave\Lighthouse\Execution\Utils\Subscription::broadcast('messageSent', $m);
=> null

什么都没有发生,没有日志,推送器仪表板中没有任何显示。我使用 laravel graphql 游乐场来收听广播消息,如下所示:

subscription {
  messageSent {
    id
    text
  }
}

每次我连接到广播时,我都会在浏览器中收到此控制台错误

client.js:547 Uncaught Error: Invalid message type!
    at e.processReceivedData (client.js:547)
    at WebSocket.client.onmessage (client.js:485)

这出现在 Pusher 仪表板错误日志中 - 缺少参数:事件。连接时出现两次错误,断开连接时出现一次。

我也没有在突变响应中获得任何订阅频道:

...
"extensions": {
    "lighthouse_subscriptions": {
      "version": 1,
      "channel": null,
      "channels": []
    }
  }

这是我的架构:

type Subscription {
    messageSent: Message! 
        @subscription(class: "App\\GraphQL\\Subscriptions\\MessageSent")
}

extend type Mutation {
    sendMessage(input: SendMessageInput! @spread): Message
        @guard
        @inject(context: "user.id", name: "sender_id")
        @create
        @broadcast(subscription: "messageSent")
...

这是 MessageSent php 类

namespace App\GraphQL\Subscriptions;

use Illuminate\Http\Request;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;

class MessageSent extends GraphQLSubscription
{
    public function authorize(Subscriber $subscriber, Request $request): bool
    {
        info("authorize");
        return true;
    }

    public function filter(Subscriber $subscriber, $root): bool
    {
        info("filter");
        return true;

    }
}

配置/广播.php

'connections' => [
     'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => true,
            ],
        ],
...

.env

BROADCAST_DRIVER=pusher # also tried "redis" instead of "pusher"

LIGHTHOUSE_BROADCASTER=pusher
LIGHTHOUSE_SUBSCRIPTION_VERSION=1
LIGHTHOUSE_SUBSCRIPTION_EXCLUDE_EMPTY=false
LIGHTHOUSE_QUEUE_BROADCASTS=false

GRAPHQL_PLAYGROUND_SUBSCRIPTION_ENDPOINT="wss://ws-${PUSHER_APP_CLUSTER}.pusher.com:443/app/${PUSHER_APP_KEY}?protocol=5"
4

1 回答 1

0

自己解决了。我没有正确配置前端监听器。这样做之后,一切正常。

于 2021-11-19T12:01:02.217 回答