Skip to content

Commit

Permalink
fix: response extensions should be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkarp committed Mar 31, 2024
1 parent 103792f commit d174b51
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class SubscriptionData extends GraphQLSocketMessage {
"type": type,
"data": data,
"errors": errors,
"extensions": extensions,
if (extensions != null) "extensions": extensions,
};

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

@override
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 d174b51

Please sign in to comment.