Hi Team,
We have developed a product using Objective C code, now we want to integrate integrate apollo graph QL SDK in the product so we can do it?
It’s possible, but it’ll be a bit of an uphill battle since the Apollo SDK makes really heavy use of Swift features, particularly structs and generics.
I think the way I’d start is by basically writing an API wrapper in Swift that has one method per query. Then you can use each method to populate ViewModel
type objects that are declared (and used) in Objective-C.
So if you had a LoginMutation
that looked something like this in GraphQL:
mutation Login($username: String!, $password: String! {
loginUser(username: $username, password: $password) {
id
givenName
familyName
}
}
And a class that holds data in Objective-C with a .h
file like this:
@class ObjCUser: NSObject
@property (nonatomic) NSString *id;
@property (nonatomic) NSString *givenName;
@property (nonatomic) NSString *familyName;
- (instancetype)initWithId:(NSString *id)
givenName:(NSString *givenName)
familyName:(NSString *familyName);
@end
You could create a wrapper in swift that basically looks like this to call through to it:
@objc
public class ApolloWrapper: NSObject {
private let apollo = ApolloClient() // This setup is actually longer, keeping it short for this example
@objc
public init() {
super.init()
}
@objc
public func loginUser(username: String,
password: String,
completion: ((ObjCUser?, Error?) -> Void)) {
let loginMutation = LoginMutation(username: username, password: password)
apollo.perform(mutation: loginMutation) { result in
switch result {
case .failure(let error):
completion(nil, error)
case .success(let graphQLResult):
if let generatedUser = graphQLResult.data?.loginUser {
let returnUser = ObjCUser(id: generatedUser.id,
givenName: generatedUser.givenName,
familyName: generatedUser.familyName)
completion(returnUser, nil)
} // handle any errors returned from the server if needed.
}
}
}
Definitely not as straightforward as being able to just set up your operations and call them, but it’s certainly doable.
if possible, Can you please share any sample Objective C application with Apollo SDK?