We need a realtime database which we can deploy using docker.
I found a Hasura/PostGreSQL docker container which looks like we can use it for our purpose: https://docs.hasura.io/1.0/graphql/manual/getting-started/docker-simple.html
One thing I figured out was that the URL in the documentation was wrong. It's http://localhost:8080/v1/graphql
and not http://localhost:8080/graphql
.
But I can't seem to get any results from my subscription... I get a BAD_ACCESS crash in the package's SplitNetworkTransport.swift
Am I missing something?
My Apollo client code looks like this:
import Apollo
import ApolloWebSocket
class Apollo {
static let shared = Apollo()
let client: ApolloClient = {
let authPayloads = [ "x-hasura-admin-secret": "secret" ]
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = authPayloads
let wsEndpointURL = URL(string: "ws://localhost:8080/v1alpha1/graphql")!
let endpointURL = URL(string: "http://localhost:8080/v1alpha1/graphql")!
let map: GraphQLMap = authPayloads
let websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map)
let httpNetworkTransport = HTTPNetworkTransport( url: endpointURL, session: URLSession(configuration: configuration))
let splitTransport = SplitNetworkTransport(httpNetworkTransport: httpNetworkTransport, webSocketNetworkTransport: websocket)
return ApolloClient(networkTransport: splitTransport)
}()
}
And I'm calling it as follows:
import UIKit
import Apollo
class ViewController: UIViewController {
private var subscription: Cancellable?
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.subscription = Apollo.shared.client.subscribe(subscription: MySubscriptionSubscription()) { [weak self] result in
guard let this = self else { return }
switch result {
case .success(let graphQLResult):
if let data = graphQLResult.data {
this.label.text = "data"
print("Simon Says data: \(data)")//TODO: Remove
}
case .failure(let error):
this.label.text = "error"
print("Simon Says error: \(error)")//TODO: Remove
}
}
}
deinit {
self.subscription?.cancel()
}
}
P.s. I'm using Xcode Version 11.2.1 (11B500), swift 5.1, Apollo swift package 0.20.0 and the docker-compose.yaml from the URL in the Hasura docs linked above.