This commit is contained in:
Your Name
2021-07-26 19:46:18 +02:00
parent e7a49138bb
commit aae17f10a6
818 changed files with 70695 additions and 16408 deletions
@@ -2,15 +2,15 @@
namespace Nuwave\Lighthouse\Subscriptions;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
use Illuminate\Http\Request;
use Nuwave\Lighthouse\GraphQL;
use Symfony\Component\HttpFoundation\Response;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Nuwave\Lighthouse\Subscriptions\Contracts\SubscriptionIterator;
use Nuwave\Lighthouse\Subscriptions\Contracts\AuthorizesSubscriptions;
use Nuwave\Lighthouse\Subscriptions\Contracts\BroadcastsSubscriptions;
use Nuwave\Lighthouse\Subscriptions\Events\BroadcastSubscriptionEvent;
use Nuwave\Lighthouse\Subscriptions\Contracts\StoresSubscriptions;
use Nuwave\Lighthouse\Subscriptions\Contracts\SubscriptionIterator;
use Symfony\Component\HttpFoundation\Response;
class SubscriptionBroadcaster implements BroadcastsSubscriptions
{
@@ -22,17 +22,17 @@ class SubscriptionBroadcaster implements BroadcastsSubscriptions
/**
* @var \Nuwave\Lighthouse\Subscriptions\Contracts\AuthorizesSubscriptions
*/
protected $auth;
protected $subscriptionAuthorizer;
/**
* @var \Nuwave\Lighthouse\Subscriptions\StorageManager
* @var \Nuwave\Lighthouse\Subscriptions\Contracts\StoresSubscriptions
*/
protected $storage;
protected $subscriptionStorage;
/**
* @var \Nuwave\Lighthouse\Subscriptions\Contracts\SubscriptionIterator
*/
protected $iterator;
protected $subscriptionIterator;
/**
* @var \Nuwave\Lighthouse\Subscriptions\BroadcastManager
@@ -40,96 +40,67 @@ class SubscriptionBroadcaster implements BroadcastsSubscriptions
protected $broadcastManager;
/**
* @var \Illuminate\Contracts\Events\Dispatcher
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
protected $eventsDispatcher;
protected $busDispatcher;
/**
* @param \Nuwave\Lighthouse\GraphQL $graphQL
* @param \Nuwave\Lighthouse\Subscriptions\Contracts\AuthorizesSubscriptions $auth
* @param \Nuwave\Lighthouse\Subscriptions\StorageManager $storage
* @param \Nuwave\Lighthouse\Subscriptions\Contracts\SubscriptionIterator $iterator
* @param \Nuwave\Lighthouse\Subscriptions\BroadcastManager $broadcastManager
* @param \Illuminate\Contracts\Events\Dispatcher $eventsDispatcher
* @return void
*/
public function __construct(
GraphQL $graphQL,
AuthorizesSubscriptions $auth,
StorageManager $storage,
SubscriptionIterator $iterator,
AuthorizesSubscriptions $subscriptionAuthorizer,
StoresSubscriptions $subscriptionStorage,
SubscriptionIterator $subscriptionIterator,
BroadcastManager $broadcastManager,
EventsDispatcher $eventsDispatcher
BusDispatcher $busDispatcher
) {
$this->graphQL = $graphQL;
$this->auth = $auth;
$this->storage = $storage;
$this->iterator = $iterator;
$this->subscriptionAuthorizer = $subscriptionAuthorizer;
$this->subscriptionStorage = $subscriptionStorage;
$this->subscriptionIterator = $subscriptionIterator;
$this->broadcastManager = $broadcastManager;
$this->eventsDispatcher = $eventsDispatcher;
$this->busDispatcher = $busDispatcher;
}
/**
* Queue pushing subscription data to subscribers.
*
* @param \Nuwave\Lighthouse\Schema\Types\GraphQLSubscription $subscription
* @param string $fieldName
* @param mixed $root
* @return void
*/
public function queueBroadcast(GraphQLSubscription $subscription, string $fieldName, $root): void
{
$this->eventsDispatcher->dispatch(
new BroadcastSubscriptionEvent($subscription, $fieldName, $root)
);
$broadcastSubscriptionJob = new BroadcastSubscriptionJob($subscription, $fieldName, $root);
$broadcastSubscriptionJob->onQueue(config('lighthouse.subscriptions.broadcasts_queue_name'));
$this->busDispatcher->dispatch($broadcastSubscriptionJob);
}
/**
* Push subscription data to subscribers.
*
* @param \Nuwave\Lighthouse\Schema\Types\GraphQLSubscription $subscription
* @param string $fieldName
* @param mixed $root
* @return void
*/
public function broadcast(GraphQLSubscription $subscription, string $fieldName, $root): void
{
$topic = $subscription->decodeTopic($fieldName, $root);
$subscribers = $this->storage
$subscribers = $this->subscriptionStorage
->subscribersByTopic($topic)
->filter(function (Subscriber $subscriber) use ($subscription, $root): bool {
return $subscription->filter($subscriber, $root);
});
$this->iterator->process(
$this->subscriptionIterator->process(
$subscribers,
function (Subscriber $subscriber) use ($root): void {
$data = $this->graphQL->executeQuery(
$subscriber->root = $root;
$executionResult = $this->graphQL->executeQuery(
$subscriber->query,
$subscriber->context,
$subscriber->args,
$subscriber->setRoot($root),
$subscriber->operationName
$subscriber->variables,
$subscriber
);
$this->broadcastManager->broadcast(
$subscriber,
$data->jsonSerialize()
$this->graphQL->serializable($executionResult)
);
}
);
}
/**
* Authorize the subscription.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function authorize(Request $request): Response
{
return $this->auth->authorize($request)
return $this->subscriptionAuthorizer->authorize($request)
? $this->broadcastManager->authorized($request)
: $this->broadcastManager->unauthorized($request);
}