From d174b51a55d40745a5dcd3a0621e77a4d435c9cd Mon Sep 17 00:00:00 2001 From: Kamil Karpacz Date: Mon, 1 Apr 2024 00:01:40 +0200 Subject: [PATCH 1/2] fix: response extensions should be optional --- .../websocket_link/websocket_messages.dart | 4 +- packages/graphql/test/websocket_test.dart | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart b/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart index 7044591b..eefbfb67 100644 --- a/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart +++ b/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart @@ -263,7 +263,7 @@ class SubscriptionData extends GraphQLSocketMessage { "type": type, "data": data, "errors": errors, - "extensions": extensions, + if (extensions != null) "extensions": extensions, }; @override @@ -288,7 +288,7 @@ class SubscriptionNext extends GraphQLSocketMessage { "type": type, "data": data, "errors": errors, - "extensions": extensions, + if (extensions != null) "extensions": extensions, }; @override diff --git a/packages/graphql/test/websocket_test.dart b/packages/graphql/test/websocket_test.dart index 2540d76d..87e5483c 100644 --- a/packages/graphql/test/websocket_test.dart +++ b/packages/graphql/test/websocket_test.dart @@ -229,6 +229,59 @@ Future main() async { ), ); }); + test('subscription data with extensions', () async { + final payload = Request( + operation: Operation(document: parseString('subscription {}')), + ); + final waitForConnection = true; + final subscriptionDataStream = + socketClient.subscribe(payload, waitForConnection); + await socketClient.connectionState + .where((state) => state == SocketConnectionState.connected) + .first; + + // ignore: unawaited_futures + socketClient.socketChannel!.stream + .where((message) => message == expectedMessage) + .first + .then((_) { + socketClient.socketChannel!.sink.add(jsonEncode({ + 'type': 'data', + 'id': '01020304-0506-4708-890a-0b0c0d0e0f10', + 'payload': { + 'data': {'foo': 'bar'}, + 'errors': [ + {'message': 'error and data can coexist'} + ], + 'extensions': {'extensionKey': 'extensionValue'}, + } + })); + }); + + await expectLater( + subscriptionDataStream, + emits( + // todo should ids be included in response context? probably '01020304-0506-4708-890a-0b0c0d0e0f10' + Response( + data: {'foo': 'bar'}, + errors: [ + GraphQLError(message: 'error and data can coexist'), + ], + context: Context().withEntry(ResponseExtensions({ + 'extensionKey': 'extensionValue', + })), + response: { + "type": "data", + "data": {"foo": "bar"}, + "errors": [ + {"message": "error and data can coexist"} + ], + "extensions": {'extensionKey': 'extensionValue'}, + }, + ), + ), + ); + }); test('resubscribe', () async { final payload = Request( operation: Operation(document: gql('subscription {}')), From 7e9c34b08cefcf7abc36172d358440049e25f58f Mon Sep 17 00:00:00 2001 From: Kamil Karpacz Date: Mon, 1 Apr 2024 15:41:11 +0200 Subject: [PATCH 2/2] fix: make == override parameters non-nullable --- .../lib/src/links/websocket_link/websocket_messages.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart b/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart index eefbfb67..dc3bad27 100644 --- a/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart +++ b/packages/graphql/lib/src/links/websocket_link/websocket_messages.dart @@ -270,7 +270,7 @@ class SubscriptionData extends GraphQLSocketMessage { int get hashCode => toJson().hashCode; @override - bool operator ==(dynamic other) => + bool operator ==(Object other) => other is SubscriptionData && jsonEncode(other) == jsonEncode(this); } @@ -295,7 +295,7 @@ class SubscriptionNext extends GraphQLSocketMessage { int get hashCode => toJson().hashCode; @override - bool operator ==(dynamic other) => + bool operator ==(Object other) => other is SubscriptionNext && jsonEncode(other) == jsonEncode(this); }