RxSwift extensions for Apollo.
github "scottrhoyt/RxApollo"Add RxApollo.swift to your project.
All the reactive extensions are encapsulated in the rx property of an ApolloClient.
import Apollo
import RxSwift
import RxApollo
letapollo:ApolloClientletdisposeBag=DisposeBag()Fetching works just how you would expect it to:
// Let's get our hero's name and print it or the error if there is one.
apollo.rx.fetch(query:HeroNameQuery()).map{ $0.hero?.name }.subscribe(onNext:{ heroName inprint("Our hero's name is \(heroName).")}, onError:{ error inprint("Received error: \(error).")}).disposed(by: disposeBag)// Let's watch to see if our hero's name changes and print it or the error if there is one.
apollo.rx.watch(query:HeroNameQuery()).map{ $0.hero?.name }.subscribe(onNext:{ heroName inprint("Our hero's name is \(heroName).")}, onError:{ error inprint("Received error: \(error).")}).disposed(by: disposeBag)Watching also works quite well with using RxCocoa bindings:
import RxCocoa
letheroField:UITextField
// Let's watch to see if our hero's name changes and set a text field.
apollo.rx.watch(query:HeroNameQuery()).map{ $0.hero?.name }.asDriver(onErrorJustReturn:nil).drive(heroField.rx.text).disposed(by: disposeBag)Mutations follow the same pattern as well:
// Let's upvote a post.
apollo.rx.perform(mutation:UpvotePostMutation(postId: postId)).subscribe().disposed(by: disposeBag)MIT