Skip to content

Add Swift interceptor iOS test and allow HttpInterceptor.intercept() to throw Throwable #6403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ jobs:
with:
name: tests-integration.zip
path: diagnostics.zip

ios-tests:
runs-on: macos-14
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #v4.1.7
- uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 #v4.2.1
with:
distribution: 'temurin'
java-version: 17
- run: /Applications/Xcode_16.2.0.app/Contents/Developer/usr/bin/xcodebuild -allowProvisioningUpdates -project tests/com.apollographql.iostest/com.apollographql.iostest.xcodeproj -configuration Debug -scheme com.apollographql.iostest -sdk iphoneos -destination name='iPhone 16' test -test-timeouts-enabled YES
intellij-plugin:
if: "!startsWith(github.head_ref, 'release-')"
name: Build IntelliJ Plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.atomicfu.locks.withLock
import kotlinx.cinterop.alloc
import kotlinx.cinterop.nativeHeap
import kotlinx.cinterop.ptr
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.suspendCancellableCoroutine
import okio.Buffer
import okio.BufferedSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.apollographql.apollo.api.http.HttpMethod
import com.apollographql.apollo.api.http.HttpRequest
import com.apollographql.apollo.api.http.HttpResponse
import com.apollographql.apollo.exception.ApolloNetworkException
import kotlinx.coroutines.CancellationException
import okio.Closeable

/**
Expand All @@ -23,6 +24,7 @@ interface HttpEngine : Closeable {
*
* @throws [ApolloNetworkException] if a network error happens
*/
@Throws(ApolloNetworkException::class, CancellationException::class)
suspend fun execute(request: HttpRequest): HttpResponse

@ApolloDeprecatedSince(v4_0_0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ package com.apollographql.apollo.network.http

import com.apollographql.apollo.api.http.HttpRequest
import com.apollographql.apollo.api.http.HttpResponse
import com.apollographql.apollo.exception.ApolloException
import com.apollographql.apollo.exception.ApolloNetworkException
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CancellationException

interface HttpInterceptorChain {
/**
* @throws [ApolloNetworkException] if a network error happens
* Continues with the request and call all downstream interceptors.
*/
@Throws(Throwable::class)
suspend fun proceed(request: HttpRequest): HttpResponse
}

interface HttpInterceptor {
/**
* Intercepts the request and returns a response.
* Implementation may throw in case of error. Those errors are typically
* caught by the network transport and subsequently exposed in `ApolloResponse.exception`.
* If the exception is not an instance of [ApolloException], it will be wrapped in an
* instance of [ApolloNetworkException].
*/
@Throws(Throwable::class)
suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse

// TODO: remove dispose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.apollographql.apollo.mpp.currentTimeMillis
import com.apollographql.apollo.network.NetworkTransport
import com.benasher44.uuid.Uuid
import com.benasher44.uuid.uuid4
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
Expand Down Expand Up @@ -62,20 +63,23 @@ private constructor(
): Flow<ApolloResponse<D>> {
return flow {
val millisStart = currentTimeMillis()
var apolloException: ApolloException? = null
var throwable: Throwable? = null
val httpResponse: HttpResponse? = try {
DefaultHttpInterceptorChain(
interceptors = interceptors + engineInterceptor,
index = 0
).proceed(httpRequest)
} catch (e: ApolloException) {
apolloException = e
} catch (t: Throwable) {
if (t is CancellationException) {
throw t
}
throwable = t
null
}

val responses = when {
httpResponse == null -> {
flowOf(errorResponse(request.operation, apolloException!!))
flowOf(errorResponse(request.operation, throwable!!))
}

httpResponse.statusCode !in 200..299 && !httpResponse.isGraphQLResponse -> {
Expand Down Expand Up @@ -212,11 +216,11 @@ private constructor(
if (jsonMerger == null) {
jsonMerger = DeferredJsonMerger()
}
val merged = jsonMerger!!.merge(part)
val deferredFragmentIds = jsonMerger!!.mergedFragmentIds
val isLast = !jsonMerger!!.hasNext
val merged = jsonMerger.merge(part)
val deferredFragmentIds = jsonMerger.mergedFragmentIds
val isLast = !jsonMerger.hasNext

if (jsonMerger!!.isEmptyPayload) {
if (jsonMerger.isEmptyPayload) {
null
} else {
@Suppress("DEPRECATION")
Expand Down
Loading
Loading