Skip to content

Commit 37e6c67

Browse files
authored
fix(graphql): Consume streams from offline graphql requests (#617)
1 parent 386a693 commit 37e6c67

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed

packages/brick_offline_first_with_graphql/lib/src/graphql_offline_request_queue.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ class GraphqlOfflineRequestQueue extends OfflineRequestQueue<Request> {
2222
@override
2323
Future<void> transmitRequest(Request request) async {
2424
logger.finest('Processing request ${request.operation.operationName}');
25-
link.request(request);
25+
await link.request(request).drain();
2626
}
2727
}

packages/brick_offline_first_with_graphql/test/__helpers__.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:convert';
22

3+
import 'package:gql/ast.dart';
4+
import 'package:gql/language.dart';
35
import 'package:gql_exec/gql_exec.dart';
46
import 'package:gql_link/gql_link.dart';
57
import 'package:mockito/mockito.dart';
@@ -55,3 +57,5 @@ class MockLink extends Mock implements Link {
5557
),
5658
) as Stream<Response>;
5759
}
60+
61+
DocumentNode gql(String query) => parseString(query);
Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import 'dart:async';
2+
13
import 'package:brick_offline_first_with_graphql/src/graphql_offline_queue_link.dart';
24
import 'package:brick_offline_first_with_graphql/src/graphql_offline_request_queue.dart';
35
import 'package:brick_offline_first_with_graphql/src/graphql_request_sqlite_cache_manager.dart';
6+
import 'package:gql_exec/gql_exec.dart';
7+
import 'package:mockito/mockito.dart';
48
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
59
import 'package:test/test.dart';
610

11+
import '__helpers__.dart';
12+
713
void main() {
814
final offlineClient = GraphqlOfflineQueueLink(
915
GraphqlRequestSqliteCacheManager('db', databaseFactory: databaseFactoryFfi),
@@ -15,23 +21,85 @@ void main() {
1521
databaseFactory: databaseFactoryFfi,
1622
);
1723

18-
test('#start', () {
19-
final queue = GraphqlOfflineRequestQueue(
20-
link: offlineClient,
21-
requestManager: requestManager,
22-
)..start();
23-
expect(queue.isRunning, isTrue);
24-
queue.stop();
24+
group('Queue Lifecycle', () {
25+
test('#start', () {
26+
final queue = GraphqlOfflineRequestQueue(
27+
link: offlineClient,
28+
requestManager: requestManager,
29+
)..start();
30+
expect(queue.isRunning, isTrue);
31+
queue.stop();
32+
});
33+
34+
test('#stop', () {
35+
final queue = GraphqlOfflineRequestQueue(
36+
link: offlineClient,
37+
requestManager: requestManager,
38+
)..start();
39+
expect(queue.isRunning, isTrue);
40+
queue.stop();
41+
expect(queue.isRunning, isFalse);
42+
});
2543
});
2644

27-
test('#stop', () {
28-
final queue = GraphqlOfflineRequestQueue(
29-
link: offlineClient,
30-
requestManager: requestManager,
31-
)..start();
32-
expect(queue.isRunning, isTrue);
33-
queue.stop();
34-
expect(queue.isRunning, isFalse);
45+
group('Request Processing', () {
46+
test('#transmitRequest', () async {
47+
final mockLink = MockLink();
48+
var streamConsumed = false;
49+
50+
when(mockLink.request(any)).thenAnswer((_) {
51+
return Stream<Response>.eventTransformed(
52+
Stream.fromIterable([
53+
const Response(
54+
data: {'test': 'data'},
55+
response: {'body': '{"test":"data"}'},
56+
),
57+
]),
58+
(sink) {
59+
streamConsumed = true;
60+
return sink;
61+
},
62+
);
63+
});
64+
65+
final testQueue = GraphqlOfflineRequestQueue(
66+
link: mockLink,
67+
requestManager: requestManager,
68+
);
69+
70+
final testRequest = Request(
71+
operation: Operation(
72+
document: gql('mutation TestMutation { test }'),
73+
),
74+
);
75+
76+
await testQueue.transmitRequest(testRequest);
77+
78+
expect(streamConsumed, isTrue);
79+
});
80+
81+
test('#transmitRequest with errors', () async {
82+
final mockLink = MockLink();
83+
when(mockLink.request(any)).thenAnswer(
84+
(_) => Stream.error(Exception('Network error')),
85+
);
86+
87+
final testQueue = GraphqlOfflineRequestQueue(
88+
link: mockLink,
89+
requestManager: requestManager,
90+
);
91+
92+
final testRequest = Request(
93+
operation: Operation(
94+
document: gql('mutation TestMutation { test }'),
95+
),
96+
);
97+
98+
expect(
99+
() => testQueue.transmitRequest(testRequest),
100+
throwsA(isA<Exception>()),
101+
);
102+
});
35103
});
36104
});
37105
}

0 commit comments

Comments
 (0)