Skip to content

Commit

Permalink
Merge pull request #1242 from juancastillo0/main
Browse files Browse the repository at this point in the history
Allow list of errors as payloads for graphql-transport-ws subprotocol's "error" message type
  • Loading branch information
vincenzopalazzo authored Oct 7, 2022
2 parents 6a5caf9 + 423adb3 commit 9b2d25c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ abstract class GraphQLSocketMessage extends JsonSerializable {
final Map<String, dynamic> map =
json.decode(message as String) as Map<String, dynamic>;
final String type = (map['type'] ?? 'unknown') as String;
final payload =
(map['payload'] ?? <String, dynamic>{}) as Map<String, dynamic>;
final payload = map['payload'] ?? <String, dynamic>{};
final String id = (map['id'] ?? 'none') as String;

switch (type) {
Expand All @@ -77,15 +76,15 @@ abstract class GraphQLSocketMessage extends JsonSerializable {

// for completeness
case MessageTypes.subscribe:
return SubscribeOperation(id, payload);
return SubscribeOperation(id, payload as Map<String, dynamic>);
case MessageTypes.start:
return StartOperation(id, payload);
return StartOperation(id, payload as Map<String, dynamic>);
case MessageTypes.stop:
return StopOperation(id);
case MessageTypes.ping:
return PingMessage(payload);
return PingMessage(payload as Map<String, dynamic>);
case MessageTypes.pong:
return PongMessage(payload);
return PongMessage(payload as Map<String, dynamic>);

case MessageTypes.data:
return SubscriptionData(id, payload['data'], payload['errors']);
Expand Down
41 changes: 41 additions & 0 deletions packages/graphql/test/websocket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,47 @@ Future<void> main() async {
});
});

group('GraphQLSocketMessage.parse', () {
test('graphql-transport-ws subprotocol with errors', () {
const payload = [
{
'message': 'Cannot query field "wrongQuery" on type "Query".',
'locations': [
{'line': 1, 'column': 2}
],
'extensions': {
'validationError': {
'spec': 'https://spec.graphql.org/draft/#sec-Field-Selections',
}
}
}
];

const message = {'id': 'message-id', 'type': 'error', 'payload': payload};
final parsed = GraphQLSocketMessage.parse(jsonEncode(message));
expect(parsed, isA<SubscriptionError>());
expect(parsed.toJson(), message);
});

test('graphql-ws apollo subprotocol with errors', () {
const payload = {
'message': 'Cannot query field "wrongQuery" on type "Query".',
'locations': [
{'line': 1, 'column': 2}
],
'extensions': {
'validationError': {
'spec': 'https://spec.graphql.org/draft/#sec-Field-Selections',
}
}
};
const message = {'id': 'message-id', 'type': 'error', 'payload': payload};
final parsed = GraphQLSocketMessage.parse(jsonEncode(message));
expect(parsed, isA<SubscriptionError>());
expect(parsed.toJson(), message);
});
});

group('SocketClient without payload', () {
late SocketClient socketClient;
StreamController<dynamic> controller;
Expand Down

0 comments on commit 9b2d25c

Please # to comment.