无法连接到 Hasura PostGreSQL docker 容器的 Web 套接字

Cant connect to web socket of Hasura PostGreSQL docker container

我们需要一个可以使用 docker 部署的实时数据库。

我找到了一个 Hasura/PostGreSQL docker 容器,看起来我们可以将其用于我们的目的: https://docs.hasura.io/1.0/graphql/manual/getting-started/docker-simple.html

我发现的一件事是文档中的 URL 是错误的。是 http://localhost:8080/v1/graphql 而不是 http://localhost:8080/graphql

但我似乎无法从我的订阅中获得任何结果... 我在包的 SplitNetworkTransport.swift

中遇到 BAD_ACCESS 崩溃

我是不是漏掉了什么?

我的 Apollo 客户端代码如下所示:

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)
    }()
}

我这样称呼它:

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。我正在使用 Xcode 版本 11.2.1 (11B500)、swift 5.1、Apollo swift 包 0.20.0 和 docker-compose.yaml 来自 URL 在上面链接的 Hasura 文档中。

解决方法是不使用 swift 包管理器,而是使用 COCOAPODS。