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

Implement Standalone Offline Client #144

Open
wants to merge 1 commit into
base: master
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
6 changes: 5 additions & 1 deletion ferry/lib/ferry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class Client extends TypedLink {
this.updateCacheHandlers = const {},
this.defaultFetchPolicies = const {},
this.addTypename = true,
}) : cache = cache ?? Cache(),
}) : cache = cache ??
Cache(
typePolicies: typePolicies,
addTypename: addTypename,
),
requestController = requestController ?? StreamController.broadcast() {
_typedLink = TypedLink.from([
RequestControllerTypedLink(this.requestController),
Expand Down
1 change: 0 additions & 1 deletion ferry/lib/typed_links.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export 'package:ferry/src/add_typename_typed_link.dart';
export 'package:ferry/src/cache_typed_link.dart';
export 'package:ferry/src/fetch_policy_typed_link.dart';
export 'package:ferry/src/gql_typed_link.dart';
export 'package:ferry/src/offline_mutation_typed_link.dart';
export 'package:ferry/src/optimistic_typed_link.dart';
export 'package:ferry/src/request_controller_typed_link.dart';
export 'package:ferry/src/update_cache_typed_link.dart';
Expand Down
1 change: 0 additions & 1 deletion ferry/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies:
gql_exec: ^0.2.4
meta: ^1.1.8
collection: ^1.14.12
hive: ^1.4.4
built_value: ^7.1.0
ferry_exec: ^0.0.2
normalize: ^0.4.5
Expand Down
11 changes: 11 additions & 0 deletions ferry_offline_client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dart
.dart_tool
.packages
pubspec.lock

# Documentation
doc/api

build/

test/__hive_data__
13 changes: 13 additions & 0 deletions ferry_offline_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.0.1]

- initial commit

21 changes: 21 additions & 0 deletions ferry_offline_client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Sat Mandir Khalsa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions ferry_offline_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[![MIT License][license-badge]][license-link]
[![PRs Welcome][prs-badge]][prs-link]
[![Watch on GitHub][github-watch-badge]][github-watch-link]
[![Star on GitHub][github-star-badge]][github-star-link]
[![Watch on GitHub][github-forks-badge]][github-forks-link]
[![Discord][discord-badge]][discord-link]

[license-badge]: https://img.shields.io/github/license/gql-dart/ferry.svg?style=for-the-badge
[license-link]: https://github.com/gql-dart/ferry/blob/master/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=for-the-badge
[prs-link]: https://github.com/gql-dart/ferry/issues

[github-watch-badge]: https://img.shields.io/github/watchers/gql-dart/ferry.svg?style=for-the-badge&logo=github&logoColor=ffffff
[github-watch-link]: https://github.com/gql-dart/ferry/watchers
[github-star-badge]: https://img.shields.io/github/stars/gql-dart/ferry.svg?style=for-the-badge&logo=github&logoColor=ffffff
[github-star-link]: https://github.com/gql-dart/ferry/stargazers
[github-forks-badge]: https://img.shields.io/github/forks/gql-dart/ferry.svg?style=for-the-badge&logo=github&logoColor=ffffff
[github-forks-link]: https://github.com/gql-dart/ferry/network/members

[discord-badge]: https://img.shields.io/discord/559455668810153989.svg?style=for-the-badge&logo=discord&logoColor=ffffff
[discord-link]: https://discord.gg/QRTfXE

An offline-ready `ferry` GraphQL Client.
1 change: 1 addition & 0 deletions ferry_offline_client/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:pedantic/analysis_options.yaml
117 changes: 117 additions & 0 deletions ferry_offline_client/lib/ferry_offline_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:ferry_exec/ferry_exec.dart';
import 'package:gql/ast.dart';
import 'package:built_value/serializer.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive/hive.dart';
import 'package:ferry/typed_links.dart';
export 'package:ferry_cache/ferry_cache.dart';
export 'package:gql_link/gql_link.dart';
export 'package:normalize/policies.dart';
export 'package:ferry/src/update_cache_typed_link.dart' show UpdateCacheHandler;
export 'package:ferry_exec/ferry_exec.dart';
export 'package:gql/ast.dart' show OperationType;
import 'package:ferry_hive_store/ferry_hive_store.dart';

import './src/offline_mutation_typed_link.dart';

class OfflineClientConfig {
final LinkExceptionHandler linkExceptionHandler;

OfflineClientConfig({
this.linkExceptionHandler,
});
}

class OfflineClient extends TypedLink {
final Link link;
final StreamController<OperationRequest> requestController;
final Map<String, TypePolicy> typePolicies;
final Map<String, Function> updateCacheHandlers;
final Map<OperationType, FetchPolicy> defaultFetchPolicies;
final bool addTypename;
final Box storeBox;
final Box mutationQueueBox;
final Cache cache;
final OfflineClientConfig offlineConfig;
final Serializers serializers;

TypedLink _typedLink;

OfflineClient({
@required this.link,
@required this.storeBox,
@required this.mutationQueueBox,
@required this.serializers,
this.offlineConfig,
StreamController<OperationRequest> requestController,
this.typePolicies = const {},
this.updateCacheHandlers = const {},
this.defaultFetchPolicies = const {},
this.addTypename = true,
}) : cache = Cache(
store: HiveStore(storeBox),
typePolicies: typePolicies,
addTypename: addTypename,
),
requestController = requestController ?? StreamController.broadcast() {
_typedLink = TypedLink.from([
RequestControllerTypedLink(this.requestController),
OfflineMutationTypedLink(
cache: cache,
mutationQueueBox: mutationQueueBox,
serializers: serializers,
requestController: this.requestController,
linkExceptionHandler: offlineConfig?.linkExceptionHandler,
),
if (addTypename) AddTypenameTypedLink(),
if (updateCacheHandlers.isNotEmpty)
UpdateCacheTypedLink(
cache: cache,
updateCacheHandlers: updateCacheHandlers,
),
FetchPolicyTypedLink(
link: link,
cache: cache,
defaultFetchPolicies: defaultFetchPolicies,
)
]);
}

@override
Stream<OperationResponse<TData, TVars>> request<TData, TVars>(
OperationRequest<TData, TVars> request, [
forward,
]) =>
_typedLink.request(request, forward);

/// Initializes an [OfflineClient] with default hive boxes
static Future<OfflineClient> init({
@required Link link,
@required Serializers serializers,
OfflineClientConfig offlineConfig,
StreamController<OperationRequest> requestController,
Map<String, TypePolicy> typePolicies,
Map<String, Function> updateCacheHandlers,
Map<OperationType, FetchPolicy> defaultFetchPolicies,
bool addTypename,
}) async {
await Hive.initFlutter();
final storeBox = await Hive.openBox<Map<String, dynamic>>('ferry_store');
final mutationQueueBox =
await Hive.openBox<Map<String, dynamic>>('ferry_mutation_queue');
return OfflineClient(
link: link,
storeBox: storeBox,
mutationQueueBox: mutationQueueBox,
serializers: serializers,
offlineConfig: offlineConfig,
requestController: requestController,
typePolicies: typePolicies,
updateCacheHandlers: updateCacheHandlers,
defaultFetchPolicies: defaultFetchPolicies,
addTypename: addTypename,
);
}
}
22 changes: 22 additions & 0 deletions ferry_offline_client/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: ferry_offline_client
version: 0.0.1
homepage: https://ferrygraphql.com/
description: An offline-ready Ferry GraphQL Client
repository: https://github.com/gql-dart/ferry
environment:
sdk: '>=2.10.0 <3.0.0'
dependencies:
gql_link: ^0.3.0
meta: ^1.1.8
hive: ^1.4.4
hive_flutter: ^0.3.0+2
ferry_exec: ^0.0.2
ferry_cache: ^0.4.2
ferry_hive_store: ^0.3.2
ferry: ^0.9.2
flutter:
sdk: flutter
dev_dependencies:
test: ^1.9.4
pedantic: ^1.9.0
ferry_test_graphql: ^0.0.2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:rxdart/rxdart.dart';
import 'package:ferry_exec/ferry_exec.dart';
import 'package:async/async.dart';

import 'package:ferry/src/offline_mutation_typed_link.dart';
import 'package:ferry_offline_client/src/offline_mutation_typed_link.dart';

import 'package:ferry_test_graphql/schema/serializers.gql.dart';
import 'package:ferry_test_graphql/mutations/variables/create_review.req.gql.dart';
Expand Down