【问题标题】:What is the Lighthouse GraphQL subscriptions Best Practice什么是 Lighthouse GraphQL 订阅最佳实践
【发布时间】:2020-02-04 08:53:32
【问题描述】:

我已尝试在 2 周内找到 Lighthouse 订阅的最佳做法。

但我直到现在才找到最佳实践。

这是我的 shema.graphql

type Subscription {
    rsvHolidaySub(id: ID): Rsv_holiday  }

type Mutation {
    updateHoliday(id: ID!, holiday_type: Int!): Rsv_holiday @update
        @broadcast(subscription: "rsvHolidaySub") }

type Rsv_holiday {
    id: ID!
    holiday_type: Int!
    start_time: String!
    end_time: String! }

当我查询突变updateHoliday 时,如下所示

希望RsvHolidaySub解析函数是从@broadcast(subscription: "rsvHolidaySub")调用的

所以我可以这样广播

class RsvHolidaySub extends GraphQLSubscription
{
    public function __construct() {}
    public function authorize(Subscriber $subscriber, Request $request): bool {}
    public function filter(Subscriber $subscriber, $root): bool {}    
    public function encodeTopic(Subscriber $subscriber, string $fieldName): string {}
    public function decodeTopic(string $fieldName, $root): string {}

    public function resolve($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Rsv_holiday
    {
        // I wanna call broadcast in here!!!!!!
        broadcast(new SomeEvent);

        return $root;
    }
}

我不知道这是订阅的最佳做法。但我认为这是非常简单和干净的。

但即使我使用 redis + laravel-echo-server 或 Beyondcode/laravel-websockets 安装了 websocket, 未调用 resolve 函数。

所以我怀疑这是否可能。

我真的很想知道 Lighthouse 订阅的最佳做法。请分享您的知识。

【问题讨论】:

    标签: subscription laravel-lighthouse


    【解决方案1】:

    我触发了我所有的订阅from code

    但我也不使用那些 @create or @update 指令,而只是一种字段,所以我在代码中做我想做的所有事情,然后即使使用任何其他数据也触发订阅......对我来说很好.

    但我不知道什么是“最佳实践”。

    【讨论】:

    • 感谢您的评论。实际上这个时间,我可以使用 >create/update 指令进行订阅。但我还有 1 个问题。你能看到这个吗? link
    猜你喜欢
    • 2019-12-02
    • 2022-01-01
    • 2018-04-19
    • 2023-04-08
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    相关资源
    最近更新 更多