You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: docs/customization.md
+32-19
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,11 @@ id: customization
3
3
title: Customizing the Client
4
4
---
5
5
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).
7
7
8
8
A TypedLink is simply a class with a `request()` method which returns a Stream of `OperationResponse`s.
9
9
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):
11
11
12
12
```dart
13
13
import 'package:ferry_exec/ferry_exec.dart';
@@ -37,11 +37,11 @@ class CacheTypedLink extends TypedLink {
37
37
}
38
38
```
39
39
40
-
## Forwarding `TypedLink`s
40
+
## Forwarding TypedLinks
41
41
42
42
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.
43
43
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.
45
45
46
46
```dart
47
47
import 'dart:async';
@@ -77,11 +77,11 @@ class AddTypenameTypedLink extends TypedLink {
77
77
}
78
78
```
79
79
80
-
## Chaining `TypedLink`s Together
80
+
## Chaining TypedLinks Together
81
81
82
82
TypedLinks can be chained together using the `TypedLink.from()` method.
83
83
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:
85
85
86
86
```dart
87
87
final myTypedLink = TypedLink.from([
@@ -90,34 +90,47 @@ final myTypedLink = TypedLink.from([
90
90
]);
91
91
```
92
92
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
-
99
93
You can then use this link to execute requests by listening to the Stream returned by the `request()` method:
The ferry Client is created by composing these core `TypedLink`s.
108
100
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.
111
124
-**CacheTypedLink**: A terminating link that fetches the operation from the Cache.
112
125
-**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`.
114
127
-**OptimisticTypedLink**: Returns the response stream from the next link in the chain, immediately emitting a response with the optimistic data.
115
128
-**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
117
130
1. an optimistic response is received
118
131
2. the first time a non-optimistic response is received
119
132
120
-
## Additional `TypedLink`s
133
+
## Additional TypedLinks
121
134
122
135
Ferry includes some additional `TypedLink`s that are not included in the default client. You can use these to compose your own client.
0 commit comments