Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 638512e

Browse files
committed
add note about TypedLink ordering
1 parent 82c9a2b commit 638512e

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

docs/customization.md

+32-19
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ id: customization
33
title: Customizing the Client
44
---
55

6-
Ferry is built to be modular and extensible. Ferry's core features are implemented as composable `TypedLink`s, a typed version of the [gql_link](https://pub.dev/packages/gql_link) API. In fact, the Ferry Client itself is a `TypedLink`, created by composing several other [core links](#core-typedlinks).
6+
Ferry is built to be modular and extensible. Ferry's core features are implemented as composable `TypedLink`s, a typed version of the [gql_link](https://pub.dev/packages/gql_link) API. In fact, the Ferry Client itself is a TypedLink, created by composing several other [core links](#core-typedlinks).
77

88
A TypedLink is simply a class with a `request()` method which returns a Stream of `OperationResponse`s.
99

10-
For example, here's the link that Ferry uses internally to resolve requests from the `ferry_cache`:
10+
For example, here's the link that Ferry uses internally to resolve requests from the [ferry_cache](cache-configuration.md):
1111

1212
```dart
1313
import 'package:ferry_exec/ferry_exec.dart';
@@ -37,11 +37,11 @@ class CacheTypedLink extends TypedLink {
3737
}
3838
```
3939

40-
## Forwarding `TypedLink`s
40+
## Forwarding TypedLinks
4141

4242
Not all links need to resolve the request directly. Some links simply modify the request or response stream by calling the `forward()` callback on the `request()` method. This defers resolution of the request to the next link in the chain.
4343

44-
For example, ferry uses the following link to automatically add `__typename` fields to each node of the GraphQL operation.
44+
For example, ferry uses the following link to automatically add "___typename_" fields to each node of the GraphQL operation.
4545

4646
```dart
4747
import 'dart:async';
@@ -77,11 +77,11 @@ class AddTypenameTypedLink extends TypedLink {
7777
}
7878
```
7979

80-
## Chaining `TypedLink`s Together
80+
## Chaining TypedLinks Together
8181

8282
TypedLinks can be chained together using the `TypedLink.from()` method.
8383

84-
The following will create a link that adds `__typename` fields to the request, then resolves the request from the Cache:
84+
The following will create a link that adds "___typename_" fields to the request, then resolves the request from the Cache:
8585

8686
```dart
8787
final myTypedLink = TypedLink.from([
@@ -90,34 +90,47 @@ final myTypedLink = TypedLink.from([
9090
]);
9191
```
9292

93-
:::warning
94-
95-
The final link in the chain must be a "terminating" link, i.e. a link that does not forward the request down the chain.
96-
97-
:::
98-
9993
You can then use this link to execute requests by listening to the Stream returned by the `request()` method:
10094

10195
```dart
10296
myTypedLink.request(GMyQueryReq()).listen((data) => print(data));
10397
```
10498

105-
## Core `TypedLink`s
10699

107-
The ferry Client is created by composing these core `TypedLink`s.
108100

109-
- **Client**: The Ferry Client itself is a `TypedLink`, implemented by composing other core `TypedLink`s.
110-
- **AddTypenameTypedLink**: Adds `__typename` to each node of the operation.
101+
### Ordering Your TypedLink Chains
102+
103+
There are a few important guidelines that you should follow when chaining TypedLinks to ensure that your requests get executed correctly.
104+
105+
1. If you are using a link that originates requests (such as _RequestControllerTypedLink_), it should be the first link in the chain.
106+
2. The final link in the chain must be a "terminating" link, i.e. a link that does not forward the request down the chain.
107+
108+
:::warning
109+
110+
## Don't Chain the Ferry Client Directly
111+
112+
You generally shoudn't chain the Ferry Client together with other TypedLinks. Since the Client's composition chain starts with a _RequestControllerTypedLink_ and ends with a terminating link, adding a link before or after it would violate the above [guidelines](#ordering-your-typedlink-chains) and lead to unexpected results.
113+
114+
Instead, you can simply use the [underlying links that Client composes](https://github.com/gql-dart/ferry/blob/5a7defb522b3ea64327602f06d6d652f31eceb20/ferry/lib/ferry.dart#L39), adding your custom link to compose your own custom chain.
115+
116+
:::
117+
118+
## Core TypedLinks
119+
120+
The ferry Client is created by composing these core TypedLinks.
121+
122+
- **Client**: The Ferry Client itself is a TypedLink, implemented by composing other core TypedLinks.
123+
- **AddTypenameTypedLink**: Adds "___typename_" to each node of the operation.
111124
- **CacheTypedLink**: A terminating link that fetches the operation from the Cache.
112125
- **FetchPolicyTypedLink**: A terminating link that resolves an operation from the Link or the Cache based on the request's `FetchPolicy`, possibly caching the response.
113-
- **GqlTypedLink**: A terminating link which defers execution to the provided `gql_link`. Any errors received by the `gql_link` are included in the `OperationResponse.linkException`.
126+
- **GqlTypedLink**: A terminating link which defers execution to the provided [gql_link](https://pub.dev/packages/gql_link). Any errors received by the [gql_link](https://pub.dev/packages/gql_link) are included in the `OperationResponse.linkException`.
114127
- **OptimisticTypedLink**: Returns the response stream from the next link in the chain, immediately emitting a response with the optimistic data.
115128
- **RequestControllerTypedLink**: Allows multiple requests to be made by adding requests to the `requestController`.
116-
- **UpdateCacheTypedLink**: Runs any specified [UpdateCacheHandler]s with a [CacheProxy] when
129+
- **UpdateCacheTypedLink**: Runs any specified `UpdateCacheHandler`s with a `CacheProxy` when
117130
1. an optimistic response is received
118131
2. the first time a non-optimistic response is received
119132

120-
## Additional `TypedLink`s
133+
## Additional TypedLinks
121134

122135
Ferry includes some additional `TypedLink`s that are not included in the default client. You can use these to compose your own client.
123136

0 commit comments

Comments
 (0)