订阅不起作用,订阅字段类中的代码不运行



即使在模式中定义了突变也不会广播,我还在Subscription字段类中添加了一些日志消息,但是如果我通过突变或来自工匠修补器触发订阅,就像这样:

>>> NuwaveLighthouseExecutionUtilsSubscription::broadcast('messageSent', $m);
=> null

什么都没发生,没有日志,也没有显示在仪表盘上。我用laravel图形游乐场来听像这样广播消息:

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)

,这出现在push仪表板错误日志中缺少参数:event。当我连接时出现两次错误,当我断开时出现一次。

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

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

这是我的schema:

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 AppGraphQLSubscriptions;
use IlluminateHttpRequest;
use NuwaveLighthouseSubscriptionsSubscriber;
use NuwaveLighthouseSchemaTypesGraphQLSubscription;
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;
}
}

配置/broadcasting.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"

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

最新更新