Apollo-ios: Add a way to differentiate multiple network transports in delegate functions

Created on 14 Aug 2019  路  6Comments  路  Source: apollographql/apollo-ios

In our app, we are using 3 different endpoints and we have 3 different schemas. 2 of which require an Authorization token. But their way of sending these tokens in headers are different. We need a way to check the network transport to set the correct headers.

class GraphAPI {

    static let shared = GraphAPI()

    private lazy var defaultNetworkTransport = HTTPNetworkTransport(url: URL(string: GraphAPI.graphQLUrl)!, delegate: self)
    private lazy var authNetworkTransport = HTTPNetworkTransport(url: URL(string: GraphAPI.authGraphQLUrl)!)
    private lazy var sixNetworkTransport = HTTPNetworkTransport(url: URL(string: GraphAPI.sixGraphQLUrl)!, delegate: self)

    private(set) lazy var client = ApolloClient(networkTransport: self.defaultNetworkTransport)
    private(set) lazy var authClient = ApolloClient(networkTransport: self.authNetworkTransport)
    private(set) lazy var sixClient = ApolloClient(networkTransport: self.sixNetworkTransport)

}

extension GraphAPI: HTTPNetworkTransportPreflightDelegate {

    func networkTransport(_ networkTransport: HTTPNetworkTransport, shouldSend request: URLRequest) -> Bool {
        return true
    }

    func networkTransport(_ networkTransport: HTTPNetworkTransport, willSend request: inout URLRequest) {
        var headers = request.allHTTPHeaderFields ?? [String: String]()

        // Somehow we need to compare network transports here
        // Maybe enable access for `url` so that we can do something like
        //
        // if networkTransport.url == defaultNetworkTransport.url {
        //     headers["Authorization"] = "Bearer \(TokenManager.shared.token!)"
        // } else if networkTransport.url == sixNetworkTransport.url {
        //.    headers["Authorization"] = "JWT \(TokenManager.shared.token!)"
        // }


        if let token = TokenManager.shared.token {
            headers["Authorization"] = "Bearer \(token)"
        }

        request.allHTTPHeaderFields = headers
    }

}
enhancement

Most helpful comment

^ ah yeah I think that compares the instances. You can also do that, but it certainly doesn't hurt to have the equatable conformance.

All 6 comments

You should be able to just compare like this:

if networkTransport == defaultNetworkTransport || networkTransport == sixNetworkTransport {
        headers["Authorization"] = "JWT \(TokenManager.shared.token!)"
} 

Is that not working?

Nope.

Binary operator '==' cannot be applied to two 'HTTPNetworkTransport' operands

Ergh, I thought you got that automatically with classes, but I was probably thinking of NSObject subclasses. I'll add some kind of Equatable conformance.

I was able to achieve this with strict comparing, using '===' instead '=='

^ ah yeah I think that compares the instances. You can also do that, but it certainly doesn't hurt to have the equatable conformance.

Equatable conformance has shipped with 0.15.0.

Was this page helpful?
0 / 5 - 0 ratings