Skip to content

Commit

Permalink
Merge pull request #1419 from kamilkarp/fix/response-extensions-optional
Browse files Browse the repository at this point in the history
fix: response extensions should be optional
  • Loading branch information
vincenzopalazzo authored Apr 4, 2024
2 parents 103792f + 7e9c34b commit eebac38
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ class SubscriptionData extends GraphQLSocketMessage {
"type": type,
"data": data,
"errors": errors,
"extensions": extensions,
if (extensions != null) "extensions": extensions,
};

@override
int get hashCode => toJson().hashCode;

@override
bool operator ==(dynamic other) =>
bool operator ==(Object other) =>
other is SubscriptionData && jsonEncode(other) == jsonEncode(this);
}

Expand All @@ -288,14 +288,14 @@ class SubscriptionNext extends GraphQLSocketMessage {
"type": type,
"data": data,
"errors": errors,
"extensions": extensions,
if (extensions != null) "extensions": extensions,
};

@override
int get hashCode => toJson().hashCode;

@override
bool operator ==(dynamic other) =>
bool operator ==(Object other) =>
other is SubscriptionNext && jsonEncode(other) == jsonEncode(this);
}

Expand Down
53 changes: 53 additions & 0 deletions packages/graphql/test/websocket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,59 @@ Future<void> 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(<dynamic, dynamic>{
'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 {}')),
Expand Down

0 comments on commit eebac38

Please sign in to comment.