Skip to content

Commit c1d1392

Browse files
truongsinhmicimize
authored andcommitted
mutation is also cached, with SplayTreeMap (#7)
1 parent fafcfff commit c1d1392

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lib/src/core/query_manager.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,8 @@ class QueryManager {
119119
).first;
120120

121121
// save the data from fetchResult to the cache
122-
if (
123-
// should never cache a mutation
124-
!(options is MutationOptions) &&
125-
fetchResult.data != null &&
126-
options.fetchPolicy != FetchPolicy.noCache) {
122+
if (fetchResult.data != null &&
123+
options.fetchPolicy != FetchPolicy.noCache) {
127124
cache.write(
128125
operation.toKey(),
129126
fetchResult.data,

lib/src/link/operation.dart

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import 'dart:convert';
1+
import 'dart:collection' show SplayTreeMap;
2+
import 'dart:convert' show json;
3+
import 'dart:io' show File;
24

35
class Operation {
4-
Operation({
6+
factory Operation({
7+
String document,
8+
Map<String, dynamic> variables,
9+
String operationName,
10+
Map<String, dynamic> extensions,
11+
}) =>
12+
Operation._(
13+
document: document,
14+
variables: SplayTreeMap<String, dynamic>.of(variables),
15+
operationName: operationName,
16+
extensions: extensions,
17+
);
18+
19+
Operation._({
520
this.document,
621
this.variables,
722
this.operationName,
823
this.extensions,
924
});
1025

1126
final String document;
12-
final Map<String, dynamic> variables;
27+
final SplayTreeMap<String, dynamic> variables;
1328
final String operationName;
1429
final Map<String, dynamic> extensions;
1530

1631
final Map<String, dynamic> _context = <String, dynamic>{};
1732

18-
/// Sets the context of an opration by merging the new context with the old one.
33+
/// Sets the context of an operation by merging the new context with the old one.
1934
void setContext(Map<String, dynamic> next) {
2035
_context.addAll(next);
2136
}
@@ -28,9 +43,14 @@ class Operation {
2843
}
2944

3045
String toKey() {
31-
/// XXX we're assuming here that variables will be serialized in the same order.
32-
/// that might not always be true
33-
final String encodedVariables = json.encode(variables);
46+
/// SplayTreeMap is always sorted
47+
final String encodedVariables =
48+
json.encode(variables, toEncodable: (dynamic object) {
49+
if (object is File) {
50+
return object.path;
51+
}
52+
return object;
53+
});
3454

3555
return '$document|$encodedVariables|$operationName';
3656
}

0 commit comments

Comments
 (0)