Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing through the rootValue as an option #492

Merged
merged 1 commit into from
Oct 22, 2019
Merged

Allow passing through the rootValue as an option #492

merged 1 commit into from
Oct 22, 2019

Conversation

tuurbo
Copy link
Contributor

@tuurbo tuurbo commented Oct 15, 2019

@mfn
Copy link
Collaborator

mfn commented Oct 17, 2019

@tuurbo nice, never heard of it

Can you share some use cases and add a test for it, please?

@tuurbo
Copy link
Contributor Author

tuurbo commented Oct 17, 2019

Here is a use case I currently use.

// NotificationMutation.php
$notification = Notification::find($id);
$notification->whatever = 'foobar';
$notification->save();

// send to clients via redis pub/sub
$this->publish('notification', $notification->fresh());
function publish($subscriptionName, $rootValue) {
    // check if anyone is subscribing to the $subscriptionName
    $subscription = $this->getSubscription($subscriptionName);

    if ($subscription) {
        // retrieve the schema of the $subscription
        $schema = $this->getSchema($subscription);
    
        // parse $rootValue into a graphql payload
        $payload = \GraphQL::query($schema, [], ['rootValue' => $rootValue]);
        
        // send payload to anyone subscribed to the schema that macthes the $subscriptionName
        Redis::connection('pub')->publish('graphql-channel', json_encode([
            'type' => 'data',
            'id' => $subscription['id'],
            'payload' => $payload,
            'key' => $subscription['key'],
        ]));
    }
}

@tuurbo
Copy link
Contributor Author

tuurbo commented Oct 17, 2019

@mfn Submitted use case and test, Thanks

@crissi
Copy link
Contributor

crissi commented Oct 22, 2019

doing graphql subscriptions here? If yes maybe you could care to share som code, would be could to add support for it in the framework

@mfn mfn self-requested a review October 22, 2019 21:58
@mfn mfn merged commit 6b313bc into rebing:master Oct 22, 2019
@mfn
Copy link
Collaborator

mfn commented Oct 22, 2019

Sorry, totally forgot about this PR 🤷‍♀️

I've added a changelog and squashed the commits and released it with https://github.com/rebing/graphql-laravel/releases/tag/3.1.0

@mfn
Copy link
Collaborator

mfn commented May 4, 2021

@tuurbo do you remember why you used rootValue and… maybe could you have used context?

I'm in the process of revamping the internal working and wonder if we truly need to support the rootValue like this or if context would solve this, too?

@tuurbo
Copy link
Contributor Author

tuurbo commented May 4, 2021

@mfn Its been a while since I've looked at this but we still use the code I wrote above today. I remember copying the apollo graphql JS library and rewriting it into php to suit our needs. https://github.com/apollographql/graphql-subscriptions/blob/v0.1.5/src/pubsub.ts#L155-L156 Not sure if the recommended way it's done now is any different.

Passing the data as context seems weird to me but I suppose it would work. I don't know enough about the inner workings of graphql to say for sure.

@mfn
Copy link
Collaborator

mfn commented May 4, 2021

@tuurbo thanks for your quick response, I found out I don't need to remove support for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants