:wave: Having v4 exception handling in mind - is t...
# apollo-kotlin
m
👋 Having v4 exception handling in mind - is there a new recommended way of falling back to a non-APQ request if server doesn't support APQ? In v3 one could catch
AutoPersistedQueriesNotSupported
exception and gracefully recover from it, like Martin suggested. For latest v4 beta the simplest thing I'm able to think of is to resemble v3's behavior by doing something like:
Copy code
chain.proceed(request)
    .onEach { response ->
        (response.exception as? AutoPersistedQueriesNotSupported)?.let { throw it }
    }
    .catch { throwable ->
        if(throwable is AutoPersistedQueriesNotSupported) {
            emitAll(chain.proceed(request.newBuilder().enableAutoPersistedQueries(false).build())
        } else {
            throw throwable
        }
    }
but maybe there are better alternatives now, not requiring artificial exception being thrown?
b
essentially the same thing, but instead of throwing, you could map the response, something like:
Copy code
return chain.proceed(request)
            .flatMapConcat { response ->
                if (response.exception is AutoPersistedQueriesNotSupported) {
                    chain.proceed(request.newBuilder().enableAutoPersistedQueries(false).build())
                } else {
                    flowOf(response)
                }
            }
or without flatMapConcat (needs opt-in):
Copy code
return flow {
            chain.proceed(request).collect { response ->
                if (response.exception is AutoPersistedQueriesNotSupported) {
                    emitAll(chain.proceed(request.newBuilder().enableAutoPersistedQueries(false).build()))
                } else {
                    emit(response)
                }
            }
        }
m
If I'm not mistaken the
flatMapConcat
approach assumes the upstream will not emit any subsequent updates and will complete immediately. If by any chance you decided to change the implementation and not complete the flow this wouldn't work. But I guess it's safe to assume this is the contract 👀 Thanks 👍
b
Yes, good point! Yes this should be the case but it also may be modified with custom interceptors downstream. But overall it makes sense to expect such an exception to be the last emission.
👍 1
gratitude thank you 1