Skip to content

Commit

Permalink
fix(graphql): ensure readNormalized correctly merges Map<String, dyna…
Browse files Browse the repository at this point in the history
…mic> patches
  • Loading branch information
nozomemein committed Dec 17, 2024
1 parent 3628544 commit 98bd81d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/graphql/lib/src/cache/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> && patchData is Map<String, Object>) {
if (value is Map<String, dynamic> && patchData is Map<String, dynamic>) {
value = deeplyMergeLeft([
value,
patchData,
Expand Down
35 changes: 35 additions & 0 deletions packages/graphql/test/cache/graphql_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98bd81d

Please sign in to comment.