Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scottrhoyt/rxapollo
RxSwift extensions for Apollo
https://github.com/scottrhoyt/rxapollo
apollo graphql ios rxswift swift
Last synced: about 2 months ago
JSON representation
RxSwift extensions for Apollo
- Host: GitHub
- URL: https://github.com/scottrhoyt/rxapollo
- Owner: scottrhoyt
- License: mit
- Created: 2017-05-09T09:04:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-30T08:43:37.000Z (over 6 years ago)
- Last Synced: 2024-10-29T08:18:57.945Z (2 months ago)
- Topics: apollo, graphql, ios, rxswift, swift
- Language: Swift
- Homepage:
- Size: 89.8 KB
- Stars: 57
- Watchers: 5
- Forks: 22
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# RxApollo
[RxSwift](https://github.com/ReactiveX/RxSwift) extensions for [Apollo](https://github.com/apollographql/apollo-ios).## Installation
### Carthage
```sh
github "scottrhoyt/RxApollo"
```### Manual
Add `RxApollo.swift` to your project.
## Usage
All the reactive extensions are encapsulated in the `rx` property of an `ApolloClient`.
```swift
import Apollo
import RxSwift
import RxApollolet apollo: ApolloClient
let disposeBag = DisposeBag()
```### Fetch
Fetching works just how you would expect it to:
```swift
// 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 in
print("Our hero's name is \(heroName).")
}, onError: { error in
print("Received error: \(error).")
})
.disposed(by: disposeBag)
```### Watch
```swift
// 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 in
print("Our hero's name is \(heroName).")
}, onError: { error in
print("Received error: \(error).")
})
.disposed(by: disposeBag)
```Watching also works quite well with using `RxCocoa` bindings:
```swift
import RxCocoalet heroField: 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)
```### Mutate
Mutations follow the same pattern as well:
```swift
// Let's upvote a post.
apollo.rx.perform(mutation: UpvotePostMutation(postId: postId))
.subscribe()
.disposed(by: disposeBag)
```## License
MIT