Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to modify URLSessionConfiguration on ApolloClient #922

Closed
jordanhbuiltbyhq opened this issue Nov 25, 2019 · 2 comments
Closed

How to modify URLSessionConfiguration on ApolloClient #922

jordanhbuiltbyhq opened this issue Nov 25, 2019 · 2 comments
Labels
question Issues that have a question which should be addressed

Comments

@jordanhbuiltbyhq
Copy link

jordanhbuiltbyhq commented Nov 25, 2019

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!
    }
}
@designatednerd
Copy link
Contributor

I would recommend using the pre-flight delegate method which allows you to modify the request rather than using the session configuration to modify the auth token if it's changing on a regular basis rather than trying to futz with constantly recreating it.

@designatednerd designatednerd added the question Issues that have a question which should be addressed label Nov 26, 2019
@jordanhbuiltbyhq
Copy link
Author

That'll do the trick thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants