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

Multiple ApolloClients per URLSessionClient instances ? #3213

Closed
petarbelokonski opened this issue Sep 7, 2023 · 4 comments
Closed

Multiple ApolloClients per URLSessionClient instances ? #3213

petarbelokonski opened this issue Sep 7, 2023 · 4 comments
Labels
enhancement Issues outlining new things we want to do or things that will make our lives as devs easier feature New addition or enhancement to existing solutions networking-stack question Issues that have a question which should be addressed

Comments

@petarbelokonski
Copy link

Question

I need to be able to issue operation requests on multiple OperationQueues (say 2). That said I need to keep a reference of multiple URLSessionClient instances. What I think will work best is to keep multiple separate instances of ApoIloClient each tied up to a specific URLSessionClient. Is there a preferred/suggested way of doing that given the InterceptorProvider protocol and that we need to initialize ApolloClient with a given InterceptorProvider before making the request ?

@petarbelokonski petarbelokonski added the question Issues that have a question which should be addressed label Sep 7, 2023
@calvincestari
Copy link
Member

Hi @petarbelokonski 👋🏻 - it sounds like you simply want to know how to deal with the interceptor provider, is that correct?

If so then you can reuse the DefaultInterceptorProvider, which is what's used internally and gives you the standard provider list, in both Apollo Client instances. The following code demonstrates how to do that:

let url = URL(string: "http://localhost:4000/")!
let sharedStore = ApolloStore() // will give you an InMemoryNormalizedCache
let sessionClient = URLSessionClie nt() // can pass in the session configuration if needed
let interceptorProvider = DefaultInterceptorProvider(client: sessionClient,store: sharedStore) // standard provider list
let networkTransport = RequestChainNetworkTransport(interceptorProvider: interceptorProvider, endpointURL: url)
let apolloClient = ApolloClient(networkTransport: networkTransport, store: sharedStore)

Does that answer your question or did I misunderstand?

@petarbelokonski
Copy link
Author

@calvincestari Thanks for your reply. I know how to work with the interceptor provider - my question is a structural one:

I need to be able to use multiple URLSessionClient instances (with different operation queues) for my requests. However I have 1 InterceptorProvider instance per 1 ApolloClient instance.

The flow goes as follows:

  • Create InterceptorProvider instance
  • Create RequestChainNetworkTransport instance
  • Create ApolloClient instance
  • Send request using the ApolloClient
  • InterceptorProvider is asked for interceptors and a NetworkFetchInterceptor is provided which contains the URLSessionClient.

I need to be able to use multiple URLSessionClients (so that I can use multiple OperationQueues, and the way I currently see this fit is to have an ApolloClient instance tied up to a concrete URLSessionClient and concrete OperationQueue - do you think this approach is fine or else would you be able suggest an alternative ?

@calvincestari
Copy link
Member

I'm still not sure I 100% understand the issue but all the networking classes we've mentioned so far are pretty much linked 1:1. So the only way you would be able to use multiple URLSessionClient instances is with an entirely separate instance of ApolloClient. Whether they use the same interceptor chain is moot since each is initialized with a URL session client.

I don't see anything particularly wrong with that other than the burden of maintaining multiple clients but what I am far less certain of is whether a store instance should be shared between them. I suspect that is not advisable and could lead to some difficult to debug caching issues.

@petarbelokonski
Copy link
Author

petarbelokonski commented Sep 8, 2023

@calvincestari That's exactly the idea - I need to use multiple URLSessionClient instances. My thought process brought me to the idea of having a URLSessionClient instance per AplloClient instance hence multiple ApolloClient instances. However I haven't considered what you said about the store which is a valid concern.

My goal is to be able to determine which URLSessionClient to use per concrete fetch method invocation. However, I must be able to use any URLSessionClient for any GraphQLOperation. I could potentially use the interceptors(for operation) to check which is the operation but I need it to happen before that - it must happen before I call the fetch method on the ApolloClient so that I can specify which URLSessionClient to use for the current fetch flow. I can maybe create a subclass of my autogenerated GraphQLQuery type and use typecasting to determine which URLSessionClient to use but that feels wonky 🤔

@AnthonyMDev AnthonyMDev added enhancement Issues outlining new things we want to do or things that will make our lives as devs easier networking-stack feature New addition or enhancement to existing solutions labels Sep 15, 2023
@AnthonyMDev AnthonyMDev added this to the Release 2.0 milestone Sep 15, 2023
@AnthonyMDev AnthonyMDev closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Issues outlining new things we want to do or things that will make our lives as devs easier feature New addition or enhancement to existing solutions networking-stack question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

3 participants