diff --git a/packages/graphql/lib/src/cache/cache.dart b/packages/graphql/lib/src/cache/cache.dart index f69659fc..f82fb4a7 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 4c2c995c..1b47d0a4 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