订阅不起作用,订阅字段中的代码 class 不 运行
Subscriptions do not work, the code in the subscription field class doesn't run
即使在模式中定义了突变,它也不会广播,我还在订阅字段中添加了一些日志消息 class 但是如果我通过突变或像这样从 artisan tinker 触发订阅:
>>> Nuwave\Lighthouse\Execution\Utils\Subscription::broadcast('messageSent', $m);
=> null
没有任何反应,没有日志,推送器仪表板中没有显示任何内容。
我使用 laravel graphql playground 来听
广播消息如下:
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")
...
这是已发送的消息phpclass
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"
自己解决了。我没有正确配置前端侦听器。这样做之后,一切正常。
即使在模式中定义了突变,它也不会广播,我还在订阅字段中添加了一些日志消息 class 但是如果我通过突变或像这样从 artisan tinker 触发订阅:
>>> Nuwave\Lighthouse\Execution\Utils\Subscription::broadcast('messageSent', $m);
=> null
没有任何反应,没有日志,推送器仪表板中没有显示任何内容。 我使用 laravel graphql playground 来听 广播消息如下:
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")
...
这是已发送的消息phpclass
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"
自己解决了。我没有正确配置前端侦听器。这样做之后,一切正常。