From 98bd81d1c21b16c4a132241726166de92a6f4970 Mon Sep 17 00:00:00 2001 From: nozomemein Date: Wed, 18 Dec 2024 07:45:38 +0900 Subject: [PATCH] fix(graphql): ensure readNormalized correctly merges Map patches --- packages/graphql/lib/src/cache/cache.dart | 2 +- .../test/cache/graphql_cache_test.dart | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/graphql/lib/src/cache/cache.dart b/packages/graphql/lib/src/cache/cache.dart index f69659fc2..f82fb4a70 100644 --- a/packages/graphql/lib/src/cache/cache.dart +++ b/packages/graphql/lib/src/cache/cache.dart @@ -120,7 +120,7 @@ class GraphQLCache extends NormalizingDataProxy { for (final patch in optimisticPatches) { if (patch.data.containsKey(rootId)) { final patchData = patch.data[rootId]; - if (value is Map && patchData is Map) { + if (value is Map && patchData is Map) { value = deeplyMergeLeft([ value, patchData, diff --git a/packages/graphql/test/cache/graphql_cache_test.dart b/packages/graphql/test/cache/graphql_cache_test.dart index 4c2c995c1..1b47d0a4b 100644 --- a/packages/graphql/test/cache/graphql_cache_test.dart +++ b/packages/graphql/test/cache/graphql_cache_test.dart @@ -198,6 +198,41 @@ void main() { }, ); + test( + 'readNormalized returns correctly merged optimistic data', + () { + cache.recordOptimisticTransaction( + (proxy) => proxy + ..writeQuery( + basicTest.request, + data: basicTest.data, + ), + '1', + ); + + expect(cache.optimisticPatches.length, 1); + expect(cache.readNormalized("C:6"), + equals({'cField': 'value', '__typename': 'C', 'id': 6})); + + cache.writeNormalized('C:6', { + '__typename': 'C', + 'id': 6, + "score": null, + }); + + expect(cache.readNormalized("C:6", optimistic: false), + equals({'__typename': 'C', 'id': 6, 'score': null})); + expect( + cache.readNormalized("C:6", optimistic: true), + equals({ + '__typename': 'C', + 'id': 6, + 'cField': 'value', + 'score': null + })); + }, + ); + recordCFragmentUpdate(GraphQLCache cache) => cache.recordOptimisticTransaction( (proxy) => proxy