From ef8a95b59360773ec0c1d48c8978d73060d75c8f Mon Sep 17 00:00:00 2001 From: Albert Haff Date: Tue, 19 Jan 2021 18:31:51 +0100 Subject: [PATCH] Add base subscription --- README.md | 34 +++++++++++++++++-- src/Exceptions/NoSubscriptionException.php | 5 +++ .../Directives/SubscribableDirective.php | 13 ++++++- .../FairwayModelEventSubscription.php | 24 +++++++++++++ src/Subscriptions/ModelEventSubscription.php | 20 ----------- src/lighthouse-fairway.php | 5 +++ 6 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 src/Exceptions/NoSubscriptionException.php create mode 100644 src/Subscriptions/FairwayModelEventSubscription.php delete mode 100644 src/Subscriptions/ModelEventSubscription.php diff --git a/README.md b/README.md index e9b2113..b59fa59 100644 --- a/README.md +++ b/README.md @@ -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 +name->value; @@ -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}\") } "); } diff --git a/src/Subscriptions/FairwayModelEventSubscription.php b/src/Subscriptions/FairwayModelEventSubscription.php new file mode 100644 index 0000000..1ba64c0 --- /dev/null +++ b/src/Subscriptions/FairwayModelEventSubscription.php @@ -0,0 +1,24 @@ +args)['events']) { + if (!in_array($root['event'], $subscriber->args['events'])) { + return false; + } + } + + // Custom filtering + return $this->filterSubscription($subscriber, $root); + } +} diff --git a/src/Subscriptions/ModelEventSubscription.php b/src/Subscriptions/ModelEventSubscription.php deleted file mode 100644 index bcd3b52..0000000 --- a/src/Subscriptions/ModelEventSubscription.php +++ /dev/null @@ -1,20 +0,0 @@ - true, + + /** + * The class that will be invoked for subscriptions. + */ + 'subscription_class' => null, ];