订阅不起作用,订阅字段类中的代码不运行
Posted
技术标签:
【中文标题】订阅不起作用,订阅字段类中的代码不运行【英文标题】:Subscriptions do not work, the code in the subscription field class doesn't run 【发布时间】:2021-11-30 05:09:47 【问题描述】:即使在模式中定义了突变,它也不会广播,我还在 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;
config/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"
【问题讨论】:
一个月后有没有找到解决办法? 【参考方案1】:自己解决了。我没有正确配置前端监听器。之后,一切正常。
【讨论】:
以上是关于订阅不起作用,订阅字段类中的代码不运行的主要内容,如果未能解决你的问题,请参考以下文章