Skip to content

Commit

Permalink
fix(graphql): transform nested object from box
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnnnn committed Apr 9, 2024
1 parent 385c331 commit 3ac14cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/graphql/lib/src/cache/hive_store.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';
import 'package:meta/meta.dart';

import 'package:hive/hive.dart';
import 'package:meta/meta.dart';

import './store.dart';

Expand Down Expand Up @@ -92,7 +92,15 @@ class HiveStore extends Store {
}

@override
Map<String, Map<String, dynamic>?> toMap() => Map.unmodifiable(box.toMap());
Map<String, Map<String, dynamic>?> toMap() {
final map = <String, Map<String, dynamic>?>{};
for (final key in box.keys) {
if (key is String) {
map[key] = get(key);
}
}
return Map.unmodifiable(map);
}

Future<void> reset() => box.clear();
}
22 changes: 22 additions & 0 deletions packages/graphql/test/cache/store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ void main() {
expect(readData2?['bob'], isA<List<dynamic>>());
expect(readData2?['bob'][0], isA<Map<String, dynamic>>());
});
test("Can re-open and reference nested data", () async {
final box1 = await HiveStore.openBox(
're-open-store',
path: path,
);
final store = HiveStore(box1);
final data = {
'foo': 'bar',
'bob': [
{'nested': true}
]
};
store.put("id", data);
expect(store.toMap(), equals({'id': data}));
await box1.close();
final box2 = await HiveStore.openBox(
're-open-store',
path: path,
);
final store2 = HiveStore(box2);
expect(store2.toMap(), equals({'id': data}));
});
test("Can put null", () async {
final box1 = await HiveStore.openBox(
'put-null',
Expand Down

0 comments on commit 3ac14cc

Please sign in to comment.