Creating a Dependency from ApolloClient #1611
-
So far using TCA I've been able to create dependencies in our app fairly easily. I created an ApiClient which can fetch from our back end. It doesn't have any generic constraint (it just returns (Data, URLResponse). This means I have one ApiClient for the whole app which is good. I then extend this ApiClient to add a generic function which fetches the (Data, URLResponse) and then decodes the data into a generic Decodable type. I also created a DataRepo which does have a generic constraint as it creates a subscribable cache from an endpoint and has a generic constraint on its Resource type to know what to decode when it gets new values. This means I can have a DataRepo for each Resource which is fine as there are a limited number of them. However... with this current problem I'm a bit stuck. I'm exploring using the ApolloClient for the first time and trying to work out how I can create a https://github.com/dependency for it to use within TCA. Ideally at the call site it would look something like this...
However, I'm having difficulty getting this to work. The problem is that the ApolloClient https://www.apollographql.com/docs/ios/docc/documentation/apollo/apolloclientprotocol conforms to a protocol that uses generic functions. In the case above the underlying ApolloClient function looks like this...
I'd like to do two things to this... Make it async await (which is the easy part). For instance, I could do this...
But now that means every query in the app will have its own separate GraphQLClient and they would all have to be defined as separate dependencies. I have tried to use some of the new existential types to try and get around this. But being honest, I don't fully know how those work or what they're for yet. I wonder if I could just create the GraphQLClient using an internal non-generic function (somehow) and then extend it (a bit like the ApiClient above) to add a public generic function to it? I've also tried just going all the way back to Any (just to see if I could get that working somehow) but that had issues with types and not knowing what was what. I saw the similar post about the KeychainClient #1604 but I'm not sure how I would override these for things like mocking etc... Hmm... unless... I turn it around and make the difference in the dependency be the actual ApolloClient. That way I can have a generic function fetch but then the .live will pass that on to the live ApolloClient and the test will pass it to an "unimplemented" version of the ApolloClientProtocol or something. 🤔 Sorry for the rambling post. I hope I managed to articulate the problem I'm having. Any advice or help would be greatly appreciated. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
A quick update on here. I've received a very detailed answer on the swift forums from @mbrandonw 👍🏻 I'll link here just to close the cross post. (I am never sure where to post questions) 😅 https://forums.swift.org/t/creating-a-dependency-from-apolloclient/61185/3 |
Beta Was this translation helpful? Give feedback.
-
@mbrandonw What did you mean when you said?
What holds onto |
Beta Was this translation helpful? Give feedback.
A quick update on here. I've received a very detailed answer on the swift forums from @mbrandonw 👍🏻
I'll link here just to close the cross post.
(I am never sure where to post questions) 😅
https://forums.swift.org/t/creating-a-dependency-from-apolloclient/61185/3