Today we’re looking at [the very cool] GraphQL and a popular iOS client library for interfacing with GraphQL servers. Being that the rest of the application in question is already chock-full of promises, it made sense to want an option for using promises rather than Apollo’s GraphQLQueryWatcher + callbacks. The good news is that Apollo has support for promises, the bad news is that they wrote it themselves.

Love it when a framework recreates another popular library and we spend time writing plumbing like this. The simplicity of this little bit of code obscures how annoying this really is. Now with multiple declarations of the same class in this project, we have lots of compiler hinting and needless explicit module qualifications that could be avoided by not reinventing the wheel. If you’re creating a library for iOS and want to include Promise functionality, please use PromiseKit.

Of course, the real answer is a set of concurrency primitives in Swift. Soon.

import Apollo
import PromiseKit

extension Apollo.Promise {
    func pkPromise() -> PromiseKit.Promise<Value> {
        return PromiseKit.Promise(resolvers: { (fulfill, reject) in
            self.andThen({ (v) in
                fulfill(v)
            }).catch({ (err) in
                reject(err)
            })
        })
    }
}