Skip to content

Commit e9fbc8b

Browse files
nozomemeinvincenzopalazzo
authored andcommitted
fix(graphql): ensure readNormalized correctly merges Map<String, dynamic> patches
1 parent b88fa9f commit e9fbc8b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/graphql/lib/src/cache/cache.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class GraphQLCache extends NormalizingDataProxy {
120120
for (final patch in optimisticPatches) {
121121
if (patch.data.containsKey(rootId)) {
122122
final patchData = patch.data[rootId];
123-
if (value is Map<String, Object> && patchData is Map<String, Object>) {
123+
if (value is Map<String, dynamic> && patchData is Map<String, dynamic>) {
124124
value = deeplyMergeLeft([
125125
value,
126126
patchData,

packages/graphql/test/cache/graphql_cache_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,41 @@ void main() {
198198
},
199199
);
200200

201+
test(
202+
'readNormalized returns correctly merged optimistic data',
203+
() {
204+
cache.recordOptimisticTransaction(
205+
(proxy) => proxy
206+
..writeQuery(
207+
basicTest.request,
208+
data: basicTest.data,
209+
),
210+
'1',
211+
);
212+
213+
expect(cache.optimisticPatches.length, 1);
214+
expect(cache.readNormalized("C:6"),
215+
equals({'cField': 'value', '__typename': 'C', 'id': 6}));
216+
217+
cache.writeNormalized('C:6', {
218+
'__typename': 'C',
219+
'id': 6,
220+
"score": null,
221+
});
222+
223+
expect(cache.readNormalized("C:6", optimistic: false),
224+
equals({'__typename': 'C', 'id': 6, 'score': null}));
225+
expect(
226+
cache.readNormalized("C:6", optimistic: true),
227+
equals({
228+
'__typename': 'C',
229+
'id': 6,
230+
'cField': 'value',
231+
'score': null
232+
}));
233+
},
234+
);
235+
201236
recordCFragmentUpdate(GraphQLCache cache) =>
202237
cache.recordOptimisticTransaction(
203238
(proxy) => proxy

0 commit comments

Comments
 (0)