Skip to content

Commit e888051

Browse files
BoDmartinbonnin
andauthored
Document @stream directive support (#6753)
* Document `@stream` directive support in defer.mdx Added support for the `@stream` directive and updated incremental delivery protocol information. * Apply suggestion from @BoD * Mention `@stream` in title and add support matrix * Update docs/source/_sidebar.yaml Co-authored-by: Martin Bonnin <[email protected]> --------- Co-authored-by: Martin Bonnin <[email protected]>
1 parent 1134af2 commit e888051

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

docs/source/_sidebar.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ items:
5858
href: ./essentials/custom-scalars
5959
- label: Fragments
6060
href: ./essentials/fragments
61-
- label: "@defer support"
61+
- label: "@defer and @stream"
6262
href: ./fetching/defer
6363
- label: Persisted queries
6464
href: ./advanced/persisted-queries

docs/source/fetching/defer.mdx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: 'Using the @defer directive in Apollo Kotlin'
2+
title: 'Using the @defer and @stream directives in Apollo Kotlin'
33
description: 'Fetch slower schema fields asynchronously'
44
---
55

66
<Note>
77

8-
The incremental delivery format used by `@defer` [isn't yet standardized](https://github.com/graphql/defer-stream-wg). Apollo Kotlin supports [the format implemented by Apollo Router](https://www.apollographql.com/docs/graphos/routing/operations/defer#specification-status), which is described in this [specification](https://specs.apollo.dev/incremental/v0.1/).
8+
The incremental delivery format used by `@defer`/`@stream` [isn't yet standardized](https://github.com/graphql/defer-stream-wg). Apollo Kotlin supports [the format implemented by Apollo Router](https://www.apollographql.com/docs/graphos/routing/operations/defer#specification-status), which is described in this [specification](https://specs.apollo.dev/incremental/v0.1/), and the more recent format described in this [specification](https://specs.apollo.dev/incremental/v0.2/)
99

1010
</Note>
1111

@@ -59,6 +59,39 @@ Received: Person(id=1, firstName=John, lastName=Doe, onUser=null))
5959
Received: Person(id=1, firstName=John, lastName=Doe, onUser=OnUser(friends=[Friend(id=2), Friend(id=3)]))
6060
```
6161

62+
### `@stream` directive support
63+
Similarly to `@defer`, the `@stream` directive can be used to receive the first few items of **lists** in the initial response, while the remaining items arrive later.
64+
65+
Note: this directive is only supported when using the [v0.2](https://www.apollographql.com/docs/kotlin/kdoc/apollo-runtime/com.apollographql.apollo.network/-incremental-delivery-protocol/-v0_2/index.html) protocol:
66+
67+
```kotlin
68+
apolloClient = ApolloClient.Builder()
69+
.networkTransport(
70+
HttpNetworkTransport.Builder()
71+
.serverUrl("https://...")
72+
// Configure the incremental v0.2 protocol // highlight-line
73+
.incrementalDeliveryProtocol(IncrementalDeliveryProtocol.V0_2) // highlight-line
74+
.build()
75+
)
76+
.build()
77+
```
78+
79+
For example this query:
80+
```graphql
81+
query FriendsQuery {
82+
friends @stream(initialCount: 2) {
83+
firstName
84+
}
85+
}
86+
```
87+
88+
Will print something like this:
89+
90+
```
91+
Received: friends=[Person(firstName=John), Person(firstName=Jane)]
92+
Received: friends=[Person(firstName=John), Person(firstName=Jane), Person(firstName=Michael), Person(firstName=Patricia), Person(firstName=James)]
93+
```
94+
6295
### Error handling
6396
When using `@defer`, the incremental payloads received from the server may each contain GraphQL errors related to the fields being returned.
6497
These errors are accumulated and exposed in the [`ApolloResponse.errors`](https://apollographql.github.io/apollo-kotlin/kdoc/apollo-api/com.apollographql.apollo.api/-apollo-response/errors.html) field.
@@ -67,6 +100,15 @@ Fetch errors like network failures can also happen during collection of the flow
67100

68101
See also the [error handling section](../essentials/errors).
69102

103+
### Support matrix
104+
105+
| Protocol | Compatible servers | Supported directives |
106+
|----------|------------------------------|----------------------|
107+
| v0.1 | Apollo Router, Apollo Server | `@defer` |
108+
| v0.2 | Apollo Server | `@defer`, `@stream` |
109+
110+
111+
70112
### Limitations/known issues
71113
* `@defer` cannot be used with `responseBased` codegen.
72114
* Some servers might send an empty payload to signal the end of the stream. In such a case you will receive an extra terminal emission. You can filter it out by using `distinctUntilChangedBy()`:
@@ -76,4 +118,3 @@ apolloClient.query(MyQuery()).toFlow()
76118
.distinctUntilChangedBy { it.data } // filter out duplicates
77119
.collect { /* ... */ }
78120
```
79-

0 commit comments

Comments
 (0)