Skip to content

Commit

Permalink
refactor: ~ s/InMemoryCache/GraphQLCache/g
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed May 14, 2020
1 parent 61582b3 commit f8699ee
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 23 deletions.
13 changes: 5 additions & 8 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dev_dependencies:
To connect to a GraphQL Server, we first need to create a `GraphQLClient`. A `GraphQLClient` requires both a `cache` and a `link` to be initialized.

In our example below, we will be using the Github Public API. In our example below, we are going to use `HttpLink` which we will concatenate with `AuthLink` so as to attach our github access token. For the cache, we are going to use `InMemoryCache`.
In our example below, we will be using the Github Public API. In our example below, we are going to use `HttpLink` which we will concatenate with `AuthLink` so as to attach our github access token. For the cache, we are going to use `GraphQLCache`.

```dart
// ...
Expand All @@ -61,7 +61,7 @@ final AuthLink _authLink = AuthLink(
final Link _link = _authLink.concat(_httpLink);
final GraphQLClient _client = GraphQLClient(
cache: InMemoryCache(),
cache: GraphQLCache(),
link: _link,
);
Expand Down Expand Up @@ -201,7 +201,7 @@ if (isStarred) {
### AST documents

> We are deprecating `document` and recommend you update your application to use
`document` instead. `document` will be removed from the api in a future version.
> `document` instead. `document` will be removed from the api in a future version.

For example:

Expand All @@ -224,7 +224,7 @@ With [`package:gql_code_gen`](https://pub.dev/packages/gql_code_gen) you can par

```graphql
mutation AddStar($starrableId: ID!) {
action: addStar(input: {starrableId: $starrableId}) {
action: addStar(input: { starrableId: $starrableId }) {
starrable {
viewerHasStarred
}
Expand Down Expand Up @@ -265,14 +265,12 @@ final ErrorLink errorLink = ErrorLink(errorHandler: (ErrorResponse response) {

### `PersistedQueriesLink` (experimental)

To improve performance you can make use of a concept introduced by [Apollo](https://www.apollographql.com/) called [Automatic persisted queries](https://www.apollographql.com/docs/apollo-server/performance/apq/) (or short "APQ") to send smaller requests and even enabled CDN caching for your GraphQL API.
To improve performance you can make use of a concept introduced by [Apollo](https://www.apollographql.com/) called [Automatic persisted queries](https://www.apollographql.com/docs/apollo-server/performance/apq/) (or short "APQ") to send smaller requests and even enabled CDN caching for your GraphQL API.

**ATTENTION:** This also requires you to have a GraphQL server that supports APQ, like [Apollo's GraphQL Server](https://www.apollographql.com/docs/apollo-server/) and will only work for queries (but not for mutations or subscriptions).


You can than use it simply by prepending a `PersistedQueriesLink` to your normal `HttpLink`:


```dart
final PersistedQueriesLink _apqLink = PersistedQueriesLink(
// To enable GET queries for the first load to allow for CDN caching
Expand All @@ -286,7 +284,6 @@ final HttpLink _httpLink = HttpLink(
final Link _link = _apqLink.concat(_httpLink);
```


[build-status-badge]: https://img.shields.io/circleci/build/github/zino-app/graphql-flutter.svg?style=flat-square
[build-status-link]: https://circleci.com/gh/zino-app/graphql-flutter
[coverage-badge]: https://img.shields.io/codecov/c/github/zino-app/graphql-flutter.svg?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/example/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GraphQLClient client() {
);

return GraphQLClient(
cache: InMemoryCache(),
cache: GraphQLCache(),
link: _link,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/test/in_memory_storage_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'helpers.dart';
void main() {
group('In memory exception handling', () {
test('FileSystemException', overridePrint((List<String> log) async {
final InMemoryCache cache = InMemoryCache(
final GraphQLCache cache = GraphQLCache(
storagePrefix: Future.error(FileSystemException()),
);
await cache.restore();
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql/test/in_memory_storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final Map<String, Object> eData = <String, Object>{
void main() {
group('Normalizes writes', () {
test('.write .read round trip', () async {
final InMemoryCache cache = InMemoryCache();
final GraphQLCache cache = GraphQLCache();
cache.write(aKey, aData);
await cache.save();
cache.reset();
Expand All @@ -51,7 +51,7 @@ void main() {

test('.write avoids overriding a superset with a subset of a field (#155)',
() async {
final InMemoryCache cache = InMemoryCache();
final GraphQLCache cache = GraphQLCache();
cache.write(aKey, aData);

final Map<String, Object> anotherAData = <String, Object>{
Expand All @@ -73,7 +73,7 @@ void main() {
});

test('.write does not mutate input', () async {
final InMemoryCache cache = InMemoryCache();
final GraphQLCache cache = GraphQLCache();
cache.write(aKey, aData);
final Map<String, Object> anotherAData = <String, Object>{
'a': <String, Object>{
Expand All @@ -97,7 +97,7 @@ void main() {
});

test('saving concurrently wont error', () async {
final InMemoryCache cache = InMemoryCache();
final GraphQLCache cache = GraphQLCache();
cache.write(aKey, aData);
cache.write(bKey, bData);
cache.write(cKey, cData);
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Find the migration from version 2 to version 3 [here](./../../changelog-v2-v3.md

## Usage

To use the client it first needs to be initialized with a link and cache. For this example, we will be using an `HttpLink` as our link and `InMemoryCache` as our cache. If your endpoint requires authentication you can concatenate the `AuthLink`, it resolves the credentials using a future, so you can authenticate asynchronously.
To use the client it first needs to be initialized with a link and cache. For this example, we will be using an `HttpLink` as our link and `GraphQLCache` as our cache. If your endpoint requires authentication you can concatenate the `AuthLink`, it resolves the credentials using a future, so you can authenticate asynchronously.

> For this example we will use the public GitHub API.
Expand All @@ -75,7 +75,7 @@ void main() {
ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
cache: InMemoryCache(),
cache: GraphQLCache(),
link: link,
),
);
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql_flutter/UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
- Client(
- endPoint: 'https://api.github.com/graphql',
+ GraphQLClient(
cache: InMemoryCache(),
cache: GraphQLCache(),
- apiToken: '<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN>',
+ link: link,
),
Expand Down Expand Up @@ -103,4 +103,3 @@ Mutation(
```

That's it! You should now be able to use the latest version of our library.

2 changes: 1 addition & 1 deletion packages/graphql_flutter/example/lib/fetchmore/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FetchMoreWidgetScreen extends StatelessWidget {

final client = ValueNotifier<GraphQLClient>(
GraphQLClient(
cache: InMemoryCache(),
cache: GraphQLCache(),
link: link,
),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql_flutter/lib/graphql_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library graphql_flutter;

export 'package:graphql/client.dart'
hide InMemoryCache, NormalizedInMemoryCache, OptimisticCache;
hide GraphQLCache, NormalizedInMemoryCache, OptimisticCache;

export 'package:graphql_flutter/src/caches.dart';

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql_flutter/lib/src/caches.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:graphql/client.dart' as client;
final FutureOr<String> flutterStoragePrefix =
(() async => (await getApplicationDocumentsDirectory()).path)();

class InMemoryCache extends client.InMemoryCache {
InMemoryCache({
class GraphQLCache extends client.GraphQLCache {
GraphQLCache({
FutureOr<String> storagePrefix,
}) : super(storagePrefix: storagePrefix ?? flutterStoragePrefix);
}
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: InMemoryCache(storagePrefix: 'test'),
cache: GraphQLCache(storagePrefix: 'test'),
link: httpLink,
),
);
Expand Down

0 comments on commit f8699ee

Please sign in to comment.