For the past few days I have been trying to get the apollo-ios subscriptions to work with AppSync with no luck. I am able to fetch the schema, generate the API.swift file, and run querys but when I try to setup the WebSocketTransport no matter what I pass I am getting
{
errorType = UnsupportedOperation;
message = "unknown not supported through the realtime channel";
}
I am setting up the Apollo Client like this:
class Network {
static let shared = Network()
private lazy var cache = InMemoryNormalizedCache()
private lazy var store = ApolloStore(cache: cache)
private lazy var webSocketTransport: WebSocketTransport = {
let authDict = [
"x-api-key": "API_KEY",
"host": "<API_URL>.appsync-api.ap-northeast-1.amazonaws.com"
]
let headerData: Data = try! JSONSerialization.data(withJSONObject: authDict, options: JSONSerialization.WritingOptions.prettyPrinted)
let headerBase64 = headerData.base64EncodedString()
let payloadData = try! JSONSerialization.data(withJSONObject: [:], options: JSONSerialization.WritingOptions.prettyPrinted)
let payloadBase64 = payloadData.base64EncodedString()
let authPayload: [String : JSONEncodable] = [
"header": headerBase64,
"payload": payloadBase64
]
let url = URL(string: "wss://<API_URL>.appsync-realtime-api.ap-northeast-1.amazonaws.com/graphql?header=\(headerBase64)&payload=\(payloadBase64)")!
let request = URLRequest(url: url)
return WebSocketTransport(request: request, sendOperationIdentifiers: false, reconnectionInterval: 5)
}()
/// An HTTP transport to use for queries and mutations
private lazy var normalTransport: RequestChainNetworkTransport = {
let url = URL(string: "https://<API_URL>.appsync-api.ap-northeast-1.amazonaws.com/graphql")!
return RequestChainNetworkTransport(interceptorProvider: LegacyInterceptorProvider(store: store), endpointURL: url, additionalHeaders: ["X-Api-Key": "API_KEY"])
}()
/// A split network transport to allow the use of both of the above
/// transports through a single `NetworkTransport` instance.
private lazy var splitNetworkTransport = SplitNetworkTransport(
uploadingNetworkTransport: self.normalTransport,
webSocketNetworkTransport: self.webSocketTransport
)
private(set) lazy var apollo: ApolloClient = {
return ApolloClient(networkTransport: splitNetworkTransport, store: store)
}()
and when setting up a subscription I did:
class ViewController: UIViewController {
var subscription: Cancellable?
override func viewDidLoad() {
super.viewDidLoad()
subscription = Network.shared.apollo.subscribe(subscription: SubscribeCommentsSubscription()) { result in
switch result {
case .success(let graphQLResult):
print("Success! Result: \(graphQLResult)")
case .failure(let error):
print("Failure! Error: \(error)")
}
}
}
deinit {
// Make sure the subscription is cancelled, if it exists, when this object is deallocated.
self.subscription?.cancel()
}
}
Just to share a snippet from API.swift
"""
subscription subscribeComments {
subscribeToEventComments(eventId: "EVENT_ID") {
__typename
eventId
commentId
content
}
}
"""
A similar setup is working on Android so I am at a loss for what is not working correctly…