Skip to content

How to modify URLSessionConfiguration on ApolloClient #922

@jordanhbuiltbyhq

Description

@jordanhbuiltbyhq

With the recent change resolving retain cycles our ApolloClient is going out of scope and being deallocated. We were using a computed property on our singleton class because we specify an x-auth-token in the URLSessionConfiguration 's httpAdditionalHeaders which can change throughout the lifecycle of the app. If they log out and log into a different account we can't continue using the same x-auth-token in the ApolloClient, so it was easy for us to create an instance for each request to ensure it would always be correctly configured. With this change we need to rethink this. Is there a way to modify the client's network transport's session's configuration's httpAdditionalHeaders or how would you propose fixing this?

I thought we could still create a new one but store it in a non-computed property on the class but this doesn't work for some reason, fetches never call the result handler.

Thanks!

private var storedApollo: ApolloClient? //must be held onto strongly
var apollo: ApolloClient {
    get {
        let configuration = URLSessionConfiguration.ephemeral
        if let authToken = RestNetworking.shared.authToken {
            configuration.httpAdditionalHeaders?["x-auth-token"] = authToken
        }
        
        let url = RestNetworking.shared.urlComponents.url!.appendingPathComponent("graphql")
        let transport = HTTPNetworkTransport(url: url, session: URLSession(configuration: configuration))
        
        storedApollo = ApolloClient(networkTransport: transport)
        return storedApollo!
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that have a question which should be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions