Skip to content

Commit 9b2d25c

Browse files
Merge pull request #1242 from juancastillo0/main
Allow list of errors as payloads for graphql-transport-ws subprotocol's "error" message type
2 parents 6a5caf9 + 423adb3 commit 9b2d25c

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

packages/graphql/lib/src/links/websocket_link/websocket_messages.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ abstract class GraphQLSocketMessage extends JsonSerializable {
5757
final Map<String, dynamic> map =
5858
json.decode(message as String) as Map<String, dynamic>;
5959
final String type = (map['type'] ?? 'unknown') as String;
60-
final payload =
61-
(map['payload'] ?? <String, dynamic>{}) as Map<String, dynamic>;
60+
final payload = map['payload'] ?? <String, dynamic>{};
6261
final String id = (map['id'] ?? 'none') as String;
6362

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

7877
// for completeness
7978
case MessageTypes.subscribe:
80-
return SubscribeOperation(id, payload);
79+
return SubscribeOperation(id, payload as Map<String, dynamic>);
8180
case MessageTypes.start:
82-
return StartOperation(id, payload);
81+
return StartOperation(id, payload as Map<String, dynamic>);
8382
case MessageTypes.stop:
8483
return StopOperation(id);
8584
case MessageTypes.ping:
86-
return PingMessage(payload);
85+
return PingMessage(payload as Map<String, dynamic>);
8786
case MessageTypes.pong:
88-
return PongMessage(payload);
87+
return PongMessage(payload as Map<String, dynamic>);
8988

9089
case MessageTypes.data:
9190
return SubscriptionData(id, payload['data'], payload['errors']);

packages/graphql/test/websocket_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,47 @@ Future<void> main() async {
7070
});
7171
});
7272

73+
group('GraphQLSocketMessage.parse', () {
74+
test('graphql-transport-ws subprotocol with errors', () {
75+
const payload = [
76+
{
77+
'message': 'Cannot query field "wrongQuery" on type "Query".',
78+
'locations': [
79+
{'line': 1, 'column': 2}
80+
],
81+
'extensions': {
82+
'validationError': {
83+
'spec': 'https://spec.graphql.org/draft/#sec-Field-Selections',
84+
}
85+
}
86+
}
87+
];
88+
89+
const message = {'id': 'message-id', 'type': 'error', 'payload': payload};
90+
final parsed = GraphQLSocketMessage.parse(jsonEncode(message));
91+
expect(parsed, isA<SubscriptionError>());
92+
expect(parsed.toJson(), message);
93+
});
94+
95+
test('graphql-ws apollo subprotocol with errors', () {
96+
const payload = {
97+
'message': 'Cannot query field "wrongQuery" on type "Query".',
98+
'locations': [
99+
{'line': 1, 'column': 2}
100+
],
101+
'extensions': {
102+
'validationError': {
103+
'spec': 'https://spec.graphql.org/draft/#sec-Field-Selections',
104+
}
105+
}
106+
};
107+
const message = {'id': 'message-id', 'type': 'error', 'payload': payload};
108+
final parsed = GraphQLSocketMessage.parse(jsonEncode(message));
109+
expect(parsed, isA<SubscriptionError>());
110+
expect(parsed.toJson(), message);
111+
});
112+
});
113+
73114
group('SocketClient without payload', () {
74115
late SocketClient socketClient;
75116
StreamController<dynamic> controller;

0 commit comments

Comments
 (0)