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

Fix rehydration of subscription args with differing internal representations #1415

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Subscriptions/SubscriptionBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function (Subscriber $subscriber) use ($root): void {
$subscriber->args,
$subscriber->setRoot($root)
);
dump($data);
spawnia marked this conversation as resolved.
Show resolved Hide resolved

$this->broadcastManager->broadcast(
$subscriber,
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriptions/SubscriptionResolverProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function (string $class): bool {
}

$subscriber = new Subscriber(
$args,
$context->request()->get('variables'), // @TODO: This must be changed to correctly consider batched requests
$context,
$resolveInfo
);
Expand Down
119 changes: 116 additions & 3 deletions tests/Integration/Subscriptions/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ protected function setUp(): void
body: String
}

enum PostStatus {
PUBLISHED @enum(value: "published")
DELETED @enum(value: "deleted")
}

type Subscription {
onPostCreated: Post
onPostUpdated(status: PostStatus!): Post
}

type Mutation {
createPost(post: String!): Post
@field(resolver: "{$this->qualifyTestResolver()}")
@broadcast(subscription: "onPostCreated")

updatePost(post: String!): Post
@field(resolver: "{$this->qualifyTestResolver()}")
@broadcast(subscription: "onPostUpdated")
}

type Query {
Expand All @@ -46,7 +56,7 @@ protected function setUp(): void

public function testSendsSubscriptionChannelInResponse(): void
{
$response = $this->subscribe();
$response = $this->subscribeToOnPostCreatedSubscription();
$subscriber = app(StorageManager::class)->subscribersByTopic('ON_POST_CREATED')->first();

$this->assertInstanceOf(Subscriber::class, $subscriber);
Expand Down Expand Up @@ -90,7 +100,7 @@ public function testSendsSubscriptionChannelInBatchedResponse(): void

public function testCanBroadcastSubscriptions(): void
{
$this->subscribe();
$this->subscribeToOnPostCreatedSubscription();
$this->graphQL(/** @lang GraphQL */ '
mutation {
createPost(post: "Foobar") {
Expand Down Expand Up @@ -138,6 +148,109 @@ public function testWithFieldAlias(): void
]);
}

public function testSubscriptionWithEnumInputCorrectlyResolves(): void
{
$this->graphQL(/** @lang GraphQL */ '
subscription {
onPostUpdated(status: DELETED) {
body
}
}
');

$this->graphQL(/** @lang GraphQL */ '
mutation {
updatePost(post: "Foobar") {
body
}
}
');

/** @var \Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster $log */
$log = app(BroadcastManager::class)->driver();
$this->assertCount(1, $log->broadcasts());

$broadcasted = Arr::get(Arr::first($log->broadcasts()), 'data', []);
$this->assertArrayHasKey('onPostUpdated', $broadcasted);
$this->assertSame(['body' => 'Foobar'], $broadcasted['onPostUpdated']);
}

public function testSubscriptionWithEnumInputVariableCorrectlyResolves(): void
{
$this->postGraphQL([
'query' => /** @lang GraphQL */ '
subscription OnPostUpdated($status: PostStatus!) {
onPostUpdated(status: $status) {
body
}
}
',
'variables' => [
'status' => 'DELETED',
],
'operationName' => 'OnPostUpdated',
]);

$this->graphQL(/** @lang GraphQL */ '
mutation {
updatePost(post: "Foobar") {
body
}
}
');

/** @var \Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster $log */
$log = app(BroadcastManager::class)->driver();
$this->assertCount(1, $log->broadcasts());

$broadcasted = Arr::get(Arr::first($log->broadcasts()), 'data', []);
$this->assertArrayHasKey('onPostUpdated', $broadcasted);
$this->assertSame(['body' => 'Foobar'], $broadcasted['onPostUpdated']);
}

public function testSubscriptionWithEnumInputCorrectlyResolvesUsingBatchedQuery(): void
{
$this
->postGraphQL([
[
'query' => /** @lang GraphQL */ '
{
bar
}
',
],
[
'query' => /** @lang GraphQL */ '
subscription OnPostUpdated($status: PostStatus!) {
onPostUpdated(status: $status) {
body
}
}
',
'variables' => [
'status' => 'DELETED',
],
'operationName' => 'OnPostUpdated',
],
]);

$this->graphQL(/** @lang GraphQL */ '
mutation {
updatePost(post: "Foobar") {
body
}
}
');

/** @var \Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster $log */
$log = app(BroadcastManager::class)->driver();
$this->assertCount(1, $log->broadcasts());

$broadcasted = Arr::get(Arr::first($log->broadcasts()), 'data', []);
$this->assertArrayHasKey('onPostUpdated', $broadcasted);
$this->assertSame(['body' => 'Foobar'], $broadcasted['onPostUpdated']);
}

/**
* @param array<string, mixed> $args
* @return array<string, string>
Expand All @@ -152,7 +265,7 @@ public function resolve($root, array $args): array
/**
* @return \Illuminate\Testing\TestResponse
*/
protected function subscribe()
protected function subscribeToOnPostCreatedSubscription()
{
return $this->postGraphQL([
'query' => /** @lang GraphQL */ '
Expand Down
26 changes: 26 additions & 0 deletions tests/Utils/Subscriptions/OnPostUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Utils\Subscriptions;

use Illuminate\Http\Request;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Nuwave\Lighthouse\Subscriptions\Subscriber;

class OnPostUpdated extends GraphQLSubscription
{
/**
* Check if subscriber is allowed to listen to the subscription.
*/
public function authorize(Subscriber $subscriber, Request $request): bool
{
return true;
}

/**
* Filter which subscribers should receive the subscription.
*/
public function filter(Subscriber $subscriber, $root): bool
{
return true;
}
}