Skip to content

Use the same URLSession as my main networking stack? #2851

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

Closed
SilverTab opened this issue Feb 24, 2023 · 8 comments
Closed

Use the same URLSession as my main networking stack? #2851

SilverTab opened this issue Feb 24, 2023 · 8 comments
Labels
awaiting response question Issues that have a question which should be addressed

Comments

@SilverTab
Copy link

Question

I'm not sure if what I'm trying to do is possible, or if it makes sense, but here's what I am trying to achieve: We're transitioning progressively to GraphQL in our app, and for queries that requires a user to be authenticated to work, I need to pass in a sessionid cookie that was acquired in our app's main URLSession (so in other words, our old non-GraphQL API, and our new GraphQL endpoint both work for authenticated queries, using the same sessionid cookie).

Our main (non-GraphQL) networking stack uses a URLSession that is initialized with the .default configuration, and after logging in, using ProxyMan, I can see that the sessionid cookie is present.

Now, when setting up the Apollo client, I'm doing this:

let apolloClient: ApolloClient = {
        let cache = InMemoryNormalizedCache()
        let store = ApolloStore(cache: cache)
        let client = URLSessionClient(sessionConfiguration: .default)
        let provider = DefaultInterceptorProvider(client: client, store: store)
        let url =  URL(string: "<our endpoint url>")!
        let rct = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: url)
        return ApolloClient(networkTransport: rct, store: store)
    }()

However, when I inspect the queries made to our GraphQL endpoint in proxyman, I do see a sessionid cookie being sent, BUT it's a different session id than the one I see on requests to our non-GraphQL endpoint.

Am I doing something wrong? Is there a way for the GraphQL client's URLSession, and our non-GraphQL client to share a sessionid cookie?

@SilverTab SilverTab added the question Issues that have a question which should be addressed label Feb 24, 2023
@calvincestari
Copy link
Member

I have the same expectation as you that using the same configuration (.default) should enable the shared cookie to be used, and from what I can see in the documentation that is implied. Apollo iOS doesn't do anything with the session configuration other than pass it in to the URLSession initializer so the code you've shown above looks correct; in fact it's the only way to initialize an ApolloClient with a custom session configuration.

I unfortunately don't have any other suggestions. Are you able to use the shared session cookie in another URLSession, i.e.: not ApolloClient?

@SilverTab
Copy link
Author

Thanks for your reply! I just wanted to double-check that I wasn't misunderstanding something on Apollo's side... I think I figured out what was happening... the 2 APIs (non-GraphQL vs. GraphQL) are actually on different domains right now, I assume the cookies are restricted to a given domain and therefore, even though the same session configuration is used, they're not shared between the 2 🤦‍♂️

@calvincestari
Copy link
Member

Yes, that make sense as the limitation. Thanks for following up.

@RamblinWreck77
Copy link

Honestly it would be really nice to be able to create our own URLSession and pass it in to Apollo. We're in the same boat (split between legacy API and Apollo) and having 2 URLSessions is causing some issues.

@calvincestari
Copy link
Member

Thanks @RamblinWreck77. One problem with this approach is URLSessionClient sets itself as the delegate for the initialized URLSession, which is going to cause problems if both Apollo iOS and your other client want to do that.

@RamblinWreck77
Copy link

RamblinWreck77 commented Sep 11, 2023

@calvincestari In my case my existing API implementation uses the new async/await calls on URLSession, the oldschool delegate stuff isn't used at all.

Also, this is totally a hack but I think I found a way @SilverTab to do what you want (I wanted to use the same URLSession for GraphQL/Apollo as my legacy API).

I modified my Apollo Client setup code to take in a static URLSession client like:

static let client: URLSessionClient = .init(sessionConfiguration: ExampleAPIManager.customAPIConfig(),
                                                                     callbackQueue: .main)
                                                                     
static let apollo: ApolloClient = {

        let cache: InMemoryNormalizedCache = .init()
        let store: ApolloStore = .init(cache: cache)
        let provider: ApolloInterceptorProvider = .init(client: client,
                                                                                     store: store)
        let transport: RequestChainNetworkTransport = .init(interceptorProvider: provider,
                                                                                                 endpointURL: ExampleAPIManager.url)
        return ApolloClient(networkTransport: transport,
                                         store: store)
    }()

(sorry, indention is so weird on github)

Then, over in my legacy API:

let (data, response): (Data, URLResponse) = try await ApolloManager.client.session.data(for: request)

I'm trying to see if there's any problems with doing this, so far everything works as expected and we're back to 1 URLSession!

@TripwireNL
Copy link

TripwireNL commented Apr 23, 2025

Sorry for commenting on this issue after a long time, but we're running into the same problems. We're transitioning slowly from Alamofire to GraphQL. We have a pretty complex authentication system which now runs side by side e.g. Interceptors (Apollo) & RequestAdapter / Retrier (Alamofire).
It would be great if we could use URLSession to have 1 place where we do this whole complex authentication system instead of running them side by side until our Alamofire codebase it moved to GraphQL (which will probably take a long time). Is there any solution for this in the latest version of the Apollo GraphQL client on iOS?

@calvincestari
Copy link
Member

@TripwireNL - the 1.x versions will unfortunately remain this way. Good news though is that we're working on 2.0 (RFC) which is being designed to allow for the kind of URLSession configuration users are asking for.

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

No branches or pull requests

4 participants