简体   繁体   中英

How to add header in Apollo GraphQL 0.49.1 for iOS

i am using this code but this code gives me error like

  1. Cannot find 'HTTPNetworkTransport' in scope
  1. Cannot find type 'HTTPNetworkTransportDelegate' in scope
final class Network {
    static let shared = Network()
    private lazy var networkTransport: NetworkTransport = {
    
        let transport = HTTPNetworkTransport(url: URL(string: "https://exampe.com/grapghql")!)
        transport.delegate = self
    
        return transport
   }()

   private(set) lazy var apollo = ApolloClient(networkTransport: self.networkTransport)
}

extension Network: HTTPNetworkTransportDelegate {
    func networkTransport(_ networkTransport: NetworkTransport, shouldSend request: URLRequest) -> Bool {
        return true
    }

    func networkTransport(_ networkTransport: NetworkTransport, willSend request: inout URLRequest) {
    
       let token = ""
       var headers = request.allHTTPHeaderFields ?? [String: String]()
       headers["Authorization"] = "Bearer \(token)"
    
       request.allHTTPHeaderFields = headers
   }
}

Looks like you're using some code from an older version of the Apollo lib. Here's the doc for the newer setu p - the short version is that you're going to want to use an interceptor to add the headers.

Answer from this issue -> https://github.com/apollographql/apollo-ios/issues/2019

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM