I am able to successfully perform all query, mutation and appsync subscription .. sample code snippet below.
android client: version: 3.8.3 com.apollographql.apollo3:apollo-runtime:3.8.3
I need to know How to dynamically change query “header” and AppSync Authorization data that the AppSyncProtocol accepts in ApolloGraphQL Kotlin Library once the Client instance is created.
FYI: https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html
I have raised the same question on client-library: https://github.com/apollographql/apollo-kotlin/issues/5879
Sample tried code.
private val authorization = mapOf(
"host" to host,
"x-api-key" to apiKey,
"productId" to productId,
"Authorization" to "Bearer $token"
)
private val header = mapOf(
"host" to host,
"productId" to productId
)
private val url = AppSyncWsProtocol.buildUrl(
baseUrl = "wss://some.server.com/graphql/realtime",
authorization = header,
payload = mapOf("payload" to "{}")
)
private val apolloClient = ApolloClient.Builder()
.networkTransport(
HttpNetworkTransport.Builder()
.serverUrl("https://some.server.com/graphql")
.httpEngine(DefaultHttpEngine(okHttpClientBuilder.build()))
.build()
)
.subscriptionNetworkTransport(
WebSocketNetworkTransport.Builder()
.serverUrl("url")
.addHeader("host", "some.server.com")
.addHeader("x-api-key", apiKey)
.protocol(AppSyncWsProtocol.Factory(authorization = "authorization"))
.okHttpClient(okHttpClientBuilder.build())
.build()
)
.build()
Here productId is associated with the selected item that user chosen, and I need to listen for the updates for this productId. need to pass the productId both in Appsyncprotocl and in url “header=” . below code worked for onetime, I dont see an option to update/replace with new subscriptionNetworkTransport in client instance.
apolloClient.subscription(SampleProductSubscription(productId))
.toFlow()
.catch { exception ->
println("Test Subscription Error: ${exception.cause}")
}.retryWhen { cause, attempt ->
if (cause is SubscriptionOperationException) {
Log.e("Test", cause.printStackTrace().toString())
} else {
println("Test ${cause.printStackTrace()} $attempt")
}
true
}.collect { subscriptionData ->
println("Test SubscriptionData Received: ${subscriptionData.data}")
}
Now the scenario is that user can select a different product, Then the productId gets changed. my question is How to dynamically pass this updated productId and listen to the changes without re-creating the ApolloClient from step 2.