我正在尝试使用apollo ios客户端实现graphql订阅.但由于缺乏文档示例而无法弄明白. 在apollo文档中给出的示例是: let apollo: ApolloClient = { let configuration = URLSessionConfiguration.default // Add addit
在apollo文档中给出的示例是:
let apollo: ApolloClient = { let configuration = URLSessionConfiguration.default // Add additional headers as needed configuration.httpAdditionalHeaders = ["Authorization": "Bearer <token>"] // Replace `<token>` let url = URL(string: "http://localhost:8080/graphql")! return ApolloClient(networkTransport: HTTPNetworkTransport(url: url, configuration: configuration)) }()
APOLLO IOS GUIDE: Creating a client
按照以下步骤在apollo ios graphql客户端中实现订阅.>使用cocoapods:
> pod’Apollo’
> pod’Apollo / WebSocket’
> pod安装
>创建客户端以支持订阅和身份验证.在AppDelegate.swift中添加以下代码:
> Websocket – 我们必须使用WebSocketTransport和URLRequest
>身份验证 – 我们必须在连接参数connectionPayload和服务器之间传递auth参数.对于http,我们将在标题中传递它,如问题片段中所述.
> SplitNetworkTransport-结合http和websocket来创建客户端.我们必须使用httpNetworkTransport和webSocketNetworkTransport
lazy var apollo: ApolloClient = { let authPayloads = [ "Authorization": "Bearer " ] let configuration = URLSessionConfiguration.default configuration.httpAdditionalHeaders = authPayloadslet map: GraphQLMap = authPayloads let wsEndpointURL = URL(string: "ws://localhost:8080/subscriptions")! let endpointURL = URL(string: "http://localhost:8080/api")! let websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map) let splitNetworkTransport = SplitNetworkTransport( httpNetworkTransport: HTTPNetworkTransport( url: endpointURL, configuration: configuration ), webSocketNetworkTransport: websocket ) return ApolloClient(networkTransport: splitNetworkTransport)
}()
let map: GraphQLMap = authPayloads let wsEndpointURL = URL(string: "ws://localhost:8080/subscriptions")! let endpointURL = URL(string: "http://localhost:8080/api")! let websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map) let splitNetworkTransport = SplitNetworkTransport( httpNetworkTransport: HTTPNetworkTransport( url: endpointURL, configuration: configuration ), webSocketNetworkTransport: websocket ) return ApolloClient(networkTransport: splitNetworkTransport)