How to change AWS AppSyncWsProtocol authorization, header in URL dynamically for already created Client Instance

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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()
</code>
<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() </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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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}")
}
</code>
<code>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}") } </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật