Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(graphql): ensure readNormalized correctly merges Map<String, dynamic> patches #1474

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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