apollo-kotlin: Using WebSocketNetworkTransport throws `IllegalArgumentException: Flow has more than one element`
Version
3.8.5
Summary
We are currently working on a migration from Apollo2 to Apollo3 and see a bunch of IllegalArgumentException: Flow has more than one element thrown when receiving updates through webSocket.
ApolloClient builder code below
ApolloClient.Builder().apply {
serverUrl(baseUrl)
httpEngine(DefaultHttpEngine(newOkHttpClient))
dispatcher(defaultDispatcher())
normalizedCache(
normalizedCacheFactory = cacheFactory,
cacheResolver = resolver,
cacheKeyGenerator = generator,
)
webSocketUrl?.let { url ->
subscriptionNetworkTransport(WebSocketNetworkTransport.Builder()
.serverUrl(url)
.okHttpClient(newOkHttpClient)
.protocol(
SubscriptionWsProtocol.Factory(
connectionPayload = {
authorizationHeaders()
}
)
)
.reopenWhen { throwable, attempt ->
if (throwable is DisableSubscriptionException) {
reconnectSignalChannel.receive()
}
true
}
.build())
}
autoPersistedQueries(enableByDefault = enablePersistedQueries)
customTypeAdapters?.forEach { (scalarType, customTypeAdapter) ->
addCustomTypeAdapter(scalarType, customTypeAdapter)
}
}.build()
I found an old issue related to the same exception, when I remove autoPersistedQueries I don’t see the exception, not sure if this is a configuration issue on our end or a bug in the library.
Steps to reproduce the behavior
No response
Logs
java.lang.IllegalArgumentException: Flow has more than one element
at kotlinx.coroutines.flow.FlowKt__ReduceKt$single$2.emit(Reduce.kt:54)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$$inlined$filterNot$1$2.emit(Emitters.kt:223)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$$inlined$map$1$2.emit(Emitters.kt:223)
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:11)
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:11)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:113)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:82)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$3.invokeSuspend(WebSocketNetworkTransport.kt:298)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$3.invoke(Unknown Source:13)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$3.invoke(Unknown Source:6)
at com.apollographql.apollo3.internal.FlowsKt$transformWhile$1$invokeSuspend$$inlined$collectWhile$1.emit(flows.kt:53)
at com.apollographql.apollo3.internal.FlowsKt$transformWhile$1$invokeSuspend$$inlined$collectWhile$2.emit(flows.kt:38)
at com.apollographql.apollo3.network.ws.WebSocketNetworkTransport$execute$$inlined$filter$1$2.emit(Emitters.kt:223)
at kotlinx.coroutines.flow.SubscribedFlowCollector.emit(Unknown Source:2)
at kotlinx.coroutines.flow.SharedFlowImpl.collect$suspendImpl(SharedFlow.kt:397)
at kotlinx.coroutines.flow.SharedFlowImpl$collect$1.invokeSuspend(Unknown Source:15)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (11 by maintainers)
That’s okay we won’t need PQs with websockets as of now, as I mentioned we never had it enabled on Apollo2, it was just a question of if the need arises what should be done. Though I’ll get back to you on our backend stack will need to ask them.
Apollo 2 has never supported APQs or PQs with subscriptions. It was disabled under the hood. You could call
ApolloClient.Builder.enableAutoPersistedQueries(true)but this would have no effect on subscriptions.The “quick” fix is to do the same in Apollo 3 but since we’re on that topic, I’ll look into what it takes to add proper support, stay tuned.
No worries, APQs vs PQs is confusing.
If you’re using Apollo Router indeed, other backends may work differently. But judging by your
WebSocketNetworkTransportusage, it looks like you’re using something else? (Router uses multipart HTTP)Thanks! I can reproduce. This is indeed a subscriptions/persisted queries integration issue. Apologies for the inconvenience, I’ll make a fix for that. In the meantime, you can disable persisted queries for your subscriptions like so: