Skip to content

Commit eebac38

Browse files
Merge pull request zino-hofmann#1419 from kamilkarp/fix/response-extensions-optional
fix: response extensions should be optional
2 parents 103792f + 7e9c34b commit eebac38

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ class SubscriptionData extends GraphQLSocketMessage {
263263
"type": type,
264264
"data": data,
265265
"errors": errors,
266-
"extensions": extensions,
266+
if (extensions != null) "extensions": extensions,
267267
};
268268

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

272272
@override
273-
bool operator ==(dynamic other) =>
273+
bool operator ==(Object other) =>
274274
other is SubscriptionData && jsonEncode(other) == jsonEncode(this);
275275
}
276276

@@ -288,14 +288,14 @@ class SubscriptionNext extends GraphQLSocketMessage {
288288
"type": type,
289289
"data": data,
290290
"errors": errors,
291-
"extensions": extensions,
291+
if (extensions != null) "extensions": extensions,
292292
};
293293

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

297297
@override
298-
bool operator ==(dynamic other) =>
298+
bool operator ==(Object other) =>
299299
other is SubscriptionNext && jsonEncode(other) == jsonEncode(this);
300300
}
301301

packages/graphql/test/websocket_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,59 @@ Future<void> main() async {
229229
),
230230
);
231231
});
232+
test('subscription data with extensions', () async {
233+
final payload = Request(
234+
operation: Operation(document: parseString('subscription {}')),
235+
);
236+
final waitForConnection = true;
237+
final subscriptionDataStream =
238+
socketClient.subscribe(payload, waitForConnection);
239+
await socketClient.connectionState
240+
.where((state) => state == SocketConnectionState.connected)
241+
.first;
242+
243+
// ignore: unawaited_futures
244+
socketClient.socketChannel!.stream
245+
.where((message) => message == expectedMessage)
246+
.first
247+
.then((_) {
248+
socketClient.socketChannel!.sink.add(jsonEncode({
249+
'type': 'data',
250+
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
251+
'payload': {
252+
'data': {'foo': 'bar'},
253+
'errors': [
254+
{'message': 'error and data can coexist'}
255+
],
256+
'extensions': {'extensionKey': 'extensionValue'},
257+
}
258+
}));
259+
});
260+
261+
await expectLater(
262+
subscriptionDataStream,
263+
emits(
264+
// todo should ids be included in response context? probably '01020304-0506-4708-890a-0b0c0d0e0f10'
265+
Response(
266+
data: {'foo': 'bar'},
267+
errors: [
268+
GraphQLError(message: 'error and data can coexist'),
269+
],
270+
context: Context().withEntry(ResponseExtensions(<dynamic, dynamic>{
271+
'extensionKey': 'extensionValue',
272+
})),
273+
response: {
274+
"type": "data",
275+
"data": {"foo": "bar"},
276+
"errors": [
277+
{"message": "error and data can coexist"}
278+
],
279+
"extensions": {'extensionKey': 'extensionValue'},
280+
},
281+
),
282+
),
283+
);
284+
});
232285
test('resubscribe', () async {
233286
final payload = Request(
234287
operation: Operation(document: gql('subscription {}')),

0 commit comments

Comments
 (0)