From 3ac14ccd27e295e88ce51b187cd5e7159407a446 Mon Sep 17 00:00:00 2001 From: ronnnnn Date: Tue, 9 Apr 2024 10:14:32 +0900 Subject: [PATCH] fix(graphql): transform nested object from box --- .../graphql/lib/src/cache/hive_store.dart | 12 ++++++++-- packages/graphql/test/cache/store_test.dart | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/graphql/lib/src/cache/hive_store.dart b/packages/graphql/lib/src/cache/hive_store.dart index c6d131301..924c0df23 100644 --- a/packages/graphql/lib/src/cache/hive_store.dart +++ b/packages/graphql/lib/src/cache/hive_store.dart @@ -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'; @@ -92,7 +92,15 @@ class HiveStore extends Store { } @override - Map?> toMap() => Map.unmodifiable(box.toMap()); + Map?> toMap() { + final map = ?>{}; + for (final key in box.keys) { + if (key is String) { + map[key] = get(key); + } + } + return Map.unmodifiable(map); + } Future reset() => box.clear(); } diff --git a/packages/graphql/test/cache/store_test.dart b/packages/graphql/test/cache/store_test.dart index 98811233c..c78617d8a 100644 --- a/packages/graphql/test/cache/store_test.dart +++ b/packages/graphql/test/cache/store_test.dart @@ -121,6 +121,28 @@ void main() { expect(readData2?['bob'], isA>()); expect(readData2?['bob'][0], isA>()); }); + 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',