Search code examples
subscriptionlaravel-lighthouse

What is the Lighthouse GraphQL subscriptions Best Practice


I have tried to find best practice for Lighthouse subscription during 2 weeks.

But i cant find the best practice until now.

This is my 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! }

When i query mutation updateHoliday like below

enter image description here

I hope RsvHolidaySub resolve function is called from @broadcast(subscription: "rsvHolidaySub")

So i can broadcast like this

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

I don't know this is the best practice for Subscription. But i think this is very simple and clean.

But even if i installed websocket by using redis + laravel-echo-server or beyondcode/laravel-websockets, The resolve function is not called.

So i doubt this is possible or not.

I really want to know Lighthouse subscription best practice. Please share your knowledge.


Solution

  • I trigger all of my subscriptions from code.

    But I also don't use those @create or @update directives, but just a kind of field, so I do everything I want in my code and then just trigger the subscription even with any other data... Works fine for me.

    But I don't know what would be the "best practices".