Skip to content

Commit

Permalink
feat(graphql): HiveStore.open
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Jun 6, 2020
1 parent ffd3294 commit 6db4677
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
36 changes: 19 additions & 17 deletions changelog-v3-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
v4 aims to solve a number of sore spots, particularly with caching, largely by leveraging libraries from the https://github.com/gql-dart ecosystem. There has also been a concerted effort to add more API docstrings to the codebase.

## Cache overhaul
* There is now only a single `GraphQLCache`, which leverages [normalize](https://pub.dev/packages/normalize),

- There is now only a single `GraphQLCache`, which leverages [normalize](https://pub.dev/packages/normalize),
Giving us a much more `apollo`ish api including `typePolicies`
* `LazyCacheMap` has been deleted
* `GraphQLCache` marks itself for rebroadcasting (should fix some related issues)
* **`Store`** is now a seperate concern:
- `LazyCacheMap` has been deleted
- `GraphQLCache` marks itself for rebroadcasting (should fix some related issues)
- **`Store`** is now a seperate concern:

```dart
GraphQLCache(
// The default store is the InMemoryStore, which does NOT persist to disk
store: HiveStore(),
store: await HiveStore.open(),
)
```

and persistence is broken into a seperate `Store` concern.

## We now use the [gql_link system](https://github.com/gql-dart/gql/tree/master/links/gql_link)
* Most links are re-exported from `graphql/client.dart`
* `QueryOptions`, `MutationOptions`, etc are turned into
[gql_exec](https://github.com/gql-dart/gql/tree/master/links/gql_exec) `Request`s
before being sent down the link chain.
* `documentNode` is deprecated in favor of `DocumentNode document` for consistency with `gql` libraries
* We won't leave alpha until we have [full backwards compatability](https://github.com/gql-dart/gql/issues/57)

- Most links are re-exported from `graphql/client.dart`
- `QueryOptions`, `MutationOptions`, etc are turned into
[gql_exec](https://github.com/gql-dart/gql/tree/master/links/gql_exec) `Request`s
before being sent down the link chain.
- `documentNode` is deprecated in favor of `DocumentNode document` for consistency with `gql` libraries
- We won't leave alpha until we have [full backwards compatability](https://github.com/gql-dart/gql/issues/57)

```diff
final httpLink = HttpLink(
Expand Down Expand Up @@ -88,17 +90,17 @@ Subscription(

## Minor changes

* As mentioned before, `documentNode: gql(...)` is now `document: gql(...)`.
* The exported `gql` utility adds `__typename` automatically.
**If you define your own, make sure to include `AddTypenameVisitor`,
- As mentioned before, `documentNode: gql(...)` is now `document: gql(...)`.
- The exported `gql` utility adds `__typename` automatically.
\*\*If you define your own, make sure to include `AddTypenameVisitor`,
or else that your cache `dataIdFromObject` works without it

### Enums are normalized and idiomatic

```diff
- QueryResultSource.OptimisticResult
- QueryResultSource.OptimisticResult
+ QueryResultSource.optimisticResult
- QueryResultSource.Cache
- QueryResultSource.Cache
+ QueryResultSource.cache
// etc

Expand All @@ -108,10 +110,10 @@ Subscription(
+ QueryLifecycle.sideEffectsPending
```


### `client.fetchMore` (experimental)

The `fetchMore` logic is now available for when one isn't using `watchQuery`:

```dart
/// Untested example code
class MyQuery {
Expand Down
21 changes: 17 additions & 4 deletions packages/graphql/lib/src/cache/hive_store.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'package:meta/meta.dart';

import 'package:hive/hive.dart';
Expand All @@ -6,14 +7,26 @@ import './store.dart';

@immutable
class HiveStore extends Store {
/// Default box name for the `graphql/client.dart` cache store (`graphqlClientStore`)
static const defaultBoxName = 'graphqlClientStore';

/// Opens a box. Convenience pass through to [Hive.openBox].
///
/// If the box is already open, the instance is returned and all provided parameters are being ignored.
static final openBox = Hive.openBox;

/// Create a [HiveStore] with a [Box] with the given [boxName] (defaults to [defaultBoxName])
/// box from [openBox(boxName)]
static Future<HiveStore> open([
String boxName = defaultBoxName,
]) async =>
HiveStore(await openBox(boxName));

@protected
final Box box;

/// Creates a HiveStore inititalized with [box],
/// which defaults to `Hive.box('defaultGraphqlStore')`
HiveStore([
Box box,
]) : box = box ?? Hive.box('defaultGraphqlStore');
HiveStore(this.box);

@override
Map<String, dynamic> get(String dataId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql_flutter/test/widgets/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void main() {
);
client = ValueNotifier(
GraphQLClient(
cache: GraphQLCache(store: HiveStore()),
cache: GraphQLCache(store: await HiveStore.open()),
link: httpLink,
),
);
Expand Down

0 comments on commit 6db4677

Please sign in to comment.