From a8726cb41e1b98e9aaeb8ac3bf7192368de68981 Mon Sep 17 00:00:00 2001 From: Christian Budde Christensen Date: Sun, 3 Apr 2022 17:52:07 +0200 Subject: [PATCH] fix(graphql): Hive integration --- .../graphql/lib/src/cache/hive_store.dart | 20 ++++----- packages/graphql/lib/src/cache/store.dart | 8 ++-- packages/graphql/test/cache/store_test.dart | 43 +++++++++++++++++++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/packages/graphql/lib/src/cache/hive_store.dart b/packages/graphql/lib/src/cache/hive_store.dart index e3fb1f2e..f7e098de 100644 --- a/packages/graphql/lib/src/cache/hive_store.dart +++ b/packages/graphql/lib/src/cache/hive_store.dart @@ -13,9 +13,9 @@ class HiveStore extends Store { /// Opens a box. Convenience pass through to [Hive.openBox]. /// /// If the box is already open, the instance is returned and all provided parameters are being ignored. - static Future>> openBox( - {required String boxName, String? path}) async { - return await Hive.openBox>(boxName, path: path); + static Future?>> openBox(String boxName, + {String? path}) async { + return await Hive.openBox?>(boxName, path: path); } /// Convenience factory for `HiveStore(await openBox(boxName ?? 'graphqlClientStore', path: path))` @@ -26,7 +26,7 @@ class HiveStore extends Store { String boxName = defaultBoxName, String? path, }) async => - HiveStore(await openBox(boxName: boxName, path: path)); + HiveStore(await openBox(boxName, path: path)); /// Init Hive on specific Path static void init({required String onPath}) => Hive.init(onPath); @@ -35,7 +35,7 @@ class HiveStore extends Store { /// /// **WARNING**: Directly editing the contents of the store will not automatically /// rebroadcast operations. - final Box?> box; + final Box?> box; /// Creates a HiveStore initialized with the given [box], defaulting to `Hive.box(defaultBoxName)` /// @@ -43,14 +43,14 @@ class HiveStore extends Store { /// This lets us decouple the async initialization logic, making store usage elsewhere much more straightforward. /// /// [opened]: https://docs.hivedb.dev/#/README?id=open-a-box - HiveStore([Box>? box]) - : this.box = box ?? Hive.box>(defaultBoxName); + HiveStore([Box?>? box]) + : this.box = box ?? Hive.box?>(defaultBoxName); @override Map? get(String dataId) { final result = box.get(dataId); if (result == null) return null; - return Map.from(result); + return Map.from(result); } @override @@ -59,7 +59,7 @@ class HiveStore extends Store { } @override - void putAll(Map> data) { + void putAll(Map?> data) { box.putAll(data); } @@ -69,7 +69,7 @@ class HiveStore extends Store { } @override - Map> toMap() => Map.unmodifiable(box.toMap()); + Map?> toMap() => Map.unmodifiable(box.toMap()); Future reset() => box.clear(); } diff --git a/packages/graphql/lib/src/cache/store.dart b/packages/graphql/lib/src/cache/store.dart index 49a8605e..ac2468de 100644 --- a/packages/graphql/lib/src/cache/store.dart +++ b/packages/graphql/lib/src/cache/store.dart @@ -15,7 +15,7 @@ abstract class Store { /// [put] all entries from [data] into the store /// /// Functionally equivalent to `data.map(put);` - void putAll(Map> data); + void putAll(Map?> data); /// Delete the value of the [dataId] from the store, if preset void delete(String dataId); @@ -27,7 +27,7 @@ abstract class Store { /// /// NOTE: some [Store]s might return mutable objects /// referenced by the store itself. - Map> toMap(); + Map?> toMap(); } /// Simplest possible [Map]-backed store @@ -52,7 +52,7 @@ class InMemoryStore extends Store { void put(String dataId, Map? value) => data[dataId] = value; @override - void putAll(Map> entries) => + void putAll(Map?> entries) => data.addAll(entries); @override @@ -60,7 +60,7 @@ class InMemoryStore extends Store { /// Return the underlying [data] as an unmodifiable [Map]. @override - Map> toMap() => Map.unmodifiable(data); + Map?> toMap() => Map.unmodifiable(data); void reset() => data.clear(); } diff --git a/packages/graphql/test/cache/store_test.dart b/packages/graphql/test/cache/store_test.dart index de4e6c48..7c2a73ef 100644 --- a/packages/graphql/test/cache/store_test.dart +++ b/packages/graphql/test/cache/store_test.dart @@ -71,6 +71,49 @@ void main() { await store.box.deleteFromDisk(); }); + group("Re-open store works", () { + test("Can re-open store", () async { + final box1 = await HiveStore.openBox( + 're-open-store', + path: path, + ); + final store = HiveStore(box1); + store.put("id", {'foo': 'bar'}); + final readData = await store.get("id"); + expect(readData, equals({'foo': 'bar'})); + expect(readData, isA>()); + await box1.close(); + final box2 = await HiveStore.openBox( + 're-open-store', + path: path, + ); + final store2 = HiveStore(box2); + final readData2 = await store2.get('id'); + expect(readData2, equals({'foo': 'bar'})); + expect(readData2, isA>()); + }); + test("Can put null", () async { + final box1 = await HiveStore.openBox( + 'put-null', + path: path, + ); + final store = HiveStore(box1); + store.put("id", {'foo': 'bar'}); + store.put("id", null); + final readData = await store.get("id"); + expect(readData, equals(null)); + await box1.close(); + final box2 = await HiveStore.openBox( + 'put-null', + path: path, + ); + final store2 = HiveStore(box2); + final readData2 = await store2.get('id'); + expect(readData2, equals(null)); + expect(store2.toMap(), isA?>>()); + expect(store2.toMap(), equals({'id': null})); + }); + }); tearDownAll(() async { await Directory(path).delete(recursive: true);