Skip to content

Commit 555f8b3

Browse files
authored
[📚docs] revisit docs 3/3 Testing + Advanced (#6096)
* rework the "testing" section * link to nullability higher up
1 parent a18a291 commit 555f8b3

File tree

7 files changed

+42
-61
lines changed

7 files changed

+42
-61
lines changed

docs/source/advanced/experimental-websockets.mdx

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,4 @@ The above uses the default retry algorithm:
111111
* Wait until the network is available if you configured a [NetworkMonitor](network-connectivity).
112112
* Or use exponential backoff else.
113113

114-
To customize the retry logic more, use `retryOnErrorInterceptor`:
115-
116-
```kotlin
117-
val apolloClient = ApolloClient.Builder()
118-
.retryOnErrorInterceptor(MyRetryOnErrorInterceptor())
119-
.build()
120-
121-
class MyRetryOnErrorInterceptor : ApolloInterceptor {
122-
object RetryException : Exception()
123-
124-
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
125-
var attempt = 0
126-
return chain.proceed(request).onEach {
127-
if (request.retryOnError == true && it.exception != null && it.exception is ApolloNetworkException) {
128-
throw RetryException
129-
} else {
130-
attempt = 0
131-
}
132-
}.retryWhen { cause, _ ->
133-
if (cause is RetryException) {
134-
attempt++
135-
delay(2.0.pow(attempt).seconds)
136-
true
137-
} else {
138-
// Not a RetryException, probably a programming error, pass it through
139-
false
140-
}
141-
}
142-
}
143-
}
144-
```
114+
To customize the retry logic more, see the [network monitoring page](../advanced/network-connectivity#customizing-the-retry-algorithm).

docs/source/advanced/network-connectivity.mdx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,34 @@ When a `NetworkMonitor` is configured, `retryOnError` uses `NetworkMonitor.waitF
5454

5555
### Customizing the retry algorithm
5656

57-
You can customize the retry algorithm further by defining you own interceptor:
57+
You can customize the retry algorithm further by defining your own interceptor:
5858

5959
```kotlin
60-
internal class MyRetryInterceptor(private val networkMonitor: NetworkMonitor?): ApolloInterceptor {
60+
val apolloClient = ApolloClient.Builder()
61+
.retryOnErrorInterceptor(MyRetryOnErrorInterceptor())
62+
.build()
63+
64+
class MyRetryOnErrorInterceptor : ApolloInterceptor {
65+
object RetryException : Exception()
66+
6167
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
62-
// Disable Apollo's built-in retry mechanism
63-
val newRequest = request.newBuilder().retryOnError(false).build()
64-
return chain.proceed(newRequest)
65-
.onEach {
66-
if (it.exception != null && it.exception.shouldRetry()) {
67-
throw RetryException
68-
}
69-
}.retryWhen { cause, _->
70-
if (cause is RetryException) {
71-
// Add your logic here
72-
true
73-
} else {
74-
// Programming error, re-throw it
75-
false
76-
}
77-
}
68+
var attempt = 0
69+
return chain.proceed(request).onEach {
70+
if (request.retryOnError == true && it.exception != null && it.exception is ApolloNetworkException) {
71+
throw RetryException
72+
} else {
73+
attempt = 0
74+
}
75+
}.retryWhen { cause, _ ->
76+
if (cause is RetryException) {
77+
attempt++
78+
delay(2.0.pow(attempt).seconds)
79+
true
80+
} else {
81+
// Not a RetryException, probably a programming error, pass it through
82+
false
83+
}
84+
}
7885
}
7986
}
8087
```

docs/source/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
"Batching operations": "/advanced/query-batching"
6363
},
6464
"Development & Testing": {
65-
"Android Studio plugin": "/testing/android-studio-plugin",
6665
"Testing overview": "/testing/overview",
6766
"Mocking HTTP responses": "/testing/mocking-http-responses",
6867
"Mocking GraphQL responses": "/testing/mocking-graphql-responses",
6968
"Data builders": "/testing/data-builders",
69+
"Android Studio plugin": "/testing/android-studio-plugin",
7070
"Apollo Debug Server": "/testing/apollo-debug-server"
7171
},
7272
"Advanced": {

docs/source/essentials/errors.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
title: Error handling
33
---
44

5+
<Note>
6+
7+
Starting with version 4.0.0, Apollo Kotlin offers a way to handle errors automatically at parsing time. Fields that are nullable only for error purposes can also be generated as non-null in Kotlin. Read ["handling nullability"](../advanced/nullability) for more details.
8+
9+
</Note>
10+
511
## `ApolloResponse`
612

713
Use `ApolloResponse.data` to check if the server returned data:
@@ -130,9 +136,4 @@ For an example, it is possible for a person to not have a starship:
130136
}
131137
```
132138

133-
In that case, `starship` is a true null and not an error.
134-
135-
### Handling GraphQL errors at parsing time
136-
137-
Because making the difference between true nulls and error nulls is cumbersome, Apollo Kotlin offers a way to handle errors automatically at parsing time. Fields that are nullable only for error purposes can also be generated as non-null in Kotlin. Read ["handling nullability"](../advanced/nullability) for more details.
138-
139+
In that case, `starship` is a true null and not an error.

docs/source/testing/mocking-graphql-responses.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ title: Mocking GraphQL responses
1010

1111
`QueueTestNetworkTransport` is a high-level test API that lets you specify the GraphQL responses that are returned by your `ApolloClient` instance.
1212

13-
> See also [`MockServer`](./mocking-http-responses), which creates a full HTTP server and helps test specific server behaviors, such as error cases, HTTP headers, and timeouts.
14-
1513
Add the dependency to your project's `build.gradle` file:
1614

1715
```kotlin title="build.gradle[.kts]"

docs/source/testing/mocking-http-responses.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ title: Mocking HTTP responses (experimental)
1010

1111
`MockServer` implements an HTTP server that you can use to mock responses. It's useful for testing specific server behaviors, such as error cases, HTTP headers, and timeouts. Using it requires minimal changes to your production code, because you only need to change your `serverUrl`.
1212

13-
> See also [`TestNetworkTransport`](./mocking-graphql-responses), which handles mocking at the GraphQL level instead of the HTTP level.
13+
14+
<Note>
15+
16+
Apollo Kotlin MockServer is developed in a separate repository. Head to https://github.com/apollographql/apollo-kotlin-mockserver for API docs and comprehensive documentation.
17+
18+
</Note>
1419

1520
Add the dependency to your project's `build.gradle` file:
1621

docs/source/testing/overview.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Apollo Kotlin provides the following tools to help with testing your code:
77

88
- [`MockServer`](./mocking-http-responses) (experimental): an HTTP server that you can use to mock any responses
99
- [`TestNetworkTransport`](./mocking-graphql-responses) (experimental): lets you specify the GraphQL responses returned by `ApolloClient`
10-
- [Test builders](./test-builders) (experimental): help instantiate your model classes by providing a DSL
10+
- [Data builders](./data-builders) (experimental): help instantiate your model classes by providing a DSL
1111

1212
`MockServer` creates a full HTTP server. It requires minimal changes to your production code (you only need to change `serverUrl`), which means you can use the same `ApolloClient` for tests and production. `MockServer` is also useful for testing specific server behaviors, such as error cases, HTTP headers, and timeouts.
1313

14-
`TestNetworkTransport` bypasses HTTP calls altogether and returns predefined GraphQL responses. It requires more modifications to your production code, but you can use it in lighter tests that don't need to create a server. You can use test builders to instantiate the data in the responses.
14+
`TestNetworkTransport` bypasses HTTP calls altogether and returns predefined GraphQL responses. It requires more modifications to your production code, but you can use it in lighter tests that don't need to create a server. You can use data builders to instantiate the data in the responses.

0 commit comments

Comments
 (0)