Skip to content

Commit 44f2c8d

Browse files
authored
add NetworkTransport.closeConnection() (apollographql#6105)
1 parent 3af9b79 commit 44f2c8d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

libraries/apollo-api/src/commonMain/kotlin/com/apollographql/apollo/exception/Exceptions.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ class RouterError(
7878
) : ApolloException(message = "Router error(s) (first: '${errors.firstOrNull()?.message}')")
7979

8080
/**
81-
* A WebSocket connection could not be established: e.g., expired token
81+
* A WebSocket close frame was received from the server
8282
*/
8383
class ApolloWebSocketClosedException(
8484
val code: Int,
8585
val reason: String? = null,
8686
cause: Throwable? = null,
8787
) : ApolloException(message = "WebSocket Closed code='$code' reason='$reason'", cause = cause)
8888

89+
/**
90+
* `closeConnection()` was called to force closing the websocket
91+
*/
92+
@ApolloExperimental
93+
data object ApolloWebSocketForceCloseException : ApolloException(message = "closeConnection() was called")
94+
8995
/**
9096
* The response was received but the response code was not 200
9197
*

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/network/websocket/WebSocketNetworkTransport.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.apollographql.apollo.api.json.ApolloJsonElement
99
import com.apollographql.apollo.api.json.jsonReader
1010
import com.apollographql.apollo.api.toApolloResponse
1111
import com.apollographql.apollo.exception.ApolloException
12+
import com.apollographql.apollo.exception.ApolloWebSocketForceCloseException
1213
import com.apollographql.apollo.exception.DefaultApolloException
1314
import com.apollographql.apollo.exception.SubscriptionOperationException
1415
import com.apollographql.apollo.internal.DeferredJsonMerger
@@ -279,7 +280,7 @@ private fun Map<String, Any?>.isDeferred(): Boolean {
279280
*
280281
* [exception] is passed down to [ApolloResponse.exception] so you can decide how to handle the exception for active subscriptions.
281282
*
282-
* ```
283+
* ```kotlin
283284
* apolloClient.subscriptionNetworkTransport.closeConnection(DefaultApolloException("oh no!"))
284285
* ```
285286
*
@@ -292,3 +293,15 @@ fun NetworkTransport.closeConnection(exception: ApolloException) {
292293

293294
webSocketNetworkTransport.closeConnection(exception)
294295
}
296+
297+
/**
298+
* Closes the websocket connection if the transport is a [WebSocketNetworkTransport].
299+
*
300+
* A response is emitted with [ApolloResponse.exception] set to [ApolloWebSocketForceCloseException].
301+
*/
302+
@ApolloExperimental
303+
fun NetworkTransport.closeConnection() {
304+
val webSocketNetworkTransport = (this as? WebSocketNetworkTransport) ?: throw IllegalArgumentException("'$this' is not an instance of com.apollographql.apollo.websocket.WebSocketNetworkTransport")
305+
306+
webSocketNetworkTransport.closeConnection(ApolloWebSocketForceCloseException)
307+
}

0 commit comments

Comments
 (0)