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 14 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
9 changes: 9 additions & 0 deletions src/Schema/Types/LaravelEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,13 @@ public function serialize($value): string

return $key;
}

public function parseValue($value)
{
if ($value instanceof $this->enumClass) {
return $value;
}

return parent::parseValue($value);
}
Comment on lines +155 to +162
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @stayallive, I think this might fix the issue at least for enums from bensampo/laravel-enum. I don't know what we can do about the enums modified by @enum tnough.

}
124 changes: 121 additions & 3 deletions tests/Integration/Subscriptions/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@ protected function setUp(): void
body: String @guard
}

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

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

type Mutation {
createPost(title: String!, body: String): Post
@mock
@broadcast(subscription: "onPostCreated")

updatePost(post: String!): Post
@mock
@broadcast(subscription: "onPostUpdated")
}

type Query {
Expand Down Expand Up @@ -121,7 +131,6 @@ public function testBroadcastSubscriptions(): void
');

$broadcastManager = $this->app->make(BroadcastManager::class);

$log = $broadcastManager->driver();
assert($log instanceof LogBroadcaster);

Expand Down Expand Up @@ -192,6 +201,115 @@ public function testWithoutExcludeEmpty(): void
]);
}

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

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

$broadcastManager = $this->app->make(BroadcastManager::class);
$log = $broadcastManager->driver();
assert($log instanceof LogBroadcaster);

$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
}
}
');

$broadcastManager = $this->app->make(BroadcastManager::class);
$log = $broadcastManager->driver();
assert($log instanceof LogBroadcaster);

$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
}
}
');

$broadcastManager = $this->app->make(BroadcastManager::class);
$log = $broadcastManager->driver();
assert($log instanceof LogBroadcaster);

$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 testWithExcludeEmpty(): void
{
$config = $this->app->make(ConfigRepository::class);
Expand Down Expand Up @@ -353,11 +471,11 @@ protected function subscribe(): TestResponse
*
* @return array<string, array<string, mixed>>
*/
protected function buildResponse(string $channelName, string $channel): array
protected function buildResponse(string $fieldName, string $channel): array
{
return [
'data' => [
'onPostCreated' => null,
$fieldName => null,
],
'extensions' => [
'lighthouse_subscriptions' => [
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

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1 with Laravel ^10 and highest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1 with Laravel ^9 and highest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2 with Laravel ^9 and highest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2 with Laravel ^10 and highest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0 with Laravel ^9 and highest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2 with Laravel ^9 and lowest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1 with Laravel ^9 and lowest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0 with Laravel ^9 and lowest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2 with Laravel ^10 and lowest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.

Check failure on line 22 in tests/Utils/Subscriptions/OnPostUpdated.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1 with Laravel ^10 and lowest dependencies

Method Tests\Utils\Subscriptions\OnPostUpdated::filter() has parameter $root with no type specified.
{
return true;
}
}
Loading