Skip to content

Commit

Permalink
Add base subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
alberthaff committed Jan 19, 2021
1 parent 14eec41 commit ef8a95b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 24 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,42 @@ This extension brings [Fairway](https://github.com/buoy-graphql/fairway-spec)-co
composer require buoy/lighthouse-fairway
```

## Configuration

Lighthouse Fairway has a configuration file that can be published:
Then publish the configuration.

```shell
php artisan vendor:publish --tag=lighthouse-fairway-config
```

Then create the class that will handle authorization and filtering for your subscriptions:

```php
<?php

namespace App\GraphQL\Subscriptions;

use Buoy\LighthouseFairway\Subscriptions\FairwayModelEventSubscription;
use Illuminate\Http\Request;
use Nuwave\Lighthouse\Subscriptions\Subscriber;

class FairwayModelSubscription extends FairwayModelEventSubscription
{
public function authorize(Subscriber $subscriber, Request $request): bool
{
// Authorize the user
return true;
}

public function filterSubscription(Subscriber $subscriber, $root): bool
{
// Add filtering here. Filtering based on event type is handled for you.
return true;
}
}
```

Lastly, enter the namespace for your subscription-class in the `lighthouse-fairway.php` config-file


## Usage

This library adds some shortcuts to making models subscribable.
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/NoSubscriptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Buoy\LighthouseFairway\Exceptions;

class NoSubscriptionException extends \Exception { }
13 changes: 12 additions & 1 deletion src/Schema/Directives/SubscribableDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Buoy\LighthouseFairway\Schema\Directives;

use Buoy\LighthouseFairway\Exceptions\NoSubscriptionException;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use Illuminate\Support\Str;
Expand All @@ -21,6 +22,9 @@ public static function definition(): string
SDL;
}

/**
* @throws NoSubscriptionException
*/
public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefinitionNode &$typeDefinition): void
{
$model = $typeDefinition->name->value;
Expand All @@ -35,11 +39,18 @@ public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefiniti
");

$subscription = Str::camel(class_basename($model)) . 'Modified';
$subscriptionClass = '\\'.config('lighthouse-fairway.subscription_class');

if (!class_exists($subscriptionClass)) {
throw new NoSubscriptionException('"subscription_class" in the lighthouse-fairway config must be a valid namespace.');
}

$subscriptionClass = str_replace('\\', '\\\\', $subscriptionClass);

$documentAST->types["Subscription"] = Parser::objectTypeDefinition(/** @lang GraphQL */"
type Subscription {
{$subscription}(events: [EventType!], id: ID): {$model}Event
@subscription(class: \"\\\\Buoy\\\\LighthouseFairway\\\\Subscriptions\\\\ModelEventSubscription\")
@subscription(class: \"{$subscriptionClass}\")
}
");
}
Expand Down
24 changes: 24 additions & 0 deletions src/Subscriptions/FairwayModelEventSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Buoy\LighthouseFairway\Subscriptions;

use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Nuwave\Lighthouse\Subscriptions\Subscriber;

abstract class FairwayModelEventSubscription extends GraphQLSubscription
{
abstract public function filterSubscription(Subscriber $subscriber, $root): bool;

public function filter(Subscriber $subscriber, $root): bool
{
// Filter based on event type
if (optional($subscriber->args)['events']) {
if (!in_array($root['event'], $subscriber->args['events'])) {
return false;
}
}

// Custom filtering
return $this->filterSubscription($subscriber, $root);
}
}
20 changes: 0 additions & 20 deletions src/Subscriptions/ModelEventSubscription.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/lighthouse-fairway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
* Automatically add Fairway client directives to the schema?
*/
'add_client_directives' => true,

/**
* The class that will be invoked for subscriptions.
*/
'subscription_class' => null,
];

0 comments on commit ef8a95b

Please sign in to comment.