浮标/灯塔航道

将航道合规性添加到Lighthouse GraphQL服务器(Laravel版)

v0.2.0 2023-11-15 10:21 UTC

This package is auto-updated.

Last update: 2024-09-15 11:56:55 UTC


README

此扩展为Fairway兼容性添加到Lighthouse,使其与Buoy-client完全兼容。

安装

composer require buoy/lighthouse-fairway

然后发布配置。

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

然后创建一个类来处理您的订阅的授权和过滤。

<?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;
    }
}

最后,在lighthouse-fairway.php配置文件中输入您的订阅类名空间。

用法

此库添加了一些使模型可订阅的快捷方式。

可订阅指令

此指令应用于模型类型。在本例中,我们假设模型App\Models\Note存在。

type Note @subscribable {
    id
    text
}

应用@subscribable指令将自动向您的模式添加以下内容

enum EventType {
    CREATE
    UPDATE
    DELETE
}

type NoteEvent {
    "ID of the model"
    id: ID!
    "Type of the event"
    event: EventType!
    "The model that has been modified"
    model: Note!
}

type Subscription {
    noteModified(
        "Limit the subscription to a specific model"
        id: ID,
        "Limit the subscription to specific events"
        events: [EventType!]
    ): NoteEvent
}

如果需要,@subscribable指令也可以使用自定义订阅类

type Note @subscribable(class: "\\\\App\\\\GraphQL\\\\Subscriptions\\\\MyCustomSubscription") {
    id
    text
}

只需确保它返回符合生成的模式的数据。建议扩展Buoy\LighthouseFairway\Subscriptions\FairwayModelEventSubscription以维护事件类型过滤。

分发事件

事件通过Broadcast实用程序分发。只需提供模型和事件类型,事件将被广播到所有授权订阅者。

$note = App\Models\Note::first();
Buoy\LighthouseFairway\Util\Broadcast::modelEvent($note, 'update');