fix(flutter): compatible with `path_provider` usage
MODIFY
packages/graphql/lib/src/cache/in_memory_html.dart
packages/graphql/lib/src/cache/in_memory_html.dart
@@ -10,7 +10,14 @@ import 'package:graphql/src/cache/cache.dart';
import 'package:graphql/src/utilities/helpers.dart' show deeplyMergeLeft;
class InMemoryCache implements Cache {
- String masterKey = 'graphql_in_memory';
+ InMemoryCache({
+ this.storagePrefix = '',
+ }) {
+ masterKey = storagePrefix ?? '' + '_graphql_cache';
+ }
+
+ final String storagePrefix;
+ String masterKey;
@protected
HashMap<String, dynamic> data = HashMap<String, dynamic>();

MODIFY
packages/graphql/lib/src/cache/in_memory_io.dart
packages/graphql/lib/src/cache/in_memory_io.dart
@@ -10,10 +10,17 @@ import 'package:meta/meta.dart';
import 'package:graphql/src/cache/cache.dart';
import 'package:graphql/src/utilities/helpers.dart' show deeplyMergeLeft;
+import 'package:path/path.dart';
class InMemoryCache implements Cache {
+ InMemoryCache({
+ this.storagePrefix = '',
+ });
+
+ final FutureOr<String> storagePrefix;
+
bool _writingToStorage = false;
@protected
@@ -65,7 +72,7 @@ class InMemoryCache implements Cache {
}
FutureOr<File> get _localStorageFile async {
- return File('cache.txt');
+ return File(join(await storagePrefix, 'cache.txt'));
}
Future<dynamic> _writeToStorage() async {

MODIFY
packages/graphql/lib/src/cache/in_memory_stub.dart
packages/graphql/lib/src/cache/in_memory_stub.dart
// @todo refactor this with other in_memory_* files
+import 'dart:async';
import 'dart:collection';
import 'package:graphql/src/cache/cache.dart';
import 'package:meta/meta.dart';
class InMemoryCache implements Cache {
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+
+ InMemoryCache({
+ this.storagePrefix = '',
+ });
+
+ /// For web/browser, `storagePrefix` is a prefix to the key
+ /// for [window.localStorage]
+ ///
+ /// For vm/flutter, `storagePrefix` is a path to the directory
+ /// that can save `cache.txt` file
+ ///
+ /// For flutter usually provided by
+ /// [path_provider.getApplicationDocumentsDirectory]
+ ///
+ /// @NotNull
+ final FutureOr<String> storagePrefix;
+
@protected
HashMap<String, dynamic> data = HashMap<String, dynamic>();
}

MODIFY
packages/graphql/lib/src/cache/normalized_in_memory.dart
packages/graphql/lib/src/cache/normalized_in_memory.dart
+import 'dart:async';
+
import 'package:meta/meta.dart';
import 'package:graphql/src/utilities/traverse.dart';
@@ -23,7 +25,8 @@ class NormalizedInMemoryCache extends InMemoryCache {
NormalizedInMemoryCache({
@required this.dataIdFromObject,
this.prefix = '@cache/reference',
- });
+ FutureOr<String> storagePrefix,
+ }) : super(storagePrefix: storagePrefix);
DataIdFromObject dataIdFromObject;

MODIFY
packages/graphql/lib/src/cache/optimistic.dart
packages/graphql/lib/src/cache/optimistic.dart
+import 'dart:async';
import 'dart:collection';
import 'package:meta/meta.dart';
@@ -61,9 +62,11 @@ class OptimisticCache extends NormalizedInMemoryCache {
OptimisticCache({
@required DataIdFromObject dataIdFromObject,
String prefix = '@cache/reference',
+ FutureOr<String> storagePrefix,
}) : super(
dataIdFromObject: dataIdFromObject,
prefix: prefix,
+ storagePrefix: storagePrefix,
);
@protected

ADD
packages/graphql/test/in_memory_storage_io_test.dart
+@TestOn("vm")
+
+import 'dart:async';
+import 'dart:io';
+import 'package:test/test.dart';
+import 'package:graphql/src/cache/in_memory_io.dart';
+
+void main() {
+ group('In memory exception handling', () {
+ test('FileSystemException', overridePrint((List<String> log) async {
+ final InMemoryCache cache = InMemoryCache(
+ storagePrefix: Future.error(FileSystemException()),
+ );
+ await cache.restore();
+ expect(
+ log,
+ ['Can\'t read file from storage, returning an empty HashMap.'],
+ );
+ }));
+ });
+}
+
+overridePrint(testFn(List<String> log)) => () {
+ final log = <String>[];
+ final spec = new ZoneSpecification(print: (_, __, ___, String msg) {
+ log.add(msg);
+ });
+ return Zone.current.fork(specification: spec).run(() => testFn(log));
+ };

MODIFY
packages/graphql_flutter/lib/src/caches.dart
packages/graphql_flutter/lib/src/caches.dart
import 'dart:async' show FutureOr;
-import 'dart:io' show Directory;
import 'package:meta/meta.dart';
import 'package:path_provider/path_provider.dart'
@@ -7,11 +6,11 @@ import 'package:path_provider/path_provider.dart'
import 'package:graphql/client.dart' as client;
-FutureOr<Directory> flutterStorageProvider() =>
- getApplicationDocumentsDirectory();
+final FutureOr<String> flutterStoragePrefix = (() async =>
+ (await getApplicationDocumentsDirectory()).path)();
class InMemoryCache extends client.InMemoryCache {
- InMemoryCache() : super(storageProvider: flutterStorageProvider);
+ InMemoryCache() : super(storagePrefix: flutterStoragePrefix);
}
class NormalizedInMemoryCache extends client.NormalizedInMemoryCache {
@@ -21,7 +20,7 @@ class NormalizedInMemoryCache extends client.NormalizedInMemoryCache {
}) : super(
dataIdFromObject: dataIdFromObject,
prefix: prefix,
- storageProvider: flutterStorageProvider,
+ storagePrefix: flutterStoragePrefix,
);
}
@@ -32,6 +31,6 @@ class OptimisticCache extends client.OptimisticCache {
}) : super(
dataIdFromObject: dataIdFromObject,
prefix: prefix,
- storageProvider: flutterStorageProvider,
+ storagePrefix: flutterStoragePrefix,
);
}

fix(flutter): compatible with `path_provider` usage