简体   繁体   中英

Subscriptions do not work, the code in the subscription field class doesn't run

The mutation does not broadcast even though it is defined in the schema, I also added some log messages into the Subscription field class but if I trigger the subscription through the mutation or from artisan tinker like this:

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

nothing happens, no logs, nothing showing up in the pusher dashboard. I use the laravel graphql playground to listen to broadcasted messages like this:

subscription {
  messageSent {
    id
    text
  }
}

every time I connect to the broadcast I get this console error in the browser

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

and this appears in the Pusher dashboard error log - Missing parameter: event. The error appears twice when I connect and once when I disconnect.

I also don't get any subscription channels in the mutation response:

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

This is my 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")
...

this is the MessageSent php class

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"

Solved it myself. I didn't properly configure the front end listener. After doing that, everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM